这是我在stackoverflow中的第3个问题,因为我不知道如何使用stackoverflow。我的问题是我的论文项目。请帮我... 我有1个listview和1个SearchView。我想在我的Listview中添加搜索过滤器,我的问题是:
基本上,我们执行搜索后,列表视图中的数据位置不会刷新。但我不知道如何刷新ListView并获得正确的数据位置。
请帮助我...我坚持了好几天。 这是我的代码..
public class DosenActivity extends AppCompatActivity implements ListView.OnItemClickListener, SearchView.OnQueryTextListener {
//root url dari webservice
public static final String ROOT_URL = "http://fyptes.esy.es/";
//deklarasi varibel untuk mengirim data ke activity lain
public static final String ID = "id";
public static final String NAMA = "nama";
public static final String SITUS = "situs";
public static final String EMAIL = "email";
public static final String TELEPON = "telepon";
public static final String KANTOR = "kantor";
public static final String JABATAN = "jabatan";
public static final String MATKUL = "matkul";
public static final String FOTO = "foto";
//listview untuk menampilkan data
private ListView listview;
//varibel books bertipe List dan List tersebut berdasarkan objek Listdosen
private List<Listdosen> dosens;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dosen);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//inisialisasi listview
listview = (ListView) findViewById(R.id.listViewDosen);
//memanggil method untuk mengambil data dosen
getDosen();
//setting onItemClickListener untuk listview
listview.setOnItemClickListener(this);
}
private void getDosen() {
//Ketika Aplikasi mengambil data kita akan melihat progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Mengambil Data","Mohon tunggu..",false,false);
//Logging Interceptor
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//set Level Log
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())//GsonConverter untuk parsing json
.client(httpClient.build())
.build();
RestAPI service = retrofit.create(RestAPI.class);
Call<Model> call = service.loadListDosen();
call.enqueue(new Callback<Model>() { //Asyncronous Request
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
loading.dismiss();
List<Listdosen> dosen = response.body().getListdosen();
//memasukkan data dari varibel dosen ke dosens
dosens = dosen;
//memanggil method untuk menampilkan list
showList();
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
}
});
}
private void showList() {
//String array untuk menyimpan nama semua nama dosen
String[] items = new String[dosens.size()];
for (int i = 0; i < dosens.size(); i++) {
items[i] = dosens.get(i).getNama();
}
//Membuat Array Adapter for listview
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_dosen_listview, items);
//setting adapter untuk listview
listview.setAdapter(adapter);
listview.setTextFilterEnabled(true);
adapter.setNotifyOnChange(true);
}
//method ini akan dieksekusi ketikan listitem diklik
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//membuat intent
Intent intent = new Intent(this, DosenDetailActivity.class);
//mengambil dosen dari list
Listdosen listdosen = dosens.get(position);
//menambahkan detail dosen untuk intent
intent.putExtra(ID, listdosen.getId());
intent.putExtra(NAMA, listdosen.getNama());
intent.putExtra(SITUS, listdosen.getSitus());
intent.putExtra(EMAIL, listdosen.getEmail());
intent.putExtra(TELEPON, listdosen.getTelepon());
intent.putExtra(KANTOR, listdosen.getKantor());
intent.putExtra(JABATAN, listdosen.getJabatan());
intent.putExtra(MATKUL, listdosen.getMatkul());
intent.putExtra(FOTO, listdosen.getFoto());
//memulai activity lain untuk menampilkan detail dosen
startActivity(intent);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText))
{
listview.clearTextFilter();
}
else
{
listview.setFilterText(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:0)
使用以下代码搜索名称,然后根据搜索查询再次填充列表视图。对我来说工作正常。
edittext.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) {
textlength = edittext.getText().length();
ArrayList<HashMap<String, String>> arrayList2 = new ArrayList<HashMap<String, String>>();
// arrayList2 = arraylist;
for (int i = 0; i < arraylist.size(); i++) {
username[i] = arraylist.get(i).get("Username");
Log.d("unm", username[i]);
}
for (int i = 0; i < arraylist.size(); i++) {
if (textlength <= username[0].length()) {
if (username[i].toLowerCase().contains(
edittext.getText().toString().toLowerCase().trim())) {
arrayList2.add(arraylist.get(i));
}
}
}
adapter = new ListViewAdapter(getApplicationContext(), arrayList2);
listview.setAdapter(adapter);
}
@Override
public void afterTextChanged(Editable s) {
}
});