Android的集ArrayList的数据,自定义适配器 [英] Android set Arraylist data to custom adapter

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

问题描述

我有一个的ArrayList<串GT; statusListTextOnly; 包含一些鸣叫。
现在,如果我用它像这样

  i.setAdapter(新ArrayAdapter<串GT;(MainActivity.this,
            android.R.layout.simple_list_item_1,statusListTextOnly));

一切都很好,在活动开始和鸣叫显示列表视图。

但是我想深入我的应用程序,我想使用自定义的适配器。

所以,我所做的:

 分享Tweet []鸣叫;
TweetAdapter适配器;    鸣叫weather_data [] =新的鸣叫[] {
        新建推(statusListTextOnly)
    };    适配器=新TweetAdapter(MainAcitivity.this,
            R.layout.listview_item_row,weather_data);    i.setAdapter(适配器);

我的分享Tweet类:

 公共类分享Tweet {
     公共字符串称号;    公开资料Tweet(){
        超();
    }    公开资料Tweet(ArrayList的<串GT;标题){
        超();
        对于(一个String:标题)
        this.title =秒;
    }
}

我的TweetAdapter类:

 公共类TweetAdapter扩展ArrayAdapter<&分享Tweet GT; {    上下文语境;
    INT layoutResourceId;
    鸣叫数据[] = NULL;    公共TweetAdapter(上下文的背景下,INT layoutResourceId,鸣叫[]数据){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看排= convertView;
        TweetHolder支架=无效;        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);            持有人=新TweetHolder();
            holder.txtTitle =(TextView中)row.findViewById(R.id.textView1);            row.setTag(保持器);
        }
        其他
        {
            支架=(TweetHolder)row.getTag();
        }        唧啾=数据[位置]
        holder.txtTitle.setText(tweet.title);        返回行;
    }    静态类TweetHolder
    {
        TextView的txtTitle;
    }
}

现在,当我运行它,只有一个鸣叫显示出来(最后的鸣叫)。这是不错,但列表视图只有一个项目,只有一个鸣叫显示出来而不是整个鸣叫名单。

statusListTextOnly 包含以下内容:

  1,
2,
3。
5,
6,
7

这只能说明 7 列表视图


解决方案

 公开资料Tweet(ArrayList的<串GT;标题){
    超();
    对于(一个String:标题)
    this.title =秒;
}

每当你重写你的title值,所以你看到的只是最后一个值从ArrayList的标题。在此构造改变你的逻辑,以某种这样的:

 公开资料Tweet(字符串名称){
    this.title =称号;
}

然后执行:

 列表<&分享Tweet GT;鸣叫=新ArratList<&分享Tweet GT;();
对于(一个String:statusListTextOnly)
    tweets.add(新建推(S))

和将鸣叫您ArrayAdapter,不要忘了改分享Tweet [],在适配器列表)
祝你好运!

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.

So I did:

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);

My Tweet clasS:

public class Tweet {
     public String title;

    public Tweet(){
        super();
    }

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

My TweetAdapter class:

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;
    }
}

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 contain:

1,
2,
3.
5,
6,
7

it only shows 7 in the listview.

解决方案

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

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;
} 

And then do:

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆