【发布时间】:2019-02-14 01:44:10
【问题描述】:
String url = "http://example.com/bilal/fetchparty.php";
StringRequest sr = new StringRequest(1, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jo = new JSONObject(response);
JSONArray ja = jo.getJSONArray("data");
cid = new String[ja.length()];
cname = new String[ja.length()];
username = new String[ja.length()];
name1 = new String[ja.length()]; //for size
timetaken = new String[ja.length()];
link = new String[ja.length()];
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
for (int i = 0; i < ja.length(); i++) {
JSONObject job = ja.getJSONObject(i);
cid[i] = job.getString("cid");
cname[i] = job.getString("cname");
username[i] = job.getString("username");
timetaken[i] = job.getString("timetaken");
link[i] = job.getString("link");
if (cid[i].equals(shared1.sp1.getString("pid", null)) && cname[i].equals(shared1.sp1.getString("title", null))) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("cid", cid[i]);
hashMap.put("cname", cname[i]);
hashMap.put("username", username[i]);
hashMap.put("timetaken", timetaken[i]);
hashMap.put("link", link[i]);
arrayList.add(hashMap);
Toast.makeText(Vote.this, username[i], Toast.LENGTH_SHORT).show();
nameCall (username);
}
}
String[] from = {"username", "timetaken", "link"};
int[] to = {R.id.tvUsername, R.id.tvTime, R.id.tvLink};
SimpleAdapter sa = new SimpleAdapter(Vote.this, arrayList, R.layout.vote, from, to);
lvVote.setAdapter(sa);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(Vote.this);
requestQueue.add(sr);
lvVote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
bi = link[position];
Toast.makeText(Vote.this, bi, Toast.LENGTH_SHORT).show();
}
});
}
private void nameCall(final String name[]) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, name);
lvVoteNow.setAdapter(adapter);
bVoteSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int p;
String s;
p = lvVoteNow.getCheckedItemPosition();
if (p != ListView.INVALID_POSITION) {
s = name[p];
Toast.makeText(Vote.this, "You voted for " + s, Toast.LENGTH_SHORT).show();
//use s here
s = null;
} else {
Toast.makeText(Vote.this, "Please Give Your Vote", Toast.LENGTH_SHORT).show();
}
}
});
}
我从在线服务器获取数据并使用 simpleAdapter 将数据显示到 ListView 中。现在我想使用 ArrayAdapter 将 Listview 的特定数据表单显示到另一个 Listview 中 但它不根据第二个 ListView 中的 if 条件显示整个列表,即 lvVoteNow
【问题讨论】:
标签: android json listview adapter android-arrayadapter