【问题标题】:Now i want to display the specific data form that listview into another listview using the ArrayAdapter现在我想使用 ArrayAdapter 将列表视图的特定数据表单显示到另一个列表视图中
【发布时间】: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


    【解决方案1】:

    这是我的代码

    public 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();
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 在 testUserName(username[i]) 中显示错误;说方法调用预期
    猜你喜欢
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多