我在Android上进行列表视图搜索有困难,请帮助我。
这是我的吸气剂安装程序,名称是toko.java
public class Toko {
private String no;
private String str_id;
private String nama;
private String alamat;
private String kabupaten;
private String provinsi;
private String latitude;
private String longitude;
private String sms;
public void setNo(String no) {
this.no = no;
}
String getNo() {
return no;
}
public void setStr_id (String str_id) {
this.str_id = str_id;
}
String getStr_id() {
return str_id;
}
public void setNama (String nama) {
this.nama = nama;
}
public String getNama() {
return nama;
}
public void setAlamat (String alamat) {
this.alamat = alamat;
}
String getAlamat() {
return alamat;
}
public void setKabupaten (String kabupaten) {
this.kabupaten = kabupaten;
}
String getKabupaten() {
return kabupaten;
}
public void setProvinsi (String provinsi) {
this.provinsi = provinsi;
}
String getProvinsi() {
return provinsi;
}
public void setLatitude (String latitude) {
this.latitude = latitude;
}
String getLatitude() {
return latitude;
}
public void setLongitude (String longitude) {
this.longitude = longitude;
}
String getLongitude() {
return longitude;
}
public void setSms (String sms) {
this.sms = sms;
}
String getSms() {
return sms;
}
}
这是我的适配器,名称是TokoAdapter.java
public class TokoAdapter extends BaseAdapter implements Filterable {
//private ArrayList<HashMap<String, String>> data;
private ArrayList<Toko> data_toko = new ArrayList<Toko>();
private List<String> originalData = null;
private List<String> filteredData = null;
private ItemFilter mFilter = new ItemFilter();
private static LayoutInflater inflater = null;
public TokoAdapter(Activity a, ArrayList<Toko> d) {
data_toko = d;
inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data_toko.size();
}
public Object getItem(int position) {
return data_toko.get(position);
}
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.listview_toko, null);
}
TextView no = vi.findViewById(R.id.no);
TextView str_id = vi.findViewById(R.id.str_id);
TextView nama = vi.findViewById(R.id.nama);
TextView alamat = vi.findViewById(R.id.alamat);
TextView kabupaten = vi.findViewById(R.id.kabupaten);
TextView provinsi = vi.findViewById(R.id.provinsi);
TextView latitude = vi.findViewById(R.id.latitude);
TextView longitude = vi.findViewById(R.id.longitude);
TextView sms = vi.findViewById(R.id.sms);
Toko daftar_toko = data_toko.get(position);
no.setText(daftar_toko.getNo());
str_id.setText(daftar_toko.getStr_id());
nama.setText(daftar_toko.getNama());
alamat.setText(daftar_toko.getAlamat());
kabupaten.setText(daftar_toko.getKabupaten());
provinsi.setText(daftar_toko.getProvinsi());
latitude.setText(daftar_toko.getLatitude());
longitude.setText(daftar_toko.getLongitude());
sms.setText(daftar_toko.getSms());
return vi;
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<>(count);
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
这是我的布局代码,名称是activity_read_toko.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".crud.toko.ReadTokoActivity"
android:padding="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/key"
android:layout_width="347dp"
android:layout_height="match_parent"
android:background="@drawable/search_view"
android:hint="@string/pencarian_nama_toko"
android:inputType="text"
android:padding="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_search_black_24dp"
android:id="@+id/toko_btnSearch"
android:padding="10dp"/>
</LinearLayout>
<ListView
android:id="@+id/listview_toko"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" />
</LinearLayout>
这是我的活动代码,名称为ReadTokoActivity.java
public class ReadTokoActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
ListView list;
JSONParser jParser = new JSONParser();
ArrayList<Toko> daftar_toko = new ArrayList<Toko>();
JSONArray daftarToko, daftarSearchToko = null;
ImageButton BtnSearch;
TokoAdapter adapter;
EditText Query;
String query, url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_toko);
list = (ListView) findViewById(R.id.listview_toko);
BtnSearch = findViewById(R.id.toko_btnSearch);
Query = findViewById(R.id.key);
// BtnSearch.setOnClickListener(this);
Query.addTextChangedListener(this);
//jalankan ReadDataTask
ReadDataTask m= (ReadDataTask) new ReadDataTask().execute();
}
@SuppressLint("StaticFieldLeak")
class ReadDataTask extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadTokoActivity.this);
pDialog.setMessage("Mohon Tunggu..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... sText) {
return getDataList();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
if(result.equalsIgnoreCase("Exception Caught")){
Toast.makeText(ReadTokoActivity.this, "Unable to connect to server,please check your internet connection!", Toast.LENGTH_LONG).show();
}
if(result.equalsIgnoreCase("no results")){
Toast.makeText(ReadTokoActivity.this, "Data empty", Toast.LENGTH_LONG).show();
}else {
adapter = new TokoAdapter(ReadTokoActivity.this,daftar_toko);
list.setAdapter(adapter);
//Adapter menampilkan data mahasiswa ke dalam listView
}
}
//method untuk memperoleh daftar mahasiswa dari JSON
String getDataList(){
Toko tempMarker = new Toko();
List<NameValuePair> parameter = new ArrayList<NameValuePair>();
try {
JSONObject json = jParser.makeHttpRequest(Konfigurasi.URL_READ_TOKO,"POST", parameter);
int success = json.getInt(Konfigurasi.TAG_SUCCESS);
if (success == 1) { //Ada record Data (SUCCESS = 1)
//Getting Array of daftar_mhs
daftarToko = json.getJSONArray(Konfigurasi.TAG_TOKO);
// looping through All daftar_mhs
for (int i = 0; i < daftarToko.length(); i++){
JSONObject c = daftarToko.getJSONObject(i);
tempMarker = new Toko();
tempMarker.setNo(c.getString(Konfigurasi.TAG_NO));
tempMarker.setStr_id(c.getString(Konfigurasi.TAG_STR_ID));
tempMarker.setNama(c.getString(Konfigurasi.TAG_NAMA));
tempMarker.setAlamat(c.getString(Konfigurasi.TAG_ALAMAT));
tempMarker.setKabupaten(c.getString(Konfigurasi.TAG_KABUPATEN));
tempMarker.setProvinsi(c.getString(Konfigurasi.TAG_PROVINSI));
tempMarker.setLatitude(c.getString(Konfigurasi.TAG_LATITUDE));
tempMarker.setLongitude(c.getString(Konfigurasi.TAG_LONGITUDE));
tempMarker.setSms(c.getString(Konfigurasi.TAG_SMS));
daftar_toko.add(tempMarker);
}
return "OK";
}else{
//Tidak Ada Record Data (SUCCESS = 0)
return "no results";
}
} catch (Exception e) {
e.printStackTrace();
return "Exception Caught";
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = Query.getText().toString().toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
}
@Override
public void onClick(View v) {
}
}
,当我在edittext中输入关键字时,列表视图没有变化。 请解决我的问题。 然后我在Google中搜索问题,但没有一个成功。
答案 0 :(得分:0)
虽然在TextChanged之后输入了字符串
const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;
在适配器中制作方法
@Override
public void afterTextChanged(Editable s) {
String names = s.toString();
List<Toko> newList = new ArrayList<>();
for (Toko r : cityList){
//citylist will be your daftar_toko list
String name = r.getCity();
if (name.toLowerCase().contains(names.toLowerCase())){
newList.add(r);
}
}
//make seFilter method in adapter and pass your new list
mAdapter.setFilter(newList);
}
答案 1 :(得分:0)
- 尝试一下
msearch.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){
int textlength = s.length();
ArrayList<SqliteGetSet> tempArrayList = new ArrayList<SqliteGetSet>();
for(SqliteGetSet c: list){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(s.toString().toLowerCase())
|| c.getMno().toLowerCase().contains(s.toString().toLowerCase()))
{
tempArrayList.add(c);
}
}
}
adapter = new SqliteAdapter(SqliteExample.this,R.layout.row_sqlite_list, tempArrayList);
listView.setAdapter(adapter);
}
@Override
public void afterTextChanged(Editable s) {
}
});
msearch-Edittext名称 SqliteGetSet-模型类