android:我如何从自定义列表适配器中读取Jsonobject

时间:2015-07-11 23:50:15

标签: java android listview android-listview

我有一个JsonObject,我试图传递给我的CustomListView,我已经读过这里类似的其他线程,但没有任何工作。我有一个简单的JsonObject,它产生这个输出

{“localstreams”:[{“people”:“John”,“post”:“我的第一篇文章”}}}

然后我传递给我的listview SetAdapter。问题是我绝对没有错误,活动显示为空白,尽管我可以看到它正在形成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();
    }
    }
      }

与此相关的活动是local_feed.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"
     tools:context="com.exoler.LocalFeed"
        android:id="@+id/LocalFeed">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/localfeed"
            android: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;
 }
 }

,它的活动是customadapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000099"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/posts" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000099"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/people" />
</LinearLayout>

如前所述,这并没有给我一个错误,它只是在ListView中显示空白,相反如果我将 JsonObject更改为String []然后ListView被填充。我已经在这个主题上阅读了其他主题,例如Android JSONObject to ListView,但无法使其正常运行。我确实认为我很接近,但似乎无法找出什么不起作用,任何建议都会很棒..

1 个答案:

答案 0 :(得分:0)

试试这个:

convertView = myiflater.inflate(R.layout.customadapter, null);
TextView posts = (TextView) convertView.findViewById(R.id.posts);
TextView people= (TextView) convertView.findViewById(R.id.people);

JSONArray jaLocalstreams = names.getJSONArray("localstreams");
JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
String postss = jsonObject.getString("post");
String peoples = jsonObject.getString("people");

posts.setText(postss);
people.setText(peoples);