带过滤器的Andr​​oid XML的列表视图 [英] android Xml-List view with filter

查看:102
本文介绍了带过滤器的Andr​​oid XML的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android是新的我。 我想开发一个程序上的1.5平台,但仍在进行中,PLZ指导我。

我有一些信息,格式如下

 ITEM1,内容描述
项目2,内容描述
项目3,description3
ITEM4,description4
。
。
。
。
 

我想向他们展示在屏幕上,我不知道这是推荐的方式来做到这一点。谷歌后,我发现2方法。但我没能成功地实现任何一个。

方法1

我打破两列数据为2个不同的阵列,然后填充 listactivity其中列1的阵列,使过滤器和放大器;在点击事件中,我想提出警告,应表现出tilte和放大器文字点击;说明从基于位置的第2个数组作为味精的身体。 但这里是问题,如果使用过滤器的索引变得重新初始化:-(,还有因此未找到另一种方式来获得该行的文本。


 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

   setListAdapter(新ArrayAdapter<字符串>(这一点,
                               android.R.layout.simple_list_item_1,名称));
   getListView()setTextFilterEnabled(真)。

}
 


 公共无效onListItemClick(ListView的父,视图V,INT位置,长的id){

    建设者建设者=新AlertDialog.Builder(本);
    builder.setTitle(名称[位置]);
    builder.setMessage(介绍[位置] + - >中+位置);
    builder.setPositiveButton(OK,NULL);
    builder.show();
}
 


 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
<的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/你好
    />
< / LinearLayout中>
 


它不拾取从位置右项目,如果用:-(过滤器,PLZ指导 你可以分享这个来源$ C ​​$ C

B法

在这里,我试图生成XML列表行,但给人错误1.5 jar文件因此未允许修改: - (


 公开查看getView(INT位置,查看convertView,ViewGroup中父){

            / *
            ViewInflate充气= context.getViewInflate();
            查看排= inflater.inflate(R.layout.row,NULL,NULL);
            * /

            查看排=(查看)convertView;

            如果(行== NULL){
                LayoutInflater充气= context.getLayoutInflater();
            // LayoutInflater充气=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                  行= inflater.inflate(R.layout.row,NULL);
            }

            TextView的标签=(TextView中)row.findViewById(R.id.label);
            label.setText(项目[位置]);

            TextView的说明=(TextView中)row.findViewById(R.id.description);
            description.setText(项目[位置]);

            // ImageView的图标=(ImageView的)row.findViewById(R.id.icon);
            // icon.setImageResource(R.drawable.delete);


            返回(行);
        }
 


PLZ表明什么是正确的方式来acconplish这一点,所以应用程序可以显示项目的说明,有过滤了。 PLZ共享如果您有任何shouce code这个

解决方案


你选择严格的解决方案取决于您的需求。我会尽量收集需求并为您提供实现我认为这是最适合的。
好了,所以让我们列举了一些要求:

  • 应该有列表项的自定义布局
  • 有一个自定义的数据,你的情况是两个字符串:项和说明
  • 应该有一个可能性,以过滤列表
  • 产品清单可能会很长

事情值得一提:
- 如果您有需要显示自定义的数据,并且它不只是载体的字符串,然后好主意是提供自己的类型的数据。这是相当方便的将所有的行数据在同一个地方,即使一些数据未在列表中显示。反映到你的例子:你有项目和说明 - 你可能希望只显示项,但你想有可能得到的描述。记住,在一个类的实例。在下面的例子是RowData类。请参阅RowData类。

- 如果你想有一个自定义的布局,它不只是一个TextView,那么你就应该实现自己的适配器 - ArrayAdapter大概的子类。请看看CustomAdapter类。

- 如果你需要过滤列表,您使用的是自定义数据类型 - 提供toString()方法为你的类型。由于这种方法,你就可以使用内置的过滤器类。其职责是从一个不匹配你输入的文字列表中筛选出的项目。它只是把文字重的物品presentation从你的适配器,使用它的过滤器。请参阅从RowData类中的toString()方法。

- 如果产品清单可能会很长,好的想法是重用行的意见,并使用包装图案。见getView()方法和ViewHolder类。

 

公共类CustomList扩展ListActivity {
    私人LayoutInflater mInflater;
    私人矢量数据;

 / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        mInflater =(LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        数据=新的向量();
        RowData RD =新RowData(AAA,说明1);
        data.add(RD);
        RD =新RowData(BBB,说明2);
        data.add(RD);
        次=新RowData(CCC,description3);
        data.add(RD);

        CustomAdapter适配器=新CustomAdapter(这一点,R.layout.custom_row,R.id.item,数据);
        setListAdapter(适配器);
        getListView()setTextFilterEnabled(真)。
    }


    公共无效onListItemClick(ListView的父,视图V,INT位置,长的id){
     CustomAdapter适配器=(CustomAdapter)parent.getAdapter();
  RowData行= adapter.getItem(位置);
     建设者建设者=新AlertDialog.Builder(本);
     builder.setTitle(row.mItem);
     builder.setMessage(row.mDescription + - >+位置);
     builder.setPositiveButton(OK,NULL);
     builder.show();
 }

    / **
     *用于自定义适配器数据类型。适配器的单个项目。
     * /
    私有类RowData {
     保护字符串MITEM;
  保护字符串mDescription;

  RowData(字符串项,字符串描述){
      MITEM =项目;
      mDescription =描述;
     }

  @覆盖
  公共字符串的toString(){
   返回MITEM ++ mDescription;
  }
    }

    私有类CustomAdapter扩展ArrayAdapter {

  公共CustomAdapter(上下文的背景下,INT资源,
    INT textViewResourceId,对象列表){
   超(背景下,资源,textViewResourceId,对象);

  }

  @覆盖
  公共查看getView(INT位置,查看convertView,ViewGroup中父){
   ViewHolder支架=无效;

   //由每个项目在列表中显示的部件
   TextView的项目= NULL;
   TextView中描述= NULL;

   从您的适配器//数据
   RowData rowData =的getItem(位置);


   //我们要重用已建成一排的意见...
   如果(空== convertView){
    convertView = mInflater.inflate(R.layout.custom_row,NULL);
    持有人=新ViewHolder(convertView);
    convertView.setTag(保持器);
   }
   //
   支架=(ViewHolder)convertView.getTag();
   项目= holder.getItem();
   item.setText(rowData.mItem);

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

   返回convertView;
  }
    }

    / **
     *包装的行数据。
     *
     * /
    私有类ViewHolder {
     私人查看MROW;
     私人TextView中描述= NULL;
     私人TextView的项目= NULL;

  公共ViewHolder(查看行){
      MROW =行;
  }

  公众的TextView getDescription(){
   如果(空==介绍){
    说明=(TextView中)mRow.findViewById(R.id.description);
   }
   返回描述;
  }

  公众的TextView的getItem(){
   如果(空==项目){
    项目=(TextView中)mRow.findViewById(R.id.item);
   }
   归还物品;
  }
    }
}
 

自定义项目布局:
  

  

 < TextView的Andr​​oid版本:文本=文本机器人:ID =@ + ID /项目
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ + ID /按钮机器人:paddingRight =10dip
机器人:以下属性来=10dip>< / TextView的>

< TextView的Andr​​oid版本:文本=文本机器人:ID =@ + ID /说明
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ + ID /项目机器人:以下属性来=10dip
机器人:paddingRight =10dip>< / TextView的>
 

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"
"item2","description2"
"item3","description3"
"item4","description4"
.
.
.
.

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) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

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

}


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

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


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>


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(R.id.label);
            label.setText(items[position]);

            TextView description=(TextView)row.findViewById(R.id.description);
            description.setText(items[position]);

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


            return(row);
        }


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

解决方案


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:

<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"
	android:paddingLeft="10dip"></TextView>

<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"
	android:paddingRight="10dip"></TextView>

这篇关于带过滤器的Andr​​oid XML的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆