我想在我的listview中应用搜索功能,这是使用齐射库从服务器获取我想要的是当我在edittext中输入日期时它只显示listview中的那一行并消除其他行(比如普通搜索功能)。我在google上看到了很多,但它使用基本适配器显示了这个功能但在我的情况下我使用了数组适配器。我该如何应用此类帮助我
Emp_Statistics
import android.app.Activity;
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.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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 org.w3c.dom.Text;
import java.util.HashMap;
import java.util.Map;
public class Emp_Statistics extends Activity
{
public static final String JSON_URL = "*****";
private ProgressDialog pDialog;
private ListView listView;
String emp_id,email_rec;
CustomListStatistics cl;
String empr_id,empr_company,task,task_desc,email_receive;
TextView tv_empty;
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emp__statistics);
inputSearch=(EditText)findViewById(R.id.inputSearch);
tv_empty=(TextView)findViewById(R.id.empty);
listView = (ListView)findViewById(R.id.list_statistics);
emp_id=getIntent().getExtras().getString("EMP_ID");
empr_id=getIntent().getExtras().getString("EMPR_ID");
empr_company=getIntent().getExtras().getString("EMPR_COMPANY");
email_receive=getIntent().getExtras().getString("EMP_EMAIL");
sendRequest();
pDialog = new ProgressDialog(Emp_Statistics.this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Listview listner
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Emp_Statistics.this.cl.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
TextView tv_date_in=(TextView)view.findViewById(R.id.date_in);
TextView tv_date_out=(TextView)view.findViewById(R.id.date_out);
TextView tv_empr_id=(TextView)view.findViewById(R.id.empr_name);
TextView tv_time_in=(TextView)view.findViewById(R.id.time_in);
TextView tv_time_out=(TextView)view.findViewById(R.id.time_out);
TextView tv_task=(TextView)view.findViewById(R.id.task_name);
TextView tv_task_desc= (TextView)view.findViewById(R.id.task_desc);
//String info=""+tv_date_in.getText().toString()+""+tv_date_out.getText().toString()+""+tv_empr_id.getText().toString()+""+tv_time_in.getText().toString()+""+tv_time_out.getText().toString()+""+tv_task.getText().toString()+""+tv_task_desc.getText().toString();
Intent in=new Intent(Emp_Statistics.this,Emp_Stat_Details.class);
in.putExtra("DATE_IN",tv_date_in.getText().toString());
in.putExtra("DATE_OUT",tv_date_out.getText().toString());
in.putExtra("TIME_IN",tv_time_in.getText().toString());
in.putExtra("TIME_OUT",tv_time_out.getText().toString());
in.putExtra("TASK_NAME",tv_task.getText().toString());
in.putExtra("TASK_DESC",tv_task_desc.getText().toString());
startActivity(in);
// Toast.makeText(Emp_Statistics.this, ""+info, Toast.LENGTH_SHORT).show();
}
});
}
private void sendRequest()
{
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hidePDialog();
showJSON(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Emp_Statistics.this,error.getMessage(),Toast.LENGTH_LONG).show();
hidePDialog();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String,String> map = new HashMap<String,String>();
map.put("empr_id",empr_id);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(Emp_Statistics.this);
requestQueue.add(stringRequest);
}
private void showJSON(String json)
{
ParseJSONStatistics pj = new ParseJSONStatistics(json);
pj.parseJSONA();
cl = new CustomListStatistics(Emp_Statistics.this,ParseJSONStatistics.ids,ParseJSONStatis tics.Empr_name,ParseJSONStatistics.DateIn,ParseJSONStatistics.DateOut,ParseJSONS tatistics.TimeIn,ParseJSONStatistics.TimeOut,ParseJSONStatistics.TaskName,ParseJ SONStatistics.TaskDesc);
//listView.setEmptyView();
if(cl.getCount()!=0)
{
listView.setAdapter(cl);
listView.setTextFilterEnabled(true);
}
else
{
//Toast.makeText(Emp_My_Companies.this, "No Items Available",Toast.LENGTH_SHORT).show();
listView.setVisibility(View.INVISIBLE);
tv_empty.setVisibility(View.VISIBLE);
}
}
@Override
public void onDestroy()
{
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
CustomListStatistics:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.wajid.virtualemployee.R;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.Locale;
public class CustomListStatistics extends ArrayAdapter<String> {
private String[] ids;
private String[] Empr_Name;
private String[] DateIn;
private String[] DateOut;
private String[] TimeIn;
private String[] TimeOut;
private String[] TaskName;
private String[] TaskDesc;
private Activity context;
public CustomListStatistics(Activity context, String[] ids,String[] Empr_Name,String[] DateIn,String[] DateOut,String[] TimeIn,String[] TimeOut,String[] TaskName,String[] TaskDesc) {
super(context, R.layout.emp_statistics,ids);
this.context = context;
this.ids = ids;
this.DateIn = DateIn;
this.DateOut = DateOut;
this.TimeIn = TimeIn;
this.TimeOut = TimeOut;
this.TaskName = TaskName;
this.TaskDesc = TaskDesc;
this.Empr_Name = Empr_Name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.emp_statistics, null, true);
TextView textid = (TextView) listViewItem.findViewById(R.id.id);
TextView txtEmprName = (TextView) listViewItem.findViewById(R.id.empr_name);
TextView txtDateIn=(TextView)listViewItem.findViewById(R.id.date_in);
TextView txtDateOut = (TextView) listViewItem.findViewById(R.id.date_out);
TextView txtTimeIn = (TextView) listViewItem.findViewById(R.id.time_in);
TextView txtTimeOut=(TextView)listViewItem.findViewById(R.id.time_out);
TextView txtTask = (TextView) listViewItem.findViewById(R.id.task_name);
TextView txtTaskDesc = (TextView) listViewItem.findViewById(R.id.task_desc);
textid.setText(ids[position]);
txtEmprName.setText(Empr_Name[position]);
txtDateIn.setText(DateIn[position]);
txtDateOut.setText(DateOut[position]);
txtTimeIn.setText(TimeIn[position]);
txtTimeOut.setText(TimeOut[position]);
txtTask.setText(TaskName[position]);
txtTaskDesc.setText(TaskDesc[position]);
return listViewItem;
}
}
ParseJSONStatistics:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ParseJSONStatistics {
public static String[] ids;
public static String[] Empr_name;
public static String[] DateIn;
public static String[] DateOut;
public static String[] TimeIn;
public static String[] TimeOut;
public static String[] TaskName;
public static String[] TaskDesc;
public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_EMPR_NAME = "empr_id";
public static final String KEY_DATEIN = "date_in";
public static final String KEY_DATEOUT = "date_out";
public static final String KEY_TIMEIN = "time_in";
public static final String KEY_TIMEOUT = "time_out";
public static final String KEY_TASK = "task";
public static final String KEY_TASKDESC = "task_desc";
private JSONArray users = null;
private String json;
public ParseJSONStatistics(String json){
this.json = json;
}
public void parseJSONA(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
Empr_name = new String[users.length()];
DateIn = new String[users.length()];
DateOut = new String[users.length()];
TimeIn = new String[users.length()];
TimeOut = new String[users.length()];
TaskName = new String[users.length()];
TaskDesc = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
Empr_name[i] = jo.getString(KEY_EMPR_NAME);
DateIn[i] = jo.getString(KEY_DATEIN);
DateOut[i] = jo.getString(KEY_DATEOUT);
TimeIn[i] = jo.getString(KEY_TIMEIN);
TimeOut[i] = jo.getString(KEY_TIMEOUT);
TaskName[i] = jo.getString(KEY_TASK);
TaskDesc[i] = jo.getString(KEY_TASKDESC);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
在适配器中使用Filterable接口的概念。之后,您将能够搜索listview的功能
答案 1 :(得分:0)
您必须使用baseadapter
扩展适配器以实现可过滤选项。