Android 现在,我想使用ArrayAdapter将listview中的特定数据表单显示到另一个listview中 stringurl=”http://example.com/bilal/fetchparty.php"; StringRequest sr=newStringRequest(1,url,new Response.Listener()){ @凌驾 公共void onResponse(字符串响应){ 试一试{ JSONObject jo=新的JSONObject(响应); JSONArray ja=jo.getJSONArray(“数据”); cid=新字符串[ja.length()]; cname=新字符串[ja.length()]; 用户名=新字符串[ja.length()]; name1=新字符串[ja.length()];//表示大小 timetake=新字符串[ja.length()]; link=新字符串[ja.length()]; ArrayList ArrayList=新的ArrayList(); 对于(int i=0;i
我从在线服务器获取数据,并使用simpleAdapter将数据显示到ListView中。现在,我想使用ArrayAdapter将Listview中的特定数据表单显示到另一个Listview中 但它显示的是整个列表,而不是第二个列表视图中的if条件,即lvVoteNow这是我的代码Android 现在,我想使用ArrayAdapter将listview中的特定数据表单显示到另一个listview中 stringurl=”http://example.com/bilal/fetchparty.php"; StringRequest sr=newStringRequest(1,url,new Response.Listener()){ @凌驾 公共void onResponse(字符串响应){ 试一试{ JSONObject jo=新的JSONObject(响应); JSONArray ja=jo.getJSONArray(“数据”); cid=新字符串[ja.length()]; cname=新字符串[ja.length()]; 用户名=新字符串[ja.length()]; name1=新字符串[ja.length()];//表示大小 timetake=新字符串[ja.length()]; link=新字符串[ja.length()]; ArrayList ArrayList=新的ArrayList(); 对于(int i=0;i,android,json,listview,adapter,android-arrayadapter,Android,Json,Listview,Adapter,Android Arrayadapter,我从在线服务器获取数据,并使用simpleAdapter将数据显示到ListView中。现在,我想使用ArrayAdapter将Listview中的特定数据表单显示到另一个Listview中 但它显示的是整个列表,而不是第二个列表视图中的if条件,即lvVoteNow这是我的代码 String url = "http://example.com/bilal/fetchparty.php"; StringRequest sr = new StringRequest(1, url, new Resp
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();
}
}
});
}
公共类投票活动{
ListView lvVote,lvVoteNow;
TextView tvUsername、tvTime、tvLink、cidVote、cnameVote;
List testUserName=new ArrayList();
按钮提交;
串bi;
字符串[]cid,cname,用户名,时间,链接,名称1,名称2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote);
lvVoteNow=findviewbyd(R.id.lvVoteNow);
bVoteSubmit=findviewbyd(R.id.bVoteSubmit);
tvUsername=findViewById(R.id.tvUsername);
tvTime=findViewById(R.id.tvTime);
tvLink=findViewById(R.id.tvLink);
cidVote=findViewById(R.id.cidVote);
cnameVote=findViewById(R.id.cnameVote);
lvVote=findViewById(R.id.lvVote);
lvVote.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
最终共享共享1=新共享(getApplicationContext());
setText(shared1.sp1.getString(“pid”,null));
cnameVote.setText(shared1.sp1.getString(“title”,null));
//正在获取用于投票的数据
字符串url=”http://example.com/bilal/fetchparty.php";
StringRequest sr=newStringRequest(1,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jo=新的JSONObject(响应);
JSONArray ja=jo.getJSONArray(“数据”);
cid=新字符串[ja.length()];
cname=新字符串[ja.length()];
用户名=新字符串[ja.length()];
name1=新字符串[ja.length()];//表示大小
timetake=新字符串[ja.length()];
link=新字符串[ja.length()];
ArrayList ArrayList=新的ArrayList();
对于(int i=0;ipublic class Vote extends AppCompatActivity {
ListView lvVote,lvVoteNow;
TextView tvUsername,tvTime,tvLink,cidVote,cnameVote;
List<String> testUserName = new ArrayList<>();
Button bVoteSubmit;
String bi;
String [] cid,cname,username,timetaken,link,name1,name2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote);
lvVoteNow = findViewById(R.id.lvVoteNow);
bVoteSubmit = findViewById(R.id.bVoteSubmit);
tvUsername = findViewById(R.id.tvUsername);
tvTime = findViewById(R.id.tvTime);
tvLink = findViewById(R.id.tvLink);
cidVote = findViewById(R.id.cidVote);
cnameVote = findViewById(R.id.cnameVote);
lvVote = findViewById(R.id.lvVote);
lvVote.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final Shared shared1 = new Shared(getApplicationContext());
cidVote.setText(shared1.sp1.getString("pid", null));
cnameVote.setText(shared1.sp1.getString("title", null));
//fetching data for vote
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();
testUserName.add(username[i]); // add userName to list
// nameCall (username);
}
}
//after all specific users add on list to this
String[] stringArray = testUserName.toArray(new String[0]);
nameCall(stringArray);
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();
}
}
});
}
}