当我运行应用程序时,listview从数据库中填充并且没关系,但是当我想根据我在edittext中编写的内容过滤适配器时,没有任何事情发生在logcat中也没有任何内容。
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
public class Search extends ListFragment implements TextWatcher {
Adapter adapter;
ArrayList<Item> myList = new ArrayList<Item>();
ListView myListView;
Databasehelper myDbHelper;
LinearLayout not_found;
EditText edtSeach;
Button searchbtn;
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search, container, false);
edtSeach = (EditText) v.findViewById(R.id.calc_txt_Prise);
edtSeach.addTextChangedListener(this);
searchbtn = (Button) v.findViewById(R.id.searchbtn);
not_found = (LinearLayout) v.findViewById(R.id.layout_not_found);
return v;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myDbHelper = new Databasehelper(getActivity());
myDbHelper.openDatabase();
Cursor cursor = myDbHelper.QueryData("select * from zankodict");
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Item itemm = new Item();
itemm.setId(cursor.getString(0));
itemm.setEng(cursor.getString(1));
itemm.setKurd(cursor.getString(2));
itemm.setAr(cursor.getString(3));
itemm.setFa(cursor.getString(4));
itemm.setTr(cursor.getString(5));
myList.add(itemm);
} while (cursor.moveToNext());
}
adapter = new Adapter(getActivity(), R.layout.single_row, myList);
myListView = getListView();
myListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
myDbHelper.closeDataBase();
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String localObject0 = Search.this.myList.get(position).getId();
String localObject1 = Search.this.myList.get(position).getEng();
String localObject2 = Search.this.myList.get(position).getKurd();
String localObject3 = Search.this.myList.get(position).getAr();
String localObject4 = Search.this.myList.get(position).getFa();
String localObject5 = Search.this.myList.get(position).getTr();
myDbHelper.openDatabase();
myDbHelper.insertRow2(localObject0, localObject1, localObject2, localObject3,localObject4,localObject5);
myDbHelper.closeDataBase();
Intent localIntent;
localIntent = new Intent(getActivity().getBaseContext(), MyResults.class);
localIntent.putExtra("enG", localObject1);
localIntent.putExtra("kurD", localObject2);
localIntent.putExtra("aR", localObject3);
getActivity().startActivity(localIntent);
}
});
}
searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyTask searchAsyn = new MyTask();
searchAsyn.cancel(true);
searchAsyn.execute("seachdb",edtSeach.getText().toString());
}
});
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MyTask searchAsyn = new MyTask();
searchAsyn.cancel(true);
searchAsyn.execute("seachdb",s.toString()); }
@Override
public void afterTextChanged(Editable s) {
}
class MyTask extends AsyncTask<String,Void,ArrayList<Item>> {
ArrayList<Item> fillarary;
@Override
protected ArrayList<Item> doInBackground(String... params) {
String searchwd = params[1];
String methods = params[0];
if(methods.equals("seachdb")){
fillarary = fillList(searchwd);
}
return fillarary;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(ArrayList<Item> fill) {
myListView.setAdapter(new Adapter(getActivity(), R.layout.single_row, fill));
validateItemCount();
}
public ArrayList<Item> search (String keyword){
ArrayList<Item> searcharray = null;
myDbHelper.openDatabase();
Cursor cursor2 = myDbHelper.QueryData("select * from zankodict WHERE eng LIKE '%"+ keyword + "%'");
if (cursor2 != null) {
if (cursor2.moveToFirst()) {
do {
Item itemm = new Item();
itemm.setId(cursor2.getString(0));
itemm.setEng(cursor2.getString(1));
itemm.setKurd(cursor2.getString(2));
itemm.setAr(cursor2.getString(3));
itemm.setFa(cursor2.getString(4));
itemm.setTr(cursor2.getString(5));
searcharray.add(itemm);
} while (cursor2.moveToNext());
}
}
return searcharray;
}
private ArrayList<Item> fillList(String keyword) {
ArrayList<Item> newarray;
if (keyword.equals(BuildConfig.FLAVOR)) {
newarray = new ArrayList();
} else if (keyword.toCharArray().length < 2) {
newarray = search(keyword);
} else {
newarray = search(keyword);
}
return newarray;
}
private void validateItemCount() {
if (fillarary.size() == 0) {
not_found.setVisibility(View.VISIBLE);
} else {
not_found.setVisibility(View.INVISIBLE);
}
}
}
}
答案 0 :(得分:0)
使用名为myList的ArrayList初始化您的适配器。 一旦你得到一个价值&#34; searcharray&#34;在你的搜索功能中调用它,
myList = searcharray;
adapter.notifyDataSetChanged();
这将使用搜索结果刷新列表视图中的值。