java 安卓:如何从自定义列表适配器读取Jsonobject
我有一个JsonObject,我正试图将它传递给我的CustomListView,我在这里读过类似的其他线程,但没有任何效果。我有一个简单的JsonObject,它生成这个输出
{“localstreams”:[{“people”:“John”,“post”:“我的第一篇文章”}]}
然后我将其传递给我的listview设置适配器。问题是,我完全没有收到任何错误,活动显示为空白事件,尽管我可以看到它正在形成JsonObject。我的代码很容易复制,就是这样
LocalFeed。Java
public class LocalFeed extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.local_feed);
try {
ListView lv=(ListView)findViewById(R.id.localfeed);
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("post", "My first post");
item.put("people", "John");
array.put(item);
json.put("localstreams", array);
DemoLocalFeed DMF=new DemoLocalFeed(json,this);
lv.setAdapter(DMF);
} catch (Exception e) {
System.err.println("failed: "+ e.getMessage());
e.printStackTrace();
}
}
}
与此相关的活动是本地订阅源。xml
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.exoler.LocalFeed"
安卓:id="@+id/LocalFeed">
<ListView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/localfeed"
安卓:layout_centerHorizontal="true" />
这就是我的自定义列表适配器所在的位置 DemoLocalFeed。java
public class DemoLocalFeed extends BaseAdapter {
JSONObject names;
Context ctx;
LayoutInflater myiflater;
public DemoLocalFeed(JSONObject arr,Context c)
{
ctx=c;
names=arr;
myiflater= (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return names.length();
}
@Override
public Object getItem(int position) {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
try {
convertView = myiflater.inflate(R.layout.customadapter, null);
TextView posts = (TextView) convertView.findViewById(R.id.posts);
TextView people= (TextView) convertView.findViewById(R.id.people);
JSONArray jArray = new JSONArray("localstreams");
String postss="";
String peoples="";
for(int i=0; i < jArray.length(); i++) {
postss= names.getString("post");
peoples= names.getString("people");
}
posts.setText(postss);
people.setText(peoples);
return convertView;
}
catch (Exception e) {
e.printStackTrace();
}
}
return convertView;
}
}
而且它是活动自定义适配器。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textColor="#000099"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:id="@+id/posts" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textColor="#000099"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:id="@+id/people" />
</LinearLayout>
如前所述,这并没有给我一个错误,它只是在ListView中显示为空。相反,如果我将JsonObject更改为字符串[],则ListView将填充。我在这里读过关于这个主题的其他帖子,比如Android JSONObject to ListView,但无法让它工作。我确实认为我很接近,但似乎无法找出什么是不起作用的。任何建议都是很好的
# 1 楼答案
试试这个: