我一直在编写一些教程来制作twitter
应用,并且遇到了有关列表中自定义布局的问题。更具体地说,列表没有更新,保留在图像中看到的先前版本(在此版本中输入了空白字符串数组而不是Tweet
pojos数组):
我遇到问题的教程的这个特定部分的链接是here。 (我遇到问题的步骤是第3步)。
TweetListActivity.java:
public class TweetListActivity extends ListActivity {
private TweetAdapter tweetItemArrayAdapter;
private 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);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, TweetDetailActivity.class);
startActivity(intent);
}
TweetAdapter.java:
public class TweetAdapter extends ArrayAdapter<Tweet>{
private LayoutInflater inflater;
public TweetAdapter(Activity activity, List<Tweet> tweets){
super(activity, R.layout.row_tweet, tweets);
inflater = activity.getWindow().getLayoutInflater();
}
@SuppressLint("ViewHolder") @Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.row_tweet, parent, false);
}
activity_tweet.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.codelearn.twitter.TweetListActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
row_tweet.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/user_profile"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="left" >
<TextView
android:id="@+id/tweetTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="19sp"
android:textColor="#222222"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tweet Body Text Here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:lines="3"
android:textColor="#666666"
android:textSize="14sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20 Nov 2013"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginTop="5dp"
android:textColor="#999999"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
感谢您的帮助。
答案 0 :(得分:0)
你可以这样做。 请看一下这个http://theopentutorials.com/post/uncategorized/android-custom-listview-with-image-and-text-using-arrayadapter/
List<Tweet> _tweets;
Activity _activity;
public TweetAdapter(Activity activity, List<Tweet> tweets){
super(activity, R.layout.row_tweet, tweets);
inflater = activity.getWindow().getLayoutInflater();
this._tweets = tweets;
this._activity = activity;
}
public class ViewHolder {
TextView tvTitle = null;
TextView tvTweetBody = null;
}
在你的getView()中。 你需要添加这个:
@Override
public View getView(final int position, View convertView, ViewGroup parent){
ViewHolder _viewHolder = new ViewHolder();
Tweet _bean = _tweets.get(position);
if (convertView == null) {
LayoutInflater _layInflater = (LayoutInflater) _activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = _layInflater.inflate(R.layout.row_tweet, null);
// Lookup view for data population
_viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tweetTitle);
_viewHolder.tvTweetBody = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(_viewHolder);
} else {
_viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
tvTitle.setText(_bean.getTitle());
tvTweetBody .setText(_bean.getBody());
return convertView;
}
答案 1 :(得分:0)
如果以下是getView()
方法中的所有代码,则表示您没有填写此处的文字。
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.row_tweet, parent, false);
}
我以更简单的方式做到这一点,如下所示:
public class TweetAdapter extends ArrayAdapter<Tweet>{
private LayoutInflater inflater;
private List<Tweet> tweets;
public TweetAdapter(Activity activity, List<Tweet> tweets){
super(activity, R.layout.row_tweet, tweets);
inflater = activity.getWindow().getLayoutInflater();
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View tweet = inflater.inflate(R.layout.row_tweet, null);
TextView tweetTitle = tweet.findViewById(R.id.tweetTitle);
tweetTitle.setText(tweets.get(position).getTitle());
// And so on
return tweet;
}
}