android Xml-List view with filter

android is new for me. I am trying to develop a program on 1.5 platform but still in progress, plz guide me.

I have some information in following format


I want to show them on screen, I dont know which is recommended way to do this. After google I found 2 method. but I failed to successfully implement any of one.

Method 1

I break both columns data into 2 different arrays, then populate listactivity with array of column 1, enable filter & on clicked event I want to raise alert which should show Text clicked in tilte & desc from 2nd array as msg body based on position. But here is problem if using filter index becomes reinitialize :-(, and there didnot find another way to get text of that row.

public void onCreate(Bundle savedInstanceState) {

   setListAdapter(new ArrayAdapter<String>(this, 
                               android.R.layout.simple_list_item_1, Names));    


public void onListItemClick(ListView parent, View v, int position, long id) {

    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(description[position] + " -> " + position );
    builder.setPositiveButton("ok", null);;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

its not picking right item from position if filter used :-(, plz guide Can you share source code of this

Method B

Here I tried to generate list row from XML , but giving error that 1.5 jar file didnot allow modification :-(

    public View getView(int position, View convertView, ViewGroup parent) {

            ViewInflate inflater=context.getViewInflate();
            View row=inflater.inflate(R.layout.row, null, null);

            View row = (View) convertView;

            if (row==null) {
                LayoutInflater  inflater=context.getLayoutInflater();
            //    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                  row = inflater.inflate(R.layout.row,null);

            TextView label=(TextView)row.findViewById(;

            TextView description=(TextView)row.findViewById(;

            //    ImageView icon=(ImageView)row.findViewById(;
            //    icon.setImageResource(R.drawable.delete);


Plz suggest what is a right way to acconplish this, so app can show desc of item , has filter too. Plz share If you have any shouce code for this

Asked by: Stella427 | Posted: 20-01-2022

Answer 1

Solution you choose strictly depends on your needs. I will try to gather requirements and provide you with implementation which I think is the most suitable.
Ok, so lets enumerate some requirements:

  • there should be a custom layout for list item
  • there is a custom data, in your case it's two strings: "item" and "description
  • there should be a possibility to filter the list
  • list of items can be very long

Things worth to mention:
- If you have custom data that you need to display, and it's not just vector of strings, then good idea is to provide your own type for data. It's quite convenient to keep all row data in one place, even if some of the data is not displayed on list. Reflecting to your example: you have "item" and "description" - you may want to display just "item" but you want to have a possibility to get the description. Keep that in instance of one class. In example below it is RowData class. Please see RowData class.

- In case you want a custom layout, and it's not just a TextView, then you should implement your own adapter - probably the subclass of ArrayAdapter. Please take a look at CustomAdapter class.

- If you need to filter the list and you are using custom data type - provide toString() method for your type. Thanks to this method you will be able to use build-in Filter class. Its responsibility is to filter out items from the list that doesn't match text you entered. It just take text representation of items from your adapter, and use it with the filter. Please see the toString() method from RowData class.

- If list of items can be long, the good idea would be to reuse row views and use wrapper pattern. See getView() method and ViewHolder class.

public class CustomList extends ListActivity {
    private LayoutInflater mInflater;
    private Vector data;

 /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        data = new Vector();
        RowData rd = new RowData("aaa", "description1");
        rd = new RowData("bbb", "description2");
        rd = new RowData("ccc", "description3");

        CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,, data);

    public void onListItemClick(ListView parent, View v, int position, long id) {
     CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
  RowData row = adapter.getItem(position);  
     Builder builder = new AlertDialog.Builder(this);
     builder.setMessage(row.mDescription + " -> " + position );
     builder.setPositiveButton("ok", null);;

     * Data type used for custom adapter. Single item of the adapter.      
    private class RowData {
     protected String mItem;
  protected String mDescription;

  RowData(String item, String description){
      mItem = item;
      mDescription = description;      

  public String toString() {
   return mItem + " " +  mDescription;

    private class CustomAdapter extends ArrayAdapter {

  public CustomAdapter(Context context, int resource,
    int textViewResourceId, List objects) {
   super(context, resource, textViewResourceId, objects);


  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder = null;

   //widgets displayed by each item in your list
   TextView item = null;
   TextView description = null;

   //data from your adapter
   RowData rowData= getItem(position);

   //we want to reuse already constructed row views...
   if(null == convertView){
    convertView = mInflater.inflate(R.layout.custom_row, null);
    holder = new ViewHolder(convertView);
   holder = (ViewHolder) convertView.getTag();
   item = holder.getItem();

   description = holder.getDescription();  

   return convertView;

     * Wrapper for row data.
    private class ViewHolder {     
     private View mRow;
     private TextView description = null;
     private TextView item = null;

  public ViewHolder(View row) {
      mRow = row;

  public TextView getDescription() {
   if(null == description){
    description = (TextView) mRow.findViewById(;
   return description;

  public TextView getItem() {
   if(null == item){
    item = (TextView) mRow.findViewById(;
   return item;

Custom item layout:

<TextView android:text="text" android:id="@+id/item"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button" android:paddingRight="10dip"

<TextView android:text="text" android:id="@+id/description"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/item" android:paddingLeft="10dip"

Answered by: John456 | Posted: 21-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android