Reputation: 49
I'm trying to create a listview ( List For Menu ) . Because i'm new, so i'm trying this tutorial http://www.androidbegin.com/tutorial/android-custom-listview-texts-tutorial/. , here is what i do
SettingFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_setting, container, false);
list = (ListView) view.findViewById(R.id.setting_menulist);
String[] MenuName = new String[] {
"Profil" ,
"Change Log",
"Check For Update",
"Sign Out"
};
String[] ActivityName = new String[] {
"ProfilActivity",
"ChangelogActivity",
"",
""};
adapter = new SettingMenuAdapter( getActivity(), MenuName , ActivityName );
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("View To Load");
alertDialog.setMessage("Here Is view to Load" + view);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
return view;
}
SettingMenuAdapter
public class SettingMenuAdapter extends BaseAdapter {
Context context;
String[] MenuName;
String[] ActibityName;
ListView list;
LayoutInflater inflater;
public SettingMenuAdapter(Context context, String[] MenuName, String[] ActivityName) {
this.context = context;
this.MenuName = MenuName;
}
@Override
public int getCount() {
return MenuName.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.fragment_setting, parent, false);
list = (ListView) itemView.findViewById(R.id.setting_menulist);
return itemView;
}
}
When i run it, i dont't see any error, but my list view is empty. Did i miss something ?
i already fix it. Create new xml called settingitem
then change my adapter to this
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.settingitem, parent, false);
MenuListName = (TextView) itemView.findViewById(R.id.menuname);
MenuListName.setText(MenuName[position]);
return itemView;
}
Upvotes: 0
Views: 111
Reputation: 1565
your adapter is wrong
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.fragment_setting, parent, false);
list = (ListView) itemView.findViewById(R.id.setting_menulist);
return itemView;
}
here you are replacing listview.it must be a listview item
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtrank;
TextView txtcountry;
TextView txtpopulation;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Locate the TextViews in listview_item.xml
txtrank = (TextView) itemView.findViewById(R.id.rank);
txtcountry = (TextView) itemView.findViewById(R.id.country);
txtpopulation = (TextView) itemView.findViewById(R.id.population);
// Capture position and set to the TextViews
txtrank.setText(rank[position]);
txtcountry.setText(country[position]);
txtpopulation.setText(population[position]);
return itemView;
}
you are inflating same thing in fragment onCreate
and adapter getView
Upvotes: 4