如何解决ListView中的SearchView过滤器
我需要帮助来解决有关searchview过滤器的问题...
这是我的代码:
activity_kehadiran.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".kehadiran">
<SearchView
android:id="@+id/simpleSearchView"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="Search Here" />
<TextView
android:id="@+id/kehadiran"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:layout_below="@+id/simpleSearchView"
android:textColor="@color/colorPrimaryDark"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text = "LAPORAN ABSENSI"
/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/kehadiran"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tablerow">
<TextView
android:textColor="#000000"
android:text="TANGGAL ABSENSI"
android:textStyle="bold"
android:id="@+id/tanggal"
android:background="#2adcff"
android:gravity="center"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="LOKASI IN"
android:textStyle="bold"
android:id="@+id/depof1"
android:background="#2adcff"
android:gravity="center"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="JAM IN"
android:textStyle="bold"
android:id="@+id/f1"
android:gravity="center"
android:background="#2adcff"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="LOKASI OUT"
android:textStyle="bold"
android:id="@+id/depof4"
android:background="#2adcff"
android:gravity="center"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="JAM OUT"
android:id="@+id/f4"
android:textStyle="bold"
android:gravity="center"
android:background="#2adcff"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="KETERANGAN"
android:id="@+id/telat"
android:gravity="center"
android:textStyle="bold"
android:background="#2adcff"
android:layout_weight="1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
</TableRow>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
list_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:id="@+id/listItem"
android:orientation="horizontal">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*" >
<TableRow>
<TextView
android:textColor="#000000"
android:text="Tanggal"
android:gravity="center"
android:maxLength="10"
android:id="@+id/tanggal"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="Lokasi Berangkat"
android:gravity="center"
android:id="@+id/depof1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="Berangkat"
android:gravity="center"
android:id="@+id/f1"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="Lokasi Pulang"
android:gravity="center"
android:id="@+id/depof4"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="Pulang"
android:gravity="center"
android:id="@+id/f4"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:text="telat"
android:id="@+id/telat"
android:gravity="center"
android:layout_width="140dp"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
ListViewAdapter.java
package com.example.eis2.Item;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.example.eis2.detail_absensi;
import com.example.eis2.R;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class ListViewAdapter extends ArrayAdapter<keteranganmodel> {
public static List<keteranganmodel> movieItemList;
private Context context;
public ListViewAdapter(List<keteranganmodel> movieItemList,Context context) {
super(context,R.layout.list_item,movieItemList);
this.movieItemList = movieItemList;
this.context = context;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View listViewItem = inflater.inflate(R.layout.list_item,null,true);
TextView tanggal = listViewItem.findViewById(R.id.tanggal);
TextView depof1 = listViewItem.findViewById(R.id.depof1);
TextView f1 = listViewItem.findViewById(R.id.f1);
TextView depof4 = listViewItem.findViewById(R.id.depof4);
TextView f4 = listViewItem.findViewById(R.id.f4);
TextView telat = listViewItem.findViewById(R.id.telat);
keteranganmodel movieItem = movieItemList.get(position);
tanggal.setText(movieItem.getShift_day());
depof1.setText(movieItem.getDepo_f1());
f1.setText(movieItem.getF1());
depof4.setText(movieItem.getDepo_f4());
f4.setText(movieItem.getF4());
telat.setText(movieItem.getKet_absensi());
// status//
if ("null".equalsIgnoreCase(f1.getText().toString())) {
f1.setBackgroundColor(Color.YELLOW);
}
if ("null".equalsIgnoreCase(depof1.getText().toString())) {
depof1.setBackgroundColor(Color.YELLOW);
}
if ("null".equalsIgnoreCase(depof4.getText().toString())) {
depof4.setBackgroundColor(Color.YELLOW);
}
if ("null".equalsIgnoreCase(f4.getText().toString())) {
f4.setBackgroundColor(Color.YELLOW);
}
return listViewItem;
}
}
keteranganmodel.java
package com.example.eis2.Item;
public class keteranganmodel {
String shift_day,F1,depo_f1,F4,depo_f4,ket_absensi;
public keteranganmodel(String shift_day,String F1,String depo_f1,String F4,String depo_f4,String ket_absensi) {
this.shift_day = shift_day;
this.F1 = F1;
this.depo_f1 = depo_f1;
this.F4 = F4;
this.depo_f4 = depo_f4;
this.ket_absensi = ket_absensi;
}
public String getShift_day() { return shift_day; }
public String getF1() {
return F1;
}
public String getDepo_f1() { return depo_f1; }
public String getF4() { return F4; }
public String getDepo_f4() {
return depo_f4;
}
public String getKet_absensi() {
return ket_absensi;
}
}
kehadiran.java
package com.example.eis2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.eis2.Item.ListViewAdapter;
import com.example.eis2.Item.keteranganmodel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static com.example.eis2.Item.LoginItem.KEY_NIK;
public class kehadiran extends AppCompatActivity {
public static ListView list;
private List<keteranganmodel> movieItemList;
TextView f1;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kehadiran);
list = findViewById(R.id.list);
movieItemList = new ArrayList<>();
f1 = findViewById(R.id.f1);
searchView = findViewById(R.id.simpleSearchView);
absensi();
}
private void absensi() {
String nik_baru = getIntent().getStringExtra(KEY_NIK);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
String currentDateandTime = sdf.format(new Date());
StringRequest stringRequest = new StringRequest(Request.Method.GET,"http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&shift_day_2=2020-08-31&badgenumber=0100028400",new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
final JSONArray movieArray = obj.getJSONArray("data");
for (int i = 0; i < movieArray.length(); i++) {
final JSONObject movieObject = movieArray.getJSONObject(i);
final keteranganmodel movieItem = new keteranganmodel(
movieObject.getString("shift_day"),movieObject.getString("F1"),movieObject.getString("depo_f1"),movieObject.getString("F4"),movieObject.getString("depo_f4"),movieObject.getString("ket_absensi"));
movieItemList.add(movieItem);
}
final ListViewAdapter adapter = new ListViewAdapter(movieItemList,getApplicationContext());
list.setAdapter(adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return true;
}
});
Collections.reverse(movieItemList);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
在这种情况下:
我想搜索2020-09-16,但这是在我使用searchview时发生的
所以,主要问题是:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return true;
}
});
你们能解决这个问题吗?我很感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)