我在Main活动中有一个列表视图,我已经使用数组适配器填充了其中的一些值
String[] arr = new String[]{"Android ListView Item 1", "Android ListView Item 2",
"Simple List View In Android", "List View onClick Event","Android List View OnItemClickListener",
"Open New Activity When ListView item Clicked", "List View onClick Source Code", "List View Array Adapter Item Click",
};
现在,我要将onclickListener设置为列表的每个项目。我正在请求一个给我jsonresponse的API,并且我想遍历jsonresponse并在单击列表项时将所需的数据发送到另一个活动。
我尝试了以下操作:
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(this,
R.layout.activity_main2, R.id.textview, arr);
listview.setAdapter(arrayadapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String textToPass = "hello";
if (position == 0) {
Bundle passData = new Bundle();
passData.putString("data",textToPass);
Intent myIntent = new Intent(view.getContext(), Main2Activity.class);
myIntent.putExtras(passData);
startActivity(myIntent);
}
}
});
}
private String jsonParse(){
String url = "//testing/api.php"; //get json response locally
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray = null;
try {
jsonArray = response.getJSONArray("Item");
for(int i=0; i<=jsonArray.length();i++){
HashMap<String,String> dict = new HashMap<String,String>();
JSONObject tv = jsonArray.getJSONObject(i);
String sid;
dict.put("cid", tv.getString("sid") );
// String cid = tv.getString("sid");
String categoryName =tv.getString("category_name");
String baseUrl = "//testing/";
String imageUrl = baseUrl + tv.getString("category_image");
//textView.append(cid + "," + categoryName + "," + imageUrl + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
assert jsonArray != null;
Log.d("Main",jsonArray.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(error.getMessage(), "utf-8");
}
});
requestQueue.add(jsonObjectRequest);
请帮助
答案 0 :(得分:1)
如果我理解正确,那么我认为您想将HashMap malloc
发送到另一个活动。 HashMap是可序列化的,这意味着您可以轻松地按意图传递它,而且键和值都是字符串,也可以序列化
dic
然后在您的下一个活动中使用类型转换来检索它。.简单
intent.putExtra("dic.key", dic);
答案 1 :(得分:0)
我尚未对此进行测试,但请尝试一下:
发件人活动时
HashMap<String, String> dict = new HashMap<>();
// Fill your data to dict
...
Bundle data = new Bundle();
// Save hash map size
data.putInt("size", dict.size());
// Save each entry (String, String)
int i = 0;
for (Map.Entry<String, String> entry : dict.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
data.putString("key" + i, key);
data.putString("value" + i, value);
++i;
}
// Put data to intent starting receiver activity
...
在接收方活动中
// Get bundle data from intent
Bundle data = getIntent().getExtras();
// Create a new map
HashMap<String, String> map = new HashMap<>();
// Get hash map size
int size = data.getInt("size");
// Get each entry from bundle then put to hash map
for (int i = 0; i < size; ++i){
String key = data.getString("key" + i);
String value = data.getString("value" + i);
map.put(key, value);
}