数据设置为自定义适配器

数据设置为自定义适配器

本文介绍了Android 将 Arraylist 数据设置为自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayListstatusListTextOnly; 包含一些推文.现在如果我像这样使用它

I have an ArrayList<String> statusListTextOnly; that contains some tweets.Now if I use it like this

i.setAdapter(new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, statusListTextOnly));

一切正常,活动开始,推文显示在列表视图中.

Everything is fine, the activity starts and the tweets show in listview.

但是我想深入了解我的应用,我想使用自定义适配器.

But I wanted to go deep in my app, I want to use custom adapter.

所以我做到了:

Tweet[] tweets;
TweetAdapter adapter;

    Tweet weather_data[] = new Tweet[] {
        new Tweet(statusListTextOnly)
    };

    adapter = new TweetAdapter(MainAcitivity.this,
            R.layout.listview_item_row, weather_data);

    i.setAdapter(adapter);

我的推文类别:

public class Tweet {
     public String title;

    public Tweet(){
        super();
    }

    public Tweet(ArrayList<String> title) {
        super();
        for(String s : title)
        this.title = s;
    }
}

我的 TweetAdapter 类:

public class TweetAdapter extends ArrayAdapter<Tweet>{

    Context context;
    int layoutResourceId;
    Tweet data[] = null;

    public TweetAdapter(Context context, int layoutResourceId, Tweet[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        TweetHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new TweetHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.textView1);

            row.setTag(holder);
        }
        else
        {
            holder = (TweetHolder)row.getTag();
        }

        Tweet tweet = data[position];
        holder.txtTitle.setText(tweet.title);

        return row;
    }

    static class TweetHolder
    {
        TextView txtTitle;
    }
}

现在当我运行它时,只显示一条推文(最后一条推文).很好,但是 listview 只有一项,只有一条推文显示而不是整个推文列表.

Now when I run it, only one tweet shows up (The last tweet). It's fine but the listview has only one item, only one tweet shows up not whole tweets list.

ex. statusListTextOnly 包含:

1,
2,
3.
5,
6,
7

它只在listview中显示7.

推荐答案

public Tweet(ArrayList<String> title) {
    super();
    for(String s : title)
    this.title = s;
}

每次覆盖标题值时,您只能看到标题 ArrayList 中的最后一个值.将此构造函数中的逻辑更改为某种形式:

Every time you override your title value, so you see only the last value from ArrayList of titles. Change your logic in this constructor to some kind of this:

public Tweet(String title) {
    this.title = title;
}

然后做:

List<Tweet> tweets = new ArratList<Tweet>();
for (String s: statusListTextOnly)
    tweets.add(new Tweet(s))

并将 tweets 放入您的 ArrayAdapter,不要忘记在适配器中将 Tweet[] 更改为 List ;)祝你好运!

and put the tweets to your ArrayAdapter, don't forget to change Tweet[] to List in adapter ;)Good luck !

这篇关于Android 将 Arraylist 数据设置为自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:16