如何将多个文本设置为listView项目 - Android

时间:2013-07-10 00:40:13

标签: android string android-listview

我有一个asynctask将数据放入listView。它只将最后一个输入放入listView。我不确定我要将3个字符串放入listView项目。我知道我做错了,但这是我现在的代码,

    class loadComments extends AsyncTask<JSONObject, String, JSONObject> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();


            } 

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

            } 

            protected JSONObject doInBackground(JSONObject... params) {


                JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                    return json2;



            }

            @Override
            protected void onPostExecute(JSONObject json2) {
                try {  
                    if (json2.getString(KEY_SUCCESS) != null) { 
                        registerErrorMsg.setText("");
                        String res2 = json2.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res2) == 1){ 


                            JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
                            String comments[] = new String[commentArray.length()];
                            for ( int i=0; i<commentArray.length(); i++ ) {
                                comments[i] = commentArray.getString(i);
                            }
                            JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
                            String numbers[] = new String[numberArray.length()];
                            for ( int i=0; i<numberArray.length(); i++ ) {
                                numberss[i] = numberArray.getString(i);
                            }
                            JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
                            String usernames[] = new String[usernameArray.length()];
                            for ( int i=0; i<usernameArray.length(); i++ ) {
                                usernames[i] = usernameArray.getString(i);
                            }



                            setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listComment, comments));
                            setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listNumber, number));
                            setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listPostedBy, usernames));







                            }//end if key is == 1
                        else{
                            // Error in registration
                            registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                        }//end else
                    }//end if
                } //end try

                catch (JSONException e) { 
                    e.printStackTrace();
                }//end catch    
            }
        }


        new loadComments().execute();

2 个答案:

答案 0 :(得分:0)

您必须自定义适配器,并从包含三个textView的xml文件中提升视图。检查android API示例代码。搜索&#34;扩展Adapter&#34;或方法 public abstract查看getView(int position,View convertView,ViewGroup parent),您可以在其中设置三个字符串。检查Android参考http://developer.android.com/reference/android/widget/Adapter.html

答案 1 :(得分:0)

setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listComment, comments));
                        setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listNumber, number));
                        setListAdapter(new ArrayAdapter<String>(DashboardActivity.this, R.layout.list_item, R.id.listPostedBy, usernames));

不正确。

创建ArrayAdapter一次并使用ArrayAdapter.add(T Object)添加数据。您需要extend ArrayAdapter并覆盖ArrayAdapter.getView(int position, View convertView, ViewGroup parent)才能使用自定义项目视图。

编辑:

请查看此How to use ArrayAdapter<myClass>