I KNOW this is a repost, but I've been trying to get this to work for ages (hours) now and I really can't understand the existing answers. All i want to know is: How can I edit this code so it works? I'm trying to populate both textviews from two different arrays.
Only the second adapter gets read and the first one's textview stays blank.
Any help at all would be appreciated.
public void run () {
if (t.getState() == Thread.State.TERMINATED) {
adapter2 = new ArrayAdapter<String>(getApplicationContext(), R.layout.listlook, R.id.txtl2, names);
setListAdapter(adapter2);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.listlook, R.id.txtl1, comments);
setListAdapter(adapter);
return;
} else {
h.postDelayed(this, 1000);
}
}}
, 1000);
}
Thanks in advance.
Listview within Usercomments.xml
<ListView
android:id="@+android:id/list"
android:layout_width="match_parent"
android:layout_height="150dp" >
</ListView>
listlook.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txtl1"
android:paddingLeft="2dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#0000FF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/txtl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtl1"
android:gravity="bottom|right"
android:paddingLeft="5dp"
android:text="TextView"
android:textColor="#5C002E"
android:textSize="15sp" />
</RelativeLayout>
listlook is just the layout i use in the listview? Don't really know what I'm doing.
You're telling your list view that you want to use adapter2
, and then telling it you want to use adapter
- it can only read data from one adapter at a time.
If you want to display data from two lists, you could do something like this.
First, combine your two lists into a class, and build a List<Post>
of these, instead of having two arrays:
public class Post {
String mName, mComment;
public Post(String name, String comment) {
mName = name;
mComment = comment;
}
public String getName() {
return mName;
}
public String getComment() {
return mComment;
}
}
Then, write an adapter that knows how to display Post
items:
public class PostAdapter extends BaseAdapter {
private Activity mActivity;
private List<Post> mPosts;
public TweetstreamAdapter(Activity activity, List<Post> posts) {
mActivity = activity;
mPosts = posts;
}
@Override
public int getCount() {
return mPosts.size();
}
@Override
public Post getItem(int position) {
return mPosts.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Post post = getItem(position);
if (convertView == null) {
convertView = mActivity.getLayoutInflater().inflate(R.layout.listlook, parent, false);
}
((TextView)convertView.findViewById(R.id.txtl1)).setText(post.getName());
((TextView)convertView.findViewById(R.id.txtl2)).setText(post.getComment());
return convertView;
}
}
Then in your activity, you set the adapter like this:
setListAdapter(new PostAdapter(this, posts));
where posts
is a List<Post>
of posts.
Note: You'll need to ensure you have Updated to match your id's. This should just drop in, provided you construct a list of TextView
entries in your listlook
layout that have IDs that match those that the adapter is searching for. Update the code to match your IDs accordingly.Post
objects correctly.
Update 2: You could build a list like this, assuming the two arrays (names/comments) are the same length:
List<Post> posts = new ArrayList<Post>();
for(int i = 0; i < names.length; i++) {
posts.add(new Post(names[i], comments[i]);
}