Skip to content Skip to sidebar Skip to footer

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 'item1','description1' 'ite

Solution 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.

publicclassCustomListextendsListActivity {
    private LayoutInflater mInflater;
    private Vector data;

 /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        data = newVector();
        RowDatard=newRowData("aaa", "description1");
        data.add(rd);
        rd = newRowData("bbb", "description2");
        data.add(rd);
        rd = newRowData("ccc", "description3");
        data.add(rd);

        CustomAdapteradapter=newCustomAdapter(this, R.layout.custom_row,R.id.item, data);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }


    publicvoidonListItemClick(ListView parent, View v, int position, long id) {
     CustomAdapteradapter= (CustomAdapter) parent.getAdapter();
  RowDatarow= adapter.getItem(position);  
     Builderbuilder=newAlertDialog.Builder(this);
     builder.setTitle(row.mItem); 
     builder.setMessage(row.mDescription + " -> " + position );
     builder.setPositiveButton("ok", null);
     builder.show();
 }

    /**
     * Data type used for custom adapter. Single item of the adapter.      
     */privateclassRowData {
     protected String mItem;
  protected String mDescription;

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

  @Overridepublic String toString() {
   return mItem + " " +  mDescription;
  }
    }

    privateclassCustomAdapterextendsArrayAdapter {

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

  }

  @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
   ViewHolderholder=null;

   //widgets displayed by each item in your listTextViewitem=null;
   TextViewdescription=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 = newViewHolder(convertView);
    convertView.setTag(holder);
   }
   // 
   holder = (ViewHolder) convertView.getTag();
   item = holder.getItem();
   item.setText(rowData.mItem);

   description = holder.getDescription();  
   description.setText(rowData.mDescription);

   return convertView;
  }
    }

    /**
     * Wrapper for row data.
     *
     */privateclassViewHolder {     
     private View mRow;
     privateTextViewdescription=null;
     privateTextViewitem=null;

  publicViewHolder(View row) {
      mRow = row;
  }

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

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

Custom item layout:

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

Post a Comment for "Android Xml-list View With Filter"