我已经在相当长的一段时间内完成了应用程序,接近完成它,但我最后需要帮助。
在此应用中,用户可以输入name of client
,mobile number of client
和email id of client
所有这些信息都会存储到在线数据库中。我有一个生成JSON文件的PHP文件,我的应用程序获取此JSON文件并将其内容设置为listview。
这是我的ListActivity,它有一个负责获取JSON文件的子类
public class ListActivity extends AppCompatActivity implements MaterialSearchView.OnQueryTextListener {
String json_string;
ListDataAdapter listDataAdapter;
MaterialSearchView materialSearchView;
DataProvider dataProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar searching = (Toolbar)findViewById(R.id.searchBar);
setSupportActionBar(searching);
getSupportActionBar().setTitle("Search");
searching.setTitleTextColor(Color.parseColor("#ffffff"));
materialSearchView = (MaterialSearchView)findViewById(R.id.search_view);
final ListView listView = (ListView) findViewById(R.id.list_view);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdapter);
Log.e("data=======", String.valueOf(listDataAdapter));
new GetJSONfile().execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
DataProvider dataProvider = (DataProvider) listDataAdapter.getItem(i);
int idofclient = dataProvider.getId();
String nameOfClient = dataProvider.getName();
String mobileOfClient = dataProvider.getMob();
String emailOfClient = dataProvider.getEmail();
Intent calAct = new Intent(ListActivity.this, UserProfile.class);
Bundle bundle = new Bundle();
bundle.putInt("idofCC", (idofclient));
bundle.putString("name", nameOfClient);
bundle.putString("mobile", mobileOfClient);
bundle.putString("email", emailOfClient);
calAct.putExtras(bundle);
startActivity(calAct);
}
});
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<DataProvider> dataProvider2 = new ArrayList<>();
//dataProvider is your data which is provide to adapter in onPostExecute
// so make it as global variable so you can access it
for (DataProvider dp : listDataAdapter) {
String name = dp.getName().toLowerCase();
if (name.contains(newText)) {
dataProvider2.add(dp);
}
}
listDataAdapter.setFilter(dataProvider2);
return true;
}
public class GetJSONfile extends AsyncTask<Void, Void, String> {
String url_get;
String jsondata;
@Override
protected void onPreExecute() {
url_get = "http://10.0.2.2/webapp/jasonclient.php";
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(url_get);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((jsondata = bufferedReader.readLine())!=null){
stringBuilder.append(jsondata+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
Log.e("Tag =============", String.valueOf(stringBuilder));
return stringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String jsonResult) {
json_string = jsonResult;
try{
JSONObject jsonn = new JSONObject(json_string);
JSONArray jsonArray = jsonn.getJSONArray("clientinfo");
int count = 0;
while(count<jsonArray.length()){
JSONObject JO = jsonArray.getJSONObject(count);
int id = JO.getInt("client_id");
String name = JO.getString("client_name");
String mobile = JO.getString("client_mobile");
String email = JO.getString("client_email");
Log.e("nameeeeeeeee==========",name);
Log.e("mobileeeeeee==========",mobile);
Log.e("emaillllllll==========",email);
dataProvider = new DataProvider(id,name,mobile,email);
Log.e("tag==================", String.valueOf(dataProvider));
listDataAdapter.add(dataProvider);
count++;
}
} catch (JSONException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem menuItem = menu.findItem(R.id.ActionSearchView);
materialSearchView.setMenuItem(menuItem);
materialSearchView.setOnQueryTextListener(this);
return true;
}
}
以下是我的ListDataAdapter类
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter( Context context, int resource) {
super(context, resource);
}
static class LayoutHandler {
TextView ID,NAME,MOB,EMAIL;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView) row.findViewById(R.id.text_user_name);
row.setTag(layoutHandler);
}else{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
return row;
}
public void setFilter(ArrayList<DataProvider> dataProvider2)
{
//dataProvider is your List which is used in constructor of adapter
//class
ArrayList<Object> dataProvider = new ArrayList<>();
dataProvider.addAll(dataProvider2);
notifyDataSetChanged();
}
}
以下是我在搜索栏中开始输入某些字符时我希望该应用执行的操作示例。
这是一个很长的项目,这就是我没有包含所有源代码的原因,但如果您需要任何内容,请发表评论,我会添加它。
答案 0 :(得分:0)
你需要先设置list.setadapter(null), 然后你可以填充适配器或适配器数据集中的值,最后调用list.setadapter(adap),然后调用adap.setNotifyDataChanged() 除了“”之外,每次更改都应该这样做,你应该很好
答案 1 :(得分:0)
您可以尝试以这种方式实施搜索。我没有测试它,我希望它能工作。在适配器类中创建一个方法:
public void setFilter(ArrayList<Object> dataProvider2)
{
//list is your List which is used in constructor of adapter
//class
list = new ArrayList<>();
list.addAll(dataProvider2);
notifyDataSetChanged();
}
然后在Activity
内,我会假设您implements SearchView.OnQueryTextListener
以及override onQueryTextSubmit
和onQueryTextChange
以及onCreateOptionsMenu
内部做了类似这样的事情:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_search, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
然后在onQueryTextChange
内使用下一个代码:
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<DataProvider> dataProvider2 = new ArrayList<>();
//listDataAdapter is your data which is provide to adapter in onPostExecute
// so make it as global variable so you can access it
for (int i = 0; i < listDataAdapter.getCount(); i++) {
DataProvider dp = listDataAdapter.getItem(i);
String name = dp.getName().toLowerCase();
if (name.contains(newText)) {
dataProvider2.add(dp);
}
}
listAdapter.setFilter(dataProvider2);
return true;
}
请确保您遵循传递正确的数据,因为如果您提供空的List
,那么它不应该为空,它将无效并且数据不会被过滤。
答案 2 :(得分:-1)
您可以通过两种方式实现这一目标:
使用RecyclerView来实现这一点:
参考:https://www.learn2crack.com/2017/03/searchview-with-recyclerview.html
Activity类上的执行以下代码:
EditText editTextSearch;
editTextSearch = (EditText) findViewById(R.id.searcheditTxtTI);
editTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editTextSearch.getText().length()>=1){
adapter.getFilter().filter(editTextSearch.getText().toString());
recyclerView.setVisibility(View.VISIBLE);
}else{
recyclerView.setVisibility(View.GONE);
}
}
});
在您的适配器上执行以下代码:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = list;
} else {
ArrayList<LoadDataResult> filteredList = new ArrayList<>();
for (LoadDataResult txt : list) {
if (txt.getName().toString().toLowerCase().contains(charString)|| txt.getUpc().toString().toLowerCase().contains(charString)) {
filteredList.add(txt);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<LoadDataResult>) filterResults.values;
notifyDataSetChanged();
}
};
}