android Xml-List view with filter

前端 未结 1 1558
时光取名叫无心
时光取名叫无心 2021-02-11 09:13

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

         


        
1条回答
  •  北恋
    北恋 (楼主)
    2021-02-11 09:41

    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. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        
            mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            data = new Vector();
            RowData rd = new RowData("aaa", "description1");
            data.add(rd);
            rd = new RowData("bbb", "description2");
            data.add(rd);
            rd = new RowData("ccc", "description3");
            data.add(rd);
    
            CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,R.id.item, data);
            setListAdapter(adapter);
            getListView().setTextFilterEnabled(true);
        }
    
    
        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.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.      
         */
        private class RowData {
         protected String mItem;
      protected String mDescription;
    
      RowData(String item, String description){
          mItem = item;
          mDescription = description;      
         }
    
      @Override
      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);
    
      }
    
      @Override
      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);
        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.
         *
         */
        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(R.id.description);
       }
       return description;
      }
    
      public TextView getItem() {
       if(null == item){
        item = (TextView) mRow.findViewById(R.id.item);
       }
       return item;
      }     
        }
    }
    

    Custom item layout:

    
    
    
    

    0 讨论(0)
提交回复
热议问题