Java 如何使用Android将ArrayList数据加载到ListView?
我正在尝试将Java 如何使用Android将ArrayList数据加载到ListView?,java,android,json,listview,arraylist,Java,Android,Json,Listview,Arraylist,我正在尝试将JSON数据存储创建到ArrayList中,并加载到ListView。现在我已经成功地将我的JSON数据存储到ArrayList中。但问题是我必须维护多列列表视图。我需要在第一列列出我的第一个数组 下面我尝试了一些东西,多列数组。但我确实不知道该怎么做。请帮助我,我是Android的新开发者 // I need to add my array into first column private ArrayList<String> myarray = new ArrayL
JSON数据存储创建到ArrayList
中,并加载到ListView
。现在我已经成功地将我的JSON数据
存储到ArrayList
中。但问题是我必须维护多列
列表视图
。我需要在第一列列出我的第一个数组
下面我尝试了一些东西,多列数组。但我确实不知道该怎么做。请帮助我,我是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);
//我需要将数组添加到第一列中
private ArrayList myarray=new ArrayList();
//我已经加载了JSON字符串数据
添加(jsondata);
//LISTVIEW监视列表
ListView ListView=(ListView)findViewById(R.id.listView1);
列表=新的ArrayList();
HashMap temp=新的HashMap();
温度输入(第一列“Minchu”);
温度输入(第二列,“美国”);
临时投入(第三列,“城市”);
临时投入(第四列,“等级”);
列表。添加(临时);
.
.
.
.
ListViewAdapters=新的ListViewAdapters(此,列表);
setAdapter(适配器);
注意:
上面的HashMap
用于放置手动数据我需要加载第一个数组的第一列,第二个数组的第二列。您需要为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());
}
}
您可以查看链接以了解更多详细信息是否已通过适配器文档?您需要的是自定义适配器,而不是字符串适配器。为listview创建自定义适配器。创建自定义适配器和getter setter,以便在所需位置显示数据。请给我IMMIDATE解决方案。我将学习str。想要得到输出!请张贴一些样品,我很难理解!你还有其他简单的解决办法吗!只需使用setter和getter创建简单类,并使用setdata解析json。
CustomAdapter adapter = new CustomAdapter(MainActivity.this, R.id.listView1, listData);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
enter code here