Yusuf Saifi
- Forum posts: 1
Sep 7, 2015, 2:01:33 PM via Website
Sep 7, 2015 2:01:33 PM via Website
Hello,
I am trying to populate an arrayList using arrayAdapter as ListView into a custom Layout in Android Studio, everything is fine not giving any error! but when i am running the app it loads okay but show nothing!
bellow are my codes:
MainActicity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item_forecast);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceHolderFragment extends Fragment{
private ArrayAdapter<String> mForecastAdapter;
public PlaceHolderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_main, container);
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 88/63",
"Weds - Sunny - 88/63",
"Thurs - Sunny - 88/63",
"Fri - Sunny - 88/63",
"Sat - Sunny - 88/63",
"Sun - Sunny - 88/63"
};
List<String> weekForecast = new ArrayList<String>(
Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView lv = (ListView) rootView.findViewById(R.id.listview_forecast);
lv.setAdapter(mForecastAdapter);
return rootView;
}
}
}
list_item_forecast.xml
<TextView
xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_forecast_textview">
</TextView>
fragment_main.xml
<FrameLayout xmlns:android="..."
xmlns:tools=".."
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast"/>
</FrameLayout>
Thanks so much in advance
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.