如何将一些已解析的JSON对象添加到自定义列表视图中?

时间:2014-08-31 12:27:56

标签: android json listview android-listview custom-adapter

好吧,我不知道标题是否清晰,所以现在我会更好地解释一切。

我已经解析了一些JSON对象。解析后,我需要将它们显示到自定义列表视图中。 JSON对象被正确解析为字符串,因为我首先将它们显示为常见的textview(仅用于测试)。 自定义列表视图也正常工作,因为我首先添加了一些值"手动" (再次,只是为了测试它)。

这是我的问题: 现在我想将JSON对象(解析为字符串)添加到我的自定义列表视图中。我已经尝试了每一个提示和技巧"我知道,但没有成功。经过两天的工作,我决定问你。

在发布代码之前:使用this library进行解析JSON对象的http请求。

此处代码
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);
    }
}

我希望我已经清楚了。你能帮助我吗?我绝望了! :(

提前致谢

编辑:这里是我的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;
    }

}

1 个答案:

答案 0 :(得分:1)

基本上你只是先预先创建List。然后使用该数据创建一个适配器并将其设置为ListView。

目前,您只需循环访问数据而不保存数据。

它看起来像这样:

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