Am trying to create a custom listview but it cant dispaly am really a newbie and need the help …. Here’s the code
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;
}
}
//then the listview layout 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>
//using the compound drawable layout
<?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>
//and the resource list of items
<?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>
you really haven’t given us enough to go on (like a logcat of errors if available or a log statement not being reached), it could be any number of things.
I’d say that there must be issue with the
View rowxml that you are inflatingView row = inflater.inflate(R.layout.list_item,. That could be the incorrect name, but is the entire layout, just theTextView? If so, you should (add the imageView &) put it in aViewGroup, likeRelativeLayout,LinearLayoutetc. just like your main layout is. But most alarming is probably:can you see that you’re referencing two different views by the same id? the latest image command might be screwing it up.
Moreover, it’s best practices not to do what’s unnecessary in
getViewsince it is called for each row of theListView. That’s what passing things through the constructor are for (items which you seem to have ignored), so that they’re only done once. Try it like this:you could have even passed all your constructor parameters straight through if they were all in the same master class.