如何使用Android将ArrayList数据加载到ListView中?

时间:2015-10-28 10:04:29

标签: java android json listview arraylist

我正在尝试将JSON data store创建为ArrayList并加载ListView。现在,我已成功将JSON data存储到ArrayList。但问题是我维持multiple column listview。我需要在第一列列出我的第一个数组。

下面我尝试了一些带有数组的多列。但我完全不知道该怎么做。请帮助我,我是Android的新开发者。

// I need to add my array into first column 
private ArrayList<String> myarray = new ArrayList<String>();

//JSON string data's I have loaded
myarray.add(jsondata);

//LISTVIEW WATCHLIST
ListView listView=(ListView)findViewById(R.id.listView1);
list=new ArrayList<HashMap<String,String>>();

HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, "Minchu");
temp.put(SECOND_COLUMN, "USA");
temp.put(THIRD_COLUMN, "City");
temp.put(FOURTH_COLUMN, "Ranks");
list.add(temp);
.
.
.
.
ListViewAdapters adapter=new ListViewAdapters(this,list);
listView.setAdapter(adapter);

NOTE :高于HashMap推送手动数据。 I need to load first array first columns,第二个数组第二列。

1 个答案:

答案 0 :(得分:0)

您需要为json对象创建模型,请参阅下面的代码。

import java.io.Serializable;

public class PersonDetailsItem implements Serializable {
public int id;
private String intEducationEN, intVillageID;

public PersonDetailsItem(int id, String name, String phoneNo, String email) {
    // TODO Auto-generated constructor stub
    this.id = id;
    this.strNameEN = name;
    this.strEmailid = email;
}

public String getIntVillageID() {
    return intVillageID;
}

public void setIntVillageID(String intVillageID) {
    this.intVillageID = intVillageID;
}

public String getIntEducationEN() {
    return intEducationEN;
}

public void setIntEducationEN(String intEducationEN) {
    this.intEducationEN = intEducationEN;
}

public PersonDetailsItem() {
    // TODO Auto-generated constructor stub
}

然后设置解析json接收字符串的模型表单的值。

  private void parseJson(String rs) {
  private ArrayList<PersonDetailsItem> listData = new      ArrayList<PersonDetailsItem>();;
        // TODO Auto-generated method stub
        listData = new ArrayList<PersonDetailsItem>();
        spinerPersonData = new ArrayList<SpinerItem>();
        try {
            JSONObject obj = new JSONObject(rs);

            JSONArray jArray = obj.getJSONArray("Table");
            for (int i = 0; i < jArray.length(); i++) {

                JSONObject c = jArray.optJSONObject(i);
                String intEducationEN = c.getString("intEducationEN");
                String intVillageID = c.getString("intVillageID");

                PersonDetailsItem personItem = new PersonDetailsItem();

                personItem.setIntEducationEN(intEducationEN);

                personItem.setIntVillageID(intVillageID);

                listData.add(personItem);


            }

        } catch (JSONException e) { // TODO Auto-generated catch block
            e.printStackTrace();
            Log.v("perosnJson Error", e.toString());

        }
    }

设置为listadapter

CustomAdapter adapter = new CustomAdapter(MainActivity.this, R.id.listView1, listData);
    lv.setAdapter(adapter);
    adapter.notifyDataSetChanged();
enter code here

您可以查看链接以获取更多详细信息https://github.com/yagneshshinde101/mysamaj/blob/master/MySamaj/src/com/example/mysamajmain/MainActivity.java