I have a list activity. I would like each item in the list to consist of a text view and three image views. But all the items line up. How do I configure my xml and/or code correctly?
First, just to make sure things were working, when I wrote my ListActivity I wrote my custom ListAdapter with a getView function which returned only a TextView as follows:
public class TA extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class custom_adapter implements ListAdapter {
public int getCount() {
return 3;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public View getView(int position, View convertView, ViewGroup parent){
TextView t2View = new TextView(getApplicationContext());
t2View.setText("some text");
return t2View;
}
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
this.setListAdapter(new custom_adapter());
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
}
And this worked fine. Then I tried to fill my list with a more complex view
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.tt_layout, (ViewGroup) findViewById(R.id.tt_root));
TextView tView = (TextView) layout.findViewById(R.id开发者_Python百科.tt);
switch(position){
case 0:tView.setText("Pos0");break;
case 1:tView.setText("Pos1");break;
case 2:tView.setText("Pos2");break;
case 3:tView.setText("Pos3");break;
}
return layout;
}
Using this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tt_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#DAAA"
android:weightSum="1">
<TextView android:layout_height="wrap_content" android:id="@+id/tt" android:layout_width="wrap_content" android:text="Some text" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<ImageButton android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageButton>
</LinearLayout>
But instead of the correct behavior all of the items stack next to each other and not as seperate list items.
They will not be separate items in the list. For this you should do something like this:
public View getView(int position, View convertView, ViewGroup parent) {
switch(position){
case 0: return new TextView();break;
case 1:return new ImageView();break;
case 2:return new ImageView();break;
case 3:return new ImageView();break;
}
return layout;
}
I hope you've got the idea.
You want to use a vertical orientation instead of horizontal:
android:orientation="vertical"
精彩评论