Listview / Listadapter With 2 Strings In Android Project
Solution 1:
http://www.youtube.com/watch?v=wDBM6wVEO70. Listview talk by Romain guy( android developer at google).
Main.xml
<ListViewandroid:id="@+id/list"android:layout_width="fill_parent"android:layout_height="0dip"android:focusableInTouchMode="false"android:listSelector="@android:color/transparent"android:layout_weight="2"android:headerDividersEnabled="false"android:footerDividersEnabled="false"android:dividerHeight="8dp"android:divider="#000000"android:cacheColorHint="#000000"android:drawSelectorOnTop="false"></ListView></LinearLayout>Customw row. row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"android:background="#ffffff"
><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"android:background="@drawable/itembkg"
/><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="TextView" /></LinearLayout>publicclassCustomListViewextendsActivity {
/** Called when the activity is first created. */ListView lv1;
Customlistadapter cus;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button b= (Button) findViewById(R.id.remove);
lv1 = (ListView) findViewById(R.id.list);
cus= newCustomlistadapter(this);
lv1.setAdapter(cus);
}
}
Custom list adapter. Inflate custom layout for each row.
publicclassCustomlistadapterextendsArrayAdapter {
private LayoutInflater mInflater;
Context c;
publicCustomlistadapter(CustomListView customListView) {
super(customListView, 0);
// TODO Auto-generated constructor stubthis.mInflater = LayoutInflater.from(customListView);
c=customListView;
}
publicintgetCount() {
return20; // number of listview rows.
}
public Object getItem(int arg0) {
return arg0;
}
publiclonggetItemId(int arg0) {
return arg0;
}
public View getView(finalint arg0, View arg1, ViewGroup arg2) {
final ViewHolder vh;
vh= newViewHolder();
if(arg1==null )
{
arg1=mInflater.inflate(R.layout.row, arg2,false);
vh.tv1= (TextView)arg1.findViewById(R.id.textView1);
vh.tv2= (TextView)arg1.findViewById(R.id.textView2);
}
else
{
arg1.setTag(vh);
}
vh.tv1.setText("hello");
vh.tv2.setText("hello");
return arg1;
}
staticclassViewHolder//use a viewholder for smooth scrolling and performance.
{
TextView tv1,tv2;
}
}
Edit:
Your activity will have a listview. This is set in oncreate setContentView(R.layout.activity_main);. The main layout will have a listview. You set the adapter of listview as listview.setAdapter(youradapter);
Then listview will have custom layout ie row.xml inflated for each row item. You custom adapter for listview is where the row.xml is inflated. You defined your class CustomAdapter which extends ArrayAdapter. You override a set of methods.
getCount() --- size of listview.
getItem(int position) -- returns the positiongetView(int position, View convertView, ViewGroup parent)
// position is the position in the listview.//convertview - view that is tobe inflated// you will return the view that is infated. You will have to use a viewholder for smooth scrolling and performance. Imagine 1000 rows is lstview with images it may cause memory exceptions. One way to get rid of this is to recycle views. The visible views(rows) are not recycled. The video in the link at the top has a detail explanation on the topic
activity_main.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"
android:orientation="vertical"
android:background="#0095FF">
<ListViewandroid:id="@+id/list"android:layout_width="fill_parent"android:layout_height="0dip"android:focusableInTouchMode="false"android:listSelector="@android:color/transparent"android:layout_weight="2"android:headerDividersEnabled="false"android:footerDividersEnabled="false"android:dividerHeight="8dp"android:divider="#000000"android:cacheColorHint="#000000"android:drawSelectorOnTop="false"></ListView></LinearLayout>row.xml (layout inflated for each listview row)
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="Header" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="80dp"android:layout_gravity="center"android:text="TextView" /></LinearLayout>MainActivity
publicclassMainActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListViewll= (ListView) findViewById(R.id.list);
CustomAdaptercus=newCustomAdapter();
ll.setAdapter(cus);
}
classCustomAdapterextendsBaseAdapter
{
LayoutInflater mInflater;
publicCustomAdapter()
{
mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn30;
}
@Overridepublic Object getItem(int position) {
// TODO Auto-generated method stubreturn position;
}
@OverridepubliclonggetItemId(int position) {
// TODO Auto-generated method stubreturn0;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stubfinal ViewHolder vh;
vh= newViewHolder();
if(convertView==null )
{
convertView=mInflater.inflate(R.layout.row, parent,false);
vh.tv2= (TextView)convertView.findViewById(R.id.textView2);
vh.tv1= (TextView)convertView.findViewById(R.id.textView2);
}
else
{
convertView.setTag(vh);
}
vh.tv1.setText("my text");
vh.tv2.setText("Postion = "+position);
return convertView;
}
classViewHolder
{
TextView tv1,tv2;
}
}
}

Post a Comment for "Listview / Listadapter With 2 Strings In Android Project"