0
我已經成功地在每個字符串/項目旁邊設置了一個帶有簡單圖標的ListFragment。爲ListFragment中的每個項目添加不同的圖標
現在我想爲我的列表中的每個項目/字符串添加不同的圖標/圖像。
我該如何做到這一點?我在使用正常活動之前完成了它,但未使用ListFragments。
在此先感謝...
ListFragment類:
public class Fragment2 extends ListFragment {
public class MyListAdapter extends ArrayAdapter<String> {
Context myContext;
public MyListAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
myContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.month);
label.setText(month[position]);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
//Set icon here
icon.setImageResource(R.drawable.ic_launcher);
return row;
}
}
String[] month = { " ", "Abstract", "Animal", "Anime", "Artistic", "Cartoon", "Comics",
"Celeberity", "Cars", "Fantasy", "Food" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(),
* android.R.layout.simple_list_item_1, month);
* setListAdapter(myListAdapter);
*/
MyListAdapter myListAdapter = new MyListAdapter(getActivity(),
R.layout.row, month);
setListAdapter(myListAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragments_layout2, container, false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(),
getListView().getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
}