Android版的JSONObject来的ListView [英] Android JSONObject to ListView
问题描述
我在这里有一个问题,我完全新的这个。
I'm having a problem here, I'm totally new at this.
我尝试做的是通过一个JSONObject我的适配器和我的适配器中创建的所有行为的ListView
What i try to do is pass a JSONObject to my adapter and inside my adapter create all rows for that ListView
这是我的code:
private static final String TAG_OS = "android";
protected void onPostExecute(JSONObject json) {
try {
JSONArray jArray = json.getJSONArray(TAG_OS);
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.get(i).toString());
}
}
menu_list=(ListView)findViewById(R.id.menu_list);
MenuAdapter adapter = new MenuAdapter(MainActivity.this, new ArrayList[] {listdata} );
menu_list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
我的适配器,这是其一:
My adapter is this one:
public class MenuAdapter extends ArrayAdapter<ArrayList> {
private final Context context;
private final ArrayList[] values;
public MenuAdapter(Context context,ArrayList[] values) {
super(context, R.layout.menu_main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.menu_main, parent, false);
if (position % 2 == 1) {
rowView.setBackgroundColor(Color.RED);
} else {
rowView.setBackgroundColor(Color.BLUE);
}
return rowView;
}
我仍然不知道如何填充整个ListView控件,它只是一个TextView中使用替代的背景。我不明白却又是如何设置一排层出不穷,还没有找到一个很好的例子。
I still have no idea how to populate the whole ListView, Its just one TextView with alternate backgrounds. What i don't understand yet is how to set one row after another, haven't found a good example.
我怎样才能解决这个问题? :■
How can I resolve this? :s
推荐答案
当你在你的code。使用只是一个单一的文本字段,你不需要做一个自定义的适配器只是,当你可以使用Android的 ArrayAdapter
。
As you are using just a single text field in your code you need not make a custom Adapter just for that as you can use Android's ArrayAdapter
.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listdata.toArray());
menu_list.setAdapter(adapter);
本段使用默认的Android清单布局,这将需要添加多个行对你照顾一个TextView。
This snippet uses default Android List layout and a TextView which will take care of adding multiple rows for you.
编辑:
它看起来像你需要使用使用BaseAdapter为你需要做一个扩展BaseAdapter类,并使用ViewHolder,看看这个博客我写的 androidadapternotifiydatasetchanged.blogspot.in ,它具有延伸BaseAdapter类,它使用自定义布局(的.xml)文件时,你会得到一个基本的想法如何做
It looks like you need to use to use BaseAdapter for that you would need to make a class that extends BaseAdapter and use a ViewHolder, look at this blog which I wrote androidadapternotifiydatasetchanged.blogspot.in, it has a class which extends BaseAdapter and it uses a custom layout (.xml) file, you will get a basic idea of how its done
这篇关于Android版的JSONObject来的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!