Java 使用自定义列表视图时出错
我一直在学习一些教程来制作一个Java 使用自定义列表视图时出错,java,android,listview,Java,Android,Listview,我一直在学习一些教程来制作一个twitter应用程序,遇到了一个关于列表中自定义布局的问题。更具体地说,列表没有更新,保留在图像中显示的先前版本上(在此版本中,输入了一个空白字符串数组,而不是Tweetpojo的数组): 我在教程中遇到问题的这一部分的链接是。(我遇到问题的步骤是步骤3) 这是我的密码 TweetListActivity.java: public class TweetListActivity extends ListActivity { private TweetAdapte
twitter
应用程序,遇到了一个关于列表中自定义布局的问题。更具体地说,列表没有更新,保留在图像中显示的先前版本上(在此版本中,输入了一个空白字符串数组,而不是Tweet
pojo的数组):
我在教程中遇到问题的这一部分的链接是。(我遇到问题的步骤是步骤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);
}
公共类TweetListActivity扩展了ListActivity{
专用TweetAdapter tweetItemArrayAdapter;
私有列表tweets=newarraylist();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u tweet\u列表);
对于(int i=0;i<20;i++){
Tweet=新Tweet();
setTitle(“tweet#“+i的一个不错的标题);
setBody(“tweet#“+i的一些随机正文文本);
添加(tweet);
}
tweetItemArrayAdapter=新的TweetAdapter(这是tweets);
setListAdapter(tweetItemArrayAdapter);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
Intent Intent=新的Intent(这个,TweetDetailActivity.class);
星触觉(意向);
}
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);
}
公共类TweetAdapter扩展了ArrayAdapter{
私人充气机;
公共TweetAdapter(活动、列表tweets){
超级(活动、R.layout.row_推文、推文);
充气器=活动。getWindow().GetLayoutFlater();
}
@SuppressLint(“视图持有者”)@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
返回充气机。充气(R.layout.row_tweet,父级,false);
}
活动_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>
<?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>
row_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>
<?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>
感谢您的帮助。您可以这样做。 请看一下这个
你可以这样做。 请看一下这个
如果以下是
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;
}
}
公共类TweetAdapter扩展了ArrayAdapter{
私人充气机;
私人列表推文;
公共TweetAdapter(活动、列表tweets){
超级(活动、R.layout.row_推文、推文);
充气器=活动。getWindow().GetLayoutFlater();
this.tweets=tweets;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
查看tweet=充气机。充气(R.layout.row_tweet,空);
TextView tweetTitle=tweet.findviewbyd(R.id.tweetTitle);
tweetTitle.setText(tweets.get(position.getTitle());
//等等
返回tweet;
}
}
如果以下是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;
}
}
公共类TweetAdapter扩展了ArrayAdapter{
私人充气机;
私人列表推文;
公共TweetAdapter(活动、列表tweets){
超级(活动、R.layout.row_推文、推文);
充气器=活动。getWindow().GetLayoutFlater();
this.tweets=tweets;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
查看tweet=充气机。充气(R.layout.row_tweet,空);
TextView tweetTitle=tweet.findviewbyd(R.id.tweetTitle);
tweetTitle.setText(tweets.get(position.getTitle());
//等等
返回tweet;
}
}
您需要在TweetAdapter.java
的getView()
中查看安装视图。请查看我的答案,如果有任何疑问,请告诉我。@ShoebSiddique谢谢。是否有一个简短的教程(或文档)可以快速总结这一点?您需要在TweetAdapter.java
的getView()中查看安装视图
。请查看我的答案,如果有任何疑问,请告诉我。@ShoebSiddique谢谢。是否有简短的教程(或文档)可以快速总结这一点?您需要在TweetAdapter.java
的getView()中查看安装视图
。请查看我的答案,如果有任何疑问,请告诉我。@ShoebSiddique谢谢。有简短的教程(或文档)吗这很快就概括了这一点?这导致了应用程序在Emmulator上运行时崩溃,您在哪一行上运行时崩溃?您在Logcath中看到了什么错误这导致了应用程序在Emmulator上运行时崩溃,您在哪一行上运行时崩溃?以及您在Logcath中看到了什么错误这导致了应用程序在Emmulator上运行时崩溃我知道你会崩溃?你在Logcat中看到了什么错误