Android set Arraylist data to custom adapter

怎甘沉沦 提交于 2020-01-02 20:47:15

问题


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.


回答1:


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 !




回答2:


The problem comes from this line in Tweet class :

for(String s : title)
    this.title = s;

What do you wanna do exactly here ? I can't say, but you take the last title in the list.



来源:https://stackoverflow.com/questions/20162764/android-set-arraylist-data-to-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!