Thursday, June 21, 2012

Example For ListView Using BaseAdapter

Above is an example of ListView in android which extends BaseAdapter. Here we are inflating Layout to ListView.

Code:
public class ListExample extends ListActivity {

private ArrayList<String> dataname = new ArrayList<String>();
private ArrayList<String> datacode = new ArrayList<String>();
Context con = this;


public static class viewHolder {
TextView tname;
TextView tcode;
}

private class EfficientAdapter extends BaseAdapter {

private Context context;
LayoutInflater inflater;

public EfficientAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
inflater = LayoutInflater.from(context);

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return datacode.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listinflate, null);
holder = new viewHolder();

convertView.setTag(holder);

} else {
holder = (viewHolder) convertView.getTag();
}
holder.tname = (TextView) convertView
.findViewById(R.id.textViewName);
holder.tcode = (TextView) convertView
.findViewById(R.id.textViewCode);
holder.tname.setText(dataname.get(position));
holder.tcode.setText(datacode.get(position));

return convertView;
}

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listView);

for (int i = 0; i < 10; i++) {
dataname.add("Name" + i);
datacode.add("Code" + i);
}

setListAdapter(new EfficientAdapter(con));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(con, "Item :"+position, Toast.LENGTH_LONG).show();
}
}

In listView.xml put this:

<?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" >


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" android:divider="@color/Grey" android:dividerHeight="0.5dp">
    </ListView>


</LinearLayout>


In listinflate.xml put this:

<?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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Name"
            android:textColor="@color/Blue"
            android:textSize="6pt"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textViewCode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Code"
            android:textColor="@color/black"
            android:textSize="6pt" />
    </LinearLayout>

</LinearLayout>