0
我在我的片段中實現了自定義列表視圖(使用其自定義適配器)。有兩個問題:Andorid ListView:onClickListener和過濾器問題
1)當我單擊列表中的項目時,即使setOnItemClickListener
已實施,也不會發生任何事情。
2)我想過濾我的列表,所以我在列表頂部添加了一個可編輯的TextView
實現addTextChangedListener
。它工作的很好,但是當我在列表中的TextView
唯一的第一個元素中刪除所有字符的出現了,不是所有如我所料
這裏有一種myfragment
<?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>
這裏有是XML我的片段的一部分代碼。
@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;
}
這是我在我的代碼 – FF91
已經寫了一點點的改變對項目單擊監聽器,並導入進口android.widget.AdapterView.OnItemClickListener庫 –
其與進口一樣 – FF91