0

Well, I don't know if the title is clear, so now I'll explain everything better.

I have parsed some JSON objects. After parsing, I need to show them into a custom listview. The JSON objects are correctly parsed into string, because I have first show them into common textviews (just for a test). Also the custom listview is working, because I have first added some values "manually" (again, just for testing it).

Here my problem: Now I want to add the JSON objects (parsed into string) into my custom listview. I've tried every "tip and trick" I know, unsuccessfully. After two days of working, I've decided to ask you.

Before posting the code: the http request for parsing JSON objects is made with this library.

Here the code
Getprofiledata.java

public class Getprofiledata extends ActionBarActivity {

    String num;
    Boolean ok=true;
    int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data);

        String url = "URL FOR JSON OBJECTS";
        ListView listadata = (ListView) findViewById(R.id.listadata);
        final List<Data> datalist = new LinkedList <Data>();

        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
               ok=true;
               i=1;
               num = Integer.toString(i);
               while(ok==true){
                       try {

                        JSONObject jsonObject = new JSONObject(response);
                        JSONObject object = jsonObject.getJSONObject(num);

                        /*********
                         * THESE ARE THE STRINGS I NEED TO ADD TO MY CUSTOM LISTVIEW
                         *********/
                        String record1 = object.getString("first");
                        String record2 = object.getString("second");
                        String record3 = object.getString("third");
                        String record4 = object.getString("fourth");
                        String record5 = object.getString("fiveth");

                        i++;
                        num = Integer.toString(i);

                       } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        ok=false;
                        }
               }
            }
            @Override
            public void onFailure(Throwable e,String response) {

               Log.d("AREQ","http GET request failed");
            }
         });


        /*********
         * HERE WE CAN ADD VALUES TO MY CUSTOM LISTVIEW
         * HOW CAN I PASS THE PREVIOUS STRING IN THIS STATEMENT?
         * 
         * THIS IS THE METHOD:
         * datalist.add(new Data(record1,record2,record3,record4,record5));
         *********/


        //HERE THE ADAPTER FOR MY CUSTOM LISTVIEW
        Getprofiledata_customadapter adapter = new Getprofiledata_customadapter(this, R.layout.data_riga, datalist);
            listadata.setAdapter(adapter);
    }
}

I hope I've been clear. Can you help me? I'm desperate! :(

Thanks in advance

Edit: here my Getprofiledata_customadapter.java

public class Getprofiledata_customadapter extends ArrayAdapter<Data>{

    public Getprofiledata_customadapter(Context context, int textViewResourceId,
            List <Data> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext()
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.data_riga, null);
        TextView firstrecord = (TextView)convertView.findViewById(R.id.tv1);
        TextView secondrecord = (TextView)convertView.findViewById(R.id.tv2);
        TextView thirdrecord = (TextView)convertView.findViewById(R.id.tv3);
        TextView forthrecord = (TextView)convertView.findViewById(R.id.tv4);
        TextView fivethrecord = (TextView)convertView.findViewById(R.id.tv5);

        Data c = getItem(position);
        firstrecord.setText(c.getRecord1());
        secondrecord.setText(c.getRecord2());
        thirdrecord.setText(c.getRecord3());
        forthrecord.setText(c.getRecord4());
        fivethrecord.setText(c.getRecord5());

        return convertView;
    }

}
2
  • Every thing is ok in your code, Just uncomment datalist.add(new ...) and then inside adapter use datalist for adding values to list item Commented Aug 31, 2014 at 12:39
  • I can't do it, the scope of the strings is not extended to the statement of "datalist.add(new ...)" Commented Aug 31, 2014 at 19:18

1 Answer 1

1

Basically you just pre-create the List first. Then with that data create an adapter and set it to your ListView.

At the moment you just loop through the data without saving it at all.

It would look something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);
    mListView = (ListView) findViewById(R.id.listadata);

    String url = "URL FOR JSON OBJECTS";
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(url,new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            ok=true;
            i=1;
            num = Integer.toString(i);
            while(ok==true){
                try {

                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject object = jsonObject.getJSONObject(num);

                    /*********
                     * THESE ARE THE STRINGS I NEED TO ADD TO MY CUSTOM LISTVIEW
                     *********/
                    String record1 = object.getString("first");
                    String record2 = object.getString("second");
                    String record3 = object.getString("third");
                    String record4 = object.getString("fourth");
                    String record5 = object.getString("fiveth");

                    // Save strings to your list
                    mData.add(new Data(record1,record2,record3,record4,record5));

                    i++;
                    num = Integer.toString(i);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    ok=false;
                }
            }
            // When the data loop is over create the adapter and set it to your ListView
            Getprofiledata_customadapter adapter = new Getprofiledata_customadapter(this, 
                    R.layout.data_riga, mData);
            mListView.setAdapter(adapter);
        }
        @Override
        public void onFailure(Throwable e,String response) {

            Log.d("AREQ","http GET request failed");
        }
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Kurtis92 after the while loop, adapter is created and set to the list.
Yes, sorry, I didn't noticed it. Anyway, we can't declare the adapter there, because then Eclipse asks me to change the constructor of my Getprofiledata_customadapter, and...well, I can't change it...
I have edited the first post with the Getprofiledata_customadapter.java
@Kurtis92 in my code replace this (when declaring the adapter) with YourActivityName.this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.