Ich möchte meine Listview filtern, die mit Volley bevölkert ist. Ich verwende ListAdapter und SimpleAdapter Listenansicht zu bevölkern und seine adaequat ... aber wenn ich TEXTWATCHER auf editText implementieren und getFilter(), um es hinzuzufügen ... es gibt einen Luft (Die Methode kann nicht lösen ‚getFilter() ')getFilter() funktioniert nicht mit ListAdapter
Hilf mir dabei ... wie kann ich meine Liste filtern? Code ist unten ...
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class grabedDb extends AppCompatActivity {
EditText grabResults;
String url = "http://MY_URL_HERE";
ArrayList<HashMap<String, String>> Item_List;
ProgressDialog PD;
ArrayAdapter adapter;
// JSON Node names
public static final String ITEM_ID = "name";
public static final String ITEM_NAME = "rate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grabed_db);
grabResults = (EditText) findViewById(R.id.grabed_et_search);
grabResults.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) {
//perform Search
if (s.toString().equals("")){
//Reset List
ReadDataFromDB();
} else {
grabedDb.this.adapter.getFilter().filter(s);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Item_List = new ArrayList<HashMap<String, String>>();
PD = new ProgressDialog(this);
PD.setMessage("Please Wait...");
PD.setCancelable(false);
ReadDataFromDB();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
private void ReadDataFromDB() {
PD.show();
JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
int success = response.getInt("success");
if (success == 1) {
JSONArray ja = response.getJSONArray("mydrugs");
for (int i = 0; i < ja.length(); i++) {
JSONObject jobj = ja.getJSONObject(i);
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_ID, jobj.getString(ITEM_ID));
item.put(ITEM_NAME,
jobj.getString(ITEM_NAME));
Item_List.add(item);
} // for loop ends
String[] from = { ITEM_ID, ITEM_NAME };
int[] to = { R.id.item_name, R.id.item_id };
adapter = new SimpleAdapter(
getApplicationContext(), Item_List,
R.layout.list_items, from, to);
ListView myList = (ListView) findViewById(R.id.list_list_listView);
myList.setAdapter(adapter);
PD.dismiss();
myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent modify_intent = new Intent(grabedDb.this,
updatedata.class);
modify_intent.putExtra("item", Item_List.get(position));
startActivity(modify_intent);
}
});
} // if ends
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
PD.dismiss();
}
});
// Adding request to request queue
MyApplication.getInstance().addToReqQueue(jreq);
}
}
Es funktioniert auch für mich ... Danke –