gpt4 book ai didi

php - 从服务器检索的JSON对象上的SearchView

转载 作者:行者123 更新时间:2023-11-29 14:12:36 25 4
gpt4 key购买 nike

我已经在应用程序上工作了一段时间,并且即将完成,但是我需要最后一件事的帮助。

在此应用程序中,用户可以输入name of clientmobile number of clientemail id of client,所有这些信息都存储在在线数据库中。我有一个PHP文件,该文件生成一个JSON文件,我的应用程序获取此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();
}
}


这是当我开始在搜索栏中键入一些字符时希望应用程序执行的示例。

Example Of What I want the Search Bar to do

这是一个漫长的项目,这就是为什么我没有包含所有源代码的原因,但是如果您需要任何内容​​,请发表评论,我会添加它。

最佳答案

您需要先设置list.setadapter(null),
那么您可以在适配器或适配器数据集中填充值,最后调用list.setadapter(adap),然后调用adap.setNotifyDataChanged()
对于除“”之外的每个字符更改,都应执行此操作,并且您应该很好

关于php - 从服务器检索的JSON对象上的SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48526358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com