数组列表和自定义适配器

时间:2015-10-07 05:30:36

标签: android listview twitter arraylist android-arrayadapter

我创建了一个数组列表:

public class TweetListActivity extends ListActivity {

    private ListView tweetListView;
    private ArrayAdapter tweetItemArrayAdapter;
    private final List<Tweet> tweets = new ArrayList<Tweet>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet_list);
        for (int i = 0; i < 20; i++)

        {
            Tweet tweet = new Tweet();
            tweet.setTitle("A nice header for Tweet # " + i);
            tweet.setBody("Some random body text for the tweet # " + i);
            tweets.add(tweet);
        }
        tweetItemArrayAdapter = new TweetAdapter(this,tweets);

        setListAdapter(tweetItemArrayAdapter);
    }
}

为该列表创建的自定义适配器为:

public class TweetAdapter extends ArrayAdapter<Tweet> 

{

 private List<Tweet> tweetslocal;
    private LayoutInflater inflater;

    public TweetAdapter(Activity activity, List<Tweet> tweets) {
        super(activity, R.layout.row_tweet, tweets);
        inflater = activity.getWindow().getLayoutInflater();
        tweetslocal = tweets;
    }

    @Override
    public Tweet getItem(int arg0) {
        return tweetslocal.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = inflater.inflate(R.layout.row_tweet, parent, false);
        TextView tweetTitle = (TextView) row.findViewById(R.id.tweetTitle);
        TextView tweetBody = (TextView) row.findViewById(R.id.tweetBody);
        Tweet tweet = tweetslocal.get(position);
        tweetTitle.setText(tweet.getTitle());
        tweetBody.setText(tweet.getBody());
        return row;
    }
}

但我无法获得所需的输出。所需的输出是一个推文列表,标题为“推文1,2,3等的好标题”。但我得到的输出是“19,19,19等推文的好标题”。任何人都可以建议我在哪里犯错误吗?

2 个答案:

答案 0 :(得分:0)

试试这个

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = inflater.inflate(R.layout.row_tweet, parent, false);
    TextView tweetTitle = (TextView) row.findViewById(R.id.tweetTitle);
    TextView tweetBody = (TextView) row.findViewById(R.id.tweetBody);
    tweetTitle.setText(tweetslocal.get(position).getTitle());
    tweetBody.setText(tweetslocal.get(position).getBody());
    return row;
}

答案 1 :(得分:0)

解决

实施

import java.io.Serializable;

Tweet.java中的