如何扩展ArrayAdapter以打印Parsed JSON?

时间:2015-06-20 08:06:38

标签: java android json

我已经尝试并阅读了很多答案,但无法弄清楚如何正确编码...

NewsAdapter.java

public class NewsAdapter extends ArrayAdapter{
    private static final String DEBUG_TAG = "NEWSADAPTER";
    private LayoutInflater inflater;

    public NewsAdapter(Activity activity, ArrayList<String> items){
        super(activity, R.layout.news_feed, items);
        Log.d(DEBUG_TAG, "This is came from the world i know of" + items);
        inflater = activity.getWindow().getLayoutInflater();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return inflater.inflate(R.layout.news_feed, parent, false);
    }
}

news_feed.xml

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

    <TextView android:id="@+id/title"
        android:text="@string/news_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="19sp" />

</LinearLayout>

以上代码的输出是......

enter image description here

如何获取项目值以及如何设置文本?

1 个答案:

答案 0 :(得分:1)

这样做:

public class NewsAdapter extends ArrayAdapter{
    private static final String DEBUG_TAG = "NEWSADAPTER";
    private LayoutInflater inflater;
    ArrayList<String> items;

    public NewsAdapter(Activity activity, ArrayList<String> items){

        inflater = activity.getWindow().getLayoutInflater();
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

       View v = convertView
        if(v == null)
            v = inflater.inflate(R.layout.news_feed, parent, false);
        TextView txtTitle = (TextView) v.findViewById(R.id.title);
        txtTitle.setText(items.get(position));
        return v;
    }
}