android - I have custom certain items in my listview but i want different textview in all the item in textview -
this activity contains 3 classes mainactivity1 , singlerow2 , shivvadapter
package com.example.shivnandan.fit; import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.res.resources; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.view.contextmenu; import android.view.layoutinflater; import android.view.view; import android.support.design.widget.navigationview; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.edittext; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.arraylist; public class mainactivity1 extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { listview list; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); list = (listview) findviewbyid(r.id.listview2); list.setadapter(new shivvadapter(this)); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == 0) { toast.maketext(getapplicationcontext(), "gender selected ", toast.length_short).show(); } if (position == 1) { toast.maketext(getapplicationcontext(), "age selected ", toast.length_short).show(); } if (position == 2) { toast.maketext(getapplicationcontext(), "height selected", toast.length_short).show(); } if (position == 3) { toast.maketext(getapplicationcontext(), "weight selected", toast.length_short).show(); } if (position == 4) { toast.maketext(getapplicationcontext(), "reset selected", toast.length_short).show(); } } }); } class singlerow2 { string title; int image; singlerow2(string title ) { this.title =title; } } class shivvadapter extends baseadapter { arraylist<singlerow> list; context context; shivvadapter(context c) { context = c; list=new arraylist<singlerow>(); resources res = c.getresources(); string[] titles=res.getstringarray(r.array.profiletitle); // string[] descriptions=res.getstringarray(r.array.description); int[] images = {r.drawable.one,r.drawable.two,r.drawable.three,r.drawable.two,r.drawable.two,r.drawable.two}; (int =0 ; i<5 ; i++) { list.add(new singlerow(titles[i],images[i])); } } @override public int getcount() { return list.size(); } @override public object getitem(int i) { return list.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int i, view view, viewgroup viewgroup) { layoutinflater inflater =(layoutinflater) context.getsystemservice(context.layout_inflater_service); view row = inflater.inflate(r.layout.single_row2,viewgroup,false); textview title= (textview) row.findviewbyid(r.id.textview); singlerow temp = list.get(i); title.settext(temp.title); return row; } }
this single_row2.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingtop="30dp" android:layout_width="match_parent" android:layout_height="82dp" android:id="@+id/single_row" android:background="@drawable/side_nav_bar" android:foregroundtint="#0a0a0a"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="main activity" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_marginleft="50dp" android:layout_margintop="-6dp" /> <textview android:layout_width="50dp" android:layout_height="40dp" android:id="@+id/hometext" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:hint="gender" /> </relativelayout>
you can create list , send shivvadapter constructon. example:
list<string> stringlist = new arraylist<>(); stringlist.add("my text 1"); stringlist.add("my text 2"); stringlist.add("my text 3"); list.setadapter(new shivvadapter(this, stringlist));
so in shivvadapter save list , use in getview() method example:
public view getview(int position, view convertview, viewgroup parent) { textview title = (textview) convertview.findviewbyid(r.id.title); title.settext(mystringlist.get(position)); return convertview; }
Comments
Post a Comment