将多个数据填充到android listview中
我正在尝试学习将数据从服务器填充到Listview 我不熟悉安卓系统 MainActivity.java将多个数据填充到android listview中,android,Android,我正在尝试学习将数据从服务器填充到Listview 我不熟悉安卓系统 MainActivity.java public class MainActivity extends Activity { // url to make request private static String url = "http://URL:7002/"; private static String url1 = "http://URL:7002/XXX"; //private H
public class MainActivity extends Activity {
// url to make request
private static String url = "http://URL:7002/";
private static String url1 = "http://URL:7002/XXX";
//private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();
List<Item> yourData = new ArrayList<Item>();
List<Item> yourData1 = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiating ProgressDialog with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONObjParser jParser = new JSONObjParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
// getting JSON string from URL
JSONArray json1 = jParser.getJSONFromUrl(url1);
try {
for (int i = 0; i < json1.length(); i++) {
JSONObject c = json1.getJSONObject(i);
// Storing each json item in variable
int id = c.getInt("_id");
String TIME = c.getString("RestaurantTime");
yourData1.add(new Item(TIME));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
// Storing each json item in variable
String NAME=c.getString("restaurantNAME");
yourData.add(new Item(NAME));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
//TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
// remaining code
public class ListAdapter extends ArrayAdapter<Item> {
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView tt = null;
TextView time=null;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
tt = (TextView) v.findViewById(R.id.RestaurantNameID);
time = (TextView) v.findViewById(R.id.RestaurantTimeID);
}
Item p = items.get(position);
if (p != null) {
if (tt != null) {
tt.setText(""+p.getName());
}
if (time != null) {
time.setText(""+p.getTime());
}
}
return v;
}
}
public class Item{
private String Name;
private String Time;
public Item(String name){
this.Name = name;
}
public String getName(){
return Name;
}
public void Time(String time){
this.Time = time;
}
public String getTime(){
return Time;
}
}
- 我如何解决这个问题
- 我已经在上面发布了必修课
- 使用列表填充数据是我所看到的
谢谢。您将时间设置为您的数据1并将姓名设置为您的数据。yourdata1的对象只包含时间,并且名称为空。yourdata的对象只包含名称,时间为空 请在一个对象中设置名称和时间,并将其存储在一个列表中,该列表将传递给ListAdapter 我希望你能理解我
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONObjParser jParser = new JSONObjParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
// getting JSON string from URL
JSONArray json1 = jParser.getJSONFromUrl(url1);
// assume json's length is the same as json1's
try {
for (int i = 0; i < json1.length(); i++) {
JSONObject c = json1.getJSONObject(i);
Item item = new Item();
// Storing each json item in variable
int id = c.getInt("_id");
String TIME = c.getString("RestaurantTime");
item.setTime(TIME);
c = json.getJSONObject(i);
// Storing each json item in variable
String NAME=c.getString("restaurantNAME");
item.setName(NAME);
yourData.add(item);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
但为什么要使用两种不同的json解析方法并生成两个arraylist??时间和名称都是不同的url???您只需为商店名称和时间创建一个arraylist…是的,两者都是不同的url。。。。。我成功地实现了普通活动(使用HashMap)。。。。但在实现ListView的相同功能时遇到问题….:(你从解析中得到正确的数据了吗?如果我想在两个单独的URL中使用两个单独的表,我不能使用arrayList吗?我需要使用其他集合吗?你能修改你答案中的代码吗?关于如何在一个对象中存储名称和时间&如何在一个列表中存储。因为时间和名称的数据来自两个单独的URL的链接D。)翁夏斌…………你的解决方案很有魅力……帮助我提高了我的学习:)你的解决方案很完美
public class Item{
private String Name;
private String Time;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
Time = time;
}
}