我如何使用 List<data> 并将其传递给另一个 class
How can i use List<data> and pass it to another class
我在 TweetAdapter.java 中创建了一个自定义适配器。我把代码写成
public class TweetAdapter 扩展 ArrayAdapter {
private List<Tweet> tweets;
private Context context;
public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) {
super(ctx, R.layout.row_tweet, tweets);
}
@Override
public Tweet getItem(int arg0) {
return tweets.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row_tweet, parent);
}
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
TextView tweetTitle = (TextView) v.findViewById(R.id.tweetTitle);
TextView tweetBody = (TextView) v.findViewById(R.id.tweetBody);
final Tweet currentTweet = tweets.get(position);
String a = currentTweet.getTitle();
String b = currentTweet.getBody();
tweetTitle.setText(a);
tweetBody.setText(b);
return v;
}
}
但出现错误
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'
在 android.view.LayoutInflater.from(LayoutInflater.java:219)
您的 context
变量似乎为空,因为它从未被设置过。
尝试将您的构造函数修改为:
public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) {
super(ctx, R.layout.row_tweet, tweets);
context=ctx;
}
看看这个link是否对你有帮助:
这里是相关部分:
public class TweetAdapter extends ArrayAdapter<Tweet> {
private LayoutInflater inflater;
private List<Tweet> tweetsLocal;
public TweetAdapter(Activity activity, List<Tweet> tweets){
super(activity, R.layout.row_tweet, tweets);
inflater = activity.getWindow().getLayoutInflater();
tweetsLocal = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = inflater.inflate(R.layout.row_tweet, parent, false);
TextView title = (TextView) row.findViewById(R.id.tweetTitle);
Tweet tweet = tweetsLocal.get(position);
title.setText(tweet.getTitle());
TextView body = (TextView) row.findViewById(R.id.textView2);
body.setText(tweet.getBody());
return row;
}
}
已解决
已实施
进口java.io.Serializable;
在Tweet.java
我在 TweetAdapter.java 中创建了一个自定义适配器。我把代码写成
public class TweetAdapter 扩展 ArrayAdapter {
private List<Tweet> tweets;
private Context context;
public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) {
super(ctx, R.layout.row_tweet, tweets);
}
@Override
public Tweet getItem(int arg0) {
return tweets.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row_tweet, parent);
}
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
TextView tweetTitle = (TextView) v.findViewById(R.id.tweetTitle);
TextView tweetBody = (TextView) v.findViewById(R.id.tweetBody);
final Tweet currentTweet = tweets.get(position);
String a = currentTweet.getTitle();
String b = currentTweet.getBody();
tweetTitle.setText(a);
tweetBody.setText(b);
return v;
}
}
但出现错误
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'在 android.view.LayoutInflater.from(LayoutInflater.java:219)
您的 context
变量似乎为空,因为它从未被设置过。
尝试将您的构造函数修改为:
public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) {
super(ctx, R.layout.row_tweet, tweets);
context=ctx;
}
看看这个link是否对你有帮助:
这里是相关部分:
public class TweetAdapter extends ArrayAdapter<Tweet> {
private LayoutInflater inflater;
private List<Tweet> tweetsLocal;
public TweetAdapter(Activity activity, List<Tweet> tweets){
super(activity, R.layout.row_tweet, tweets);
inflater = activity.getWindow().getLayoutInflater();
tweetsLocal = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = inflater.inflate(R.layout.row_tweet, parent, false);
TextView title = (TextView) row.findViewById(R.id.tweetTitle);
Tweet tweet = tweetsLocal.get(position);
title.setText(tweet.getTitle());
TextView body = (TextView) row.findViewById(R.id.textView2);
body.setText(tweet.getBody());
return row;
}
}
已解决
已实施
进口java.io.Serializable;
在Tweet.java