Reputation: 49
I have a sample code that i made for my custom navigation drawer. I don't know how can i add icons to every item position in the listview. The position indicates an intent in order to link the current page to another.I also put string items in an array named data so that i can add it via adapter. The listview is navList Please help!
public class HomePatient extends Activity {
final String[] data ={"Home","Health Progress","Diet","Workout","Medications","Help","Settings","Logout"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.homepatient);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
navList.setAdapter(adapter);
ArrayAdapter<String> adaptertext = new ArrayAdapter<String>(HomePatient.this, R.layout.textholder, data);
navList.setAdapter(adaptertext);
navList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = data[position];
if(position==1)
{
Intent i = new Intent(HomePatient.this, HealthProgress.class);
i.putExtra("TEXT", text);
startActivity(i);
//onDestroy();
finish();
}
if(position==2)
{
Intent i = new Intent(HomePatient.this, DietPatient.class);
i.putExtra("TEXT", text);
startActivity(i);
//onDestroy();
finish();
}
if(position==3)
{
//Intent i = new Intent(HomePatient.this, WorkoutPatient.class);
//i.putExtra("TEXT", text);
// startActivity(i);
//onDestroy();
//finish();
}
if(position==4)
{
Intent i = new Intent(HomePatient.this, TreatmentPatient.class);
i.putExtra("TEXT", text);
startActivity(i);
// onDestroy();
finish();
}
if(position==7)
{
Intent i = new Intent(HomePatient.this, PatientLogin.class);
i.putExtra("TEXT", text);
startActivity(i);
//onDestroy();
finish();
}
}
});
Upvotes: 0
Views: 2352
Reputation: 507
/* here is my activity*/
public class abc extends Activity{
public String[] items{
"A","B"
};
public void oncreate(***)
{
.........
.......
Listview lv =(Listview)findviewbyid(R.id.listview); // reference to list view
Myadapter myadapter = new Myadapter(this,android.r.layout.simple_list_item_1,android.r.id.text,items);
lv.setadapter(myadapter); // i am setting my list view to this adapter
}
}
// here goes my adapter class
public class Myadapter extends Arrayadapter<String>
{
Context context;
String[] objs;
public Myadapter(context context , resouce , txtresid , String[] objs)
{
super(context , resouce , txtresid , objs);
this.context = context;
this.objs= objs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// now i will inflate my xml by returning view
// create an xml file that contais imageview and text or whatever you want
// i have named my xml file as simple_list.xml
Layoutinflater inflater = (Layoutinflater) context.getsystemservice(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflater(R.layout.simple_list.xml,parent,false);//check out layout inflater class and its method
Imageview iv = (Imageview)view.findviewbyid(R.id.imgview);
Textview tv = (TextView) view.findviewbyid(R.id.txtview);
tv.setText(objs[position]);
if(objs[position].equals("A"))
{
iv.setimageresource(R.drawable.someimage);
}
else if(objs[position].equals("B"))
{
iv.setimageresource(R.drwable.nextimage);
}
return view;
}
}
Upvotes: 1
Reputation: 3322
You have use baseadapter for that and use custom adapter and custom list row item
have a look at
http://www.pcsalt.com/android/listview-using-baseadapter-android/
Upvotes: 2
Reputation: 132992
I don't know how can i add icons to every item position in the listview
Because you are using default android.R.layout.simple_list_item_1 layout which contain only one TextView
. to show images and text both in ListView
row you will need to create custom layout then pass it to ListView Adapter. see following example to create custom ArrayAdapter
Customizing Android ListView Items with Custom ArrayAdapter
Upvotes: 3