gpt4 book ai didi

java - 将 json 解析值与用户输入的值进行匹配

转载 作者:行者123 更新时间:2023-12-01 13:06:39 24 4
gpt4 key购买 nike

我解析了 json 以适应我的 autocompleteTextView。我尝试将 json 对象解析的值与用户输入的值进行匹配。

但有些地方我失败了。请帮助我匹配这些值。

我必须将用户输入的字符串(城市)与存储在responseList中的解析后的json值相匹配。

这就是我正在尝试的方法。

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

new HttpGetTask().execute();

Button shwBtn = (Button) findViewById(R.id.showBtn);

shwBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

AutoCompleteTextView city1 = (AutoCompleteTextView) findViewById(R.id.autoCity);
EditText area1 = (EditText) findViewById(R.id.edArea);

String aCity = city1.getText().toString().trim();
String aArea = area1.getText().toString().trim();

//here i have to match acity with all the values in responseList before sending it to next activity

Intent myInt = new Intent(getBaseContext(),
Map1Activity.class);

String city = city1.getText().toString();
String area = area1.getText().toString();
myInt.putExtra("city", city);
myInt.putExtra("area", area);
startActivity(myInt);


}
});

}

private class HttpGetTask extends AsyncTask<Void, Void, String> {

String URL = "xyzz.cities.json?app_id=test";
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

@Override
protected String doInBackground(Void... params) {
// http stuff
return null;
}

@Override
protected void onPostExecute(String result) {
try {
JSONArray json = new JSONArray(result);

//getting my response(cities)
Log.v("ResponseCity", result);

final List<String> responseList = new ArrayList<String>();

for (int i = 0; i < json.length(); i++) {
final JSONObject e = json.getJSONObject(i);

String name = e.getString("name");
//Adding all values to a stringList
responseList.add(name);

ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(SearchActivity.this,
android.R.layout.simple_dropdown_item_1line,
responseList);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCity);
textView.setAdapter(adapter);

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != mClient)
mClient.close();
}

}

最佳答案

因此,我们假设您需要先匹配某个城市,然后才能使用 map :

 private final List<String> responseList;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

new HttpGetTask().execute();

Button shwBtn = (Button) findViewById(R.id.showBtn);

shwBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

AutoCompleteTextView city1 = (AutoCompleteTextView) findViewById(R.id.autoCity);
EditText area1 = (EditText) findViewById(R.id.edArea);

String aCity = city1.getText().toString().trim();
String aArea = area1.getText().toString().trim();

//here i have to match acity with all the values in responseList before sending it to next activity

for(int i=0; i<responseList.size(); i++) {

if(responseList.get(i).equals(aCity)) {
Intent myInt = new Intent(getBaseContext(),
Map1Activity.class);

String city = city1.getText().toString();
String area = area1.getText().toString();
myInt.putExtra("city", city);
myInt.putExtra("area", area);
startActivity(myInt);
}

}
}
});

}

private class HttpGetTask extends AsyncTask<Void, Void, String> {

String URL = "xyzz.cities.json?app_id=test";
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

@Override
protected String doInBackground(Void... params) {
// http stuff
return null;
}

@Override
protected void onPostExecute(String result) {
try {
JSONArray json = new JSONArray(result);

//getting my response(cities)
Log.v("ResponseCity", result);

responseList = new ArrayList<String>();

for (int i = 0; i < json.length(); i++) {
final JSONObject e = json.getJSONObject(i);

String name = e.getString("name");
//Adding all values to a stringList
responseList.add(name);

ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(SearchActivity.this,
android.R.layout.simple_dropdown_item_1line,
responseList);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCity);
textView.setAdapter(adapter);

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != mClient)
mClient.close();
}

}

如果我没有猜错你的 Intent 的话应该是这样的。

关于java - 将 json 解析值与用户输入的值进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218838/

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