简体   繁体   中英

How to add some parsed JSON objects into a custom listview?

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;
    }

}

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");
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM