0

I have an asynctask the places data into an a listView. It only places the last input into the listView. I am not sure what I am suppose to do to place the 3 strings into the listView item. I know I am doing this wrong but this is my current code,

    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 Answers2

0

you have to customize your adapter, and inflate a view from a xml file which includes three textView. check the android API sample code. search "extends Adapter", or the method public abstract View getView (int position, View convertView, ViewGroup parent), in which you can set your three strings. check the Android reference http://developer.android.com/reference/android/widget/Adapter.html

yushulx
  • 11,695
  • 8
  • 37
  • 64
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));

is not correct.

Create the ArrayAdapter once and use ArrayAdapter.add(T Object) to add data. You will need to extend ArrayAdapter and override ArrayAdapter.getView(int position, View convertView, ViewGroup parent) to use a custom item view.

EDIT:

Have look at this How to use ArrayAdapter<myClass>.

Community
  • 1
  • 1
techi.services
  • 8,473
  • 4
  • 39
  • 42