我有一个带有自定义 ArrayAdapter 的列表,它在列表项旁边加载图标,除了每个其他列表项都显示一个图标
public class MyAdapter extends ArrayAdapter<String>{
Context mContext;
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.filetypeslist, parent, false);
String[] items = mContext.getResources().getStringArray(R.array.filetypes);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.tvtype);
tv.setText(items[position]);
if(items[position].equals("txt")){
iv.setImageResource(R.drawable.txticon);//not showing up
}else if(items[position].equals("HTML")){
iv.setImageResource(R.drawable.htmlicon);
}else if(items[position].equals("JS")){
iv.setImageResource(R.drawable.javascripticon);//not showing up
}else if(items[position].equals("CSS")){
iv.setImageResource(R.drawable.cssicon);
}
return row;
}
}
public class NewFile extends ListActivity {
EditTextLineNumbers newet;
LayoutInflater mInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_file);
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_1, R.id.tvtype,
getResources().getStringArray(R.array.filetypes)));
}
}
***EDIT***
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.filetypeslist, parent, false);
}
String[] items = mContext.getResources().getStringArray(R.array.filetypes);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.tvtype);
tv.setText(items[position]);
if(items[position].equals("txt")){
iv.setImageResource(R.drawable.txticon);
}else if(items[position].equals("HTML")){
iv.setImageResource(R.drawable.htmlicon);
}else if(items[position].equals("JS")){
iv.setImageResource(R.drawable.javascripticon);
}else if(items[position].equals("PHP")){
iv.setImageResource(R.drawable.phpicon);
}else if(items[position].equals("CSS")){
iv.setImageResource(R.drawable.cssicon);
}
return row;
}
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="filetypes">
<item name="txt">Text</item>
<item name="html">HTML</item>
<item name="js">Javascript Class</item>
<item name="css">CSS</item>
<item name="php">PHP Class</item>
</string-array>
</resources>