已过滤的Listview未更新

时间:2010-06-15 12:35:09

标签: android listview

我遇到过滤列表视图未更新的问题。

在我的应用程序中有一个自定义列表视图,它有两个文本视图和一个图像视图作为行元素。

过滤器工作正常但我的自定义列表视图未更新;结果显示了List的第一行。

这是我的代码。

abc.java文件

public class abc extends ListActivity 
{

private ArrayList<String> m_orders = null;

private OrderAdapter m_adapter;

List<String> Title_List=new ArrayList<String>();

List<String> Author_List=new ArrayList<String>();

private ArrayAdapter<String> adapter = null;

private LayoutInflater mInflater;

private Vector data;

private EditText filterText = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}

@Override

public void onStart() {
super.onStart();


m_orders = new ArrayList<String>();


filterText = (EditText) findViewById(R.id.search_box);


filterText.addTextChangedListener(filterTextWatcher);


setListAdapter(m_adapter=new OrderAdapter(this,R.layout.list_row, m_orders));


m_adapter.notifyDataSetChanged();


getListView().setTextFilterEnabled(true);


}

private TextWatcher filterTextWatcher = new TextWatcher() {

@Override

public void beforeTextChanged(CharSequence s, int start, int count,
                int after) 

{


        }
        @Override
public void onTextChanged(CharSequence s, int start, int before,
                int count) {


           m_adapter.getFilter().filter(s, new Filter.FilterListener() {
                                       public void onFilterComplete(int count) {
                                  Log.d(null, "filter complete! count: "+ count);
                                           m_adapter.notifyDataSetChanged();

                                       }
                                   }); 
      }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

    };


private class OrderAdapter extends ArrayAdapter {

            private ArrayList<String> items;

     public OrderAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
                    super(context, textViewResourceId, items);
                    this.items = items;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                    View v = convertView;
                    if (v == null) {
     LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.list_row, null);
                    }
                    String order = m_orders.get(position);
                    String description = Item_List.get(position);
                    if (order != null)
                    {

                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                            if (tt != null) {
                                  tt.setText("Title: "+order);    

                              }
                            if (bt != null) {
                             bt.setText("des: "+description);    
                                                              }
                    }
                    return v;
            }
    }

      @Override
public void onListItemClick(ListView parent, final View v, int pos, long id) 
{

}

}

list_row.xml文件

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="40px"
        android:layout_height="40px"
        android:layout_marginRight="6dip"
       />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="50px">
        <TextView
            android:paddingTop="1pt"
            android:id="@+id/toptext"
            android:layout_width="200px"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:paddingBottom="1pt"
            android:ellipsize="marquee" 

            android:marqueeRepeatLimit="marquee_forever" 
            android:lines="1" 
            android:scrollHorizontally="true"
        />
        <TextView
            android:paddingTop="1pt"
            android:layout_width="200px"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:paddingBottom="2pt"
            android:ellipsize="marquee" 

            android:marqueeRepeatLimit="marquee_forever" 
            android:lines="1" 
            android:scrollHorizontally="true"
        />
    </LinearLayout>
</LinearLayout>

我认为过滤器有效,但只有结果未在列表视图中更新。

1 个答案:

答案 0 :(得分:0)

我想我遇到了同样的问题。我花了大约5个小时来找到它。 错误可能在你的getView()中。

使用ArrayAdapter方法的getItem()方法而不是

String order = m_orders.get(position);
String description = Item_List.get(position);

问题是过滤后的数据包含在ArrayAdapter.mObjects中。如果您使用自己的数据变量,则不会过滤任何内容。