gpt4 book ai didi

java - 使用自定义 ListView 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:49 24 4
gpt4 key购买 nike

我一直在学习一些教程来制作 twitter 应用程序,但遇到了有关列表中自定义布局的问题。更具体地说,该列表没有更新,保留在以前的版本上,如图所示(在此版本中输入了空白字符串数组,而不是 Tweet pojos 数组): enter image description here

我遇到问题的教程特定部分的链接是 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>

感谢您的帮助。

最佳答案

你可以做这样的事情。请看看这个 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;

}

关于java - 使用自定义 ListView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com