我正在尝试创建一个自定义列表视图,但它不能显示我真的是一个新手并且需要帮助....这是代码
public class Main_Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
setListAdapter((ListAdapter) new MyAdapter(this,
android.R.layout.simple_list_item_1,R.id.textView1,
getResources().getStringArray(R.array.categories)));
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.categories);
ImageView image = (ImageView)row.findViewById(R.id.textView1);
TextView text =(TextView)row.findViewById(R.id.textView1);
text.setText(items[position]);
if(items[position].equals("Life")){
image.setImageResource(R.drawable.lifeico);
}
else if(items[position].equals("Corporate")){
image.setImageResource(R.drawable.corpico);
}
else if(items[position].equals("umash")){
image.setImageResource(R.drawable.umashico);
}
return row;
}
}
//然后是listview布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
//使用复合可绘制布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/lifeico"
android:drawablePadding="5dp"
android:text="@string/textview"
android:textSize="25sp" >
</TextView>
//以及item的资源列表
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="categories">
<item name="Life">Individual Life</item>
<item name="Corporate">Corporate Insurance</item>
<item name="Umash">Umash Funeral Services</item>
</string-array>
</resources>