Android ListView:onClickListener和过滤器问题

时间:2015-09-05 10:38:43

标签: android listview filter onclicklistener

我在片段中实现了自定义列表视图(带有自定义适配器)。有两个问题:

1)当我点击列表中的项目时,即使setOnItemClickListener已实施,也没有任何反应。

2)我想过滤我的列表,因此我在列表实现TextView之上添加了一个可编辑的addTextChangedListener。它工作得很好,但是当我删除TextView中的所有字符时,只显示列表的第一个元素而不是我预期的全部

这里有myfragment

的xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:background="#ffffff">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editableText"
        android:layout_gravity="center_horizontal"
        android:editable="true"
        android:hint="Search Here"
        android:padding="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp" />

    <ListView

        android:id="@+id/listSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:paddingStart="15dp"
        android:paddingEnd="15dp" />

</LinearLayout>

这里有我片段的部分代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {

    View firstAccessView;
    if(savedBundle==null) {
        firstAccessView = inflater.inflate(R.layout.search_fragment_layout, null);

        ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle("Search Friend");

        for(int i=0; i<8; i++){
            Contact c = new Contact(idContact[i], nameSurname[i], facebookId[i], timeStamp[i]);
            this.rows.add(c);

        }


        adapter = new SearchListAdapter(getActivity(), this.rows);
        list = (ListView) firstAccessView.findViewById(R.id.listSearch);
        list.setAdapter(adapter);

        list.setOnScrollListener(this);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //TODO Auto-generated method stub

                Toast.makeText(getActivity().getApplicationContext(), "item selected", Toast.LENGTH_SHORT).show();

            }
        });

        this.editableText = (TextView) firstAccessView.findViewById(R.id.editableText);
        this.editableText.addTextChangedListener(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) {
                List<Contact> temp = new ArrayList<Contact>();
                int length = editableText.getText().length();
                temp.clear();
                for(int i = 0; i<rows.size(); i++){
                    if(editableText.getText().toString().equalsIgnoreCase("")){
                        list.setAdapter(new SearchListAdapter(getActivity(), rows));
                    }
                    if(length < rows.get(i).getName().length()){
                        if(editableText.getText().toString().equalsIgnoreCase((String)rows.get(i).getName().subSequence(0,length))){
                            temp.add(rows.get(i));

                        }
                    }

                }
                if(temp.size()>0){
                    list.setAdapter(new SearchListAdapter(getActivity(), temp));
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }else{
        firstAccessView = getView();
    }
    return firstAccessView;
}

1 个答案:

答案 0 :(得分:0)

导入  import android.widget.AdapterView.OnItemClickListener;

并使用此代码

list.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
   //code here
});

}