我有一个 ListView 只显示一项而不是两项。那是我的主要活动:
public class MainActivity extends Activity {
ImageView Add;
ImageView Shadow;
ProgressBar PB;
ListView listView;
String[] valuesUp = { "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"Sed non justo eros. Praesent a nisl dui" };
String[] valuesDown = { "Quisque ut ante eu arcu sagittis porta eu a dolor...",
"In orci augue facilisis eget fermentum consequat ac..." };
int[] imgValues = {R.drawable.mobileapp_16, R.drawable.mobileapp_19};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new MyAdapter(this, valuesUp, valuesDown, imgValues);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
那就是我的适配器:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] valuesUp;
private final String[] valuesDown;
private final int[] ImageValue;
private TextView textViewUp;
private TextView textViewDown;
private ImageView image;
public MyAdapter(Context context, String[] valuesUP, String[] valuesDown, int[] imageValue) {
super(context, R.layout.rowlayout, valuesUP);
this.context = context;
this.valuesUp = valuesUP;
this.valuesDown = valuesDown;
this.ImageValue = imageValue;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
textViewUp = (TextView) rowView.findViewById(R.id.row_text_view_up);
textViewDown = (TextView) rowView.findViewById(R.id.row_text_view_down);
image = (ImageView) rowView.findViewById(R.id.image);
textViewUp.setText(valuesUp[position]);
textViewDown.setText(valuesDown[position]);
image.setImageDrawable(context.getResources().getDrawable(ImageValue[position]));
return rowView;
}}
当我调试时,我看到 getView 被调用了很多次(大约 12 次)并且“位置”始终为 0。为什么会这样?谢谢!
我现在发现了我的问题!有点奇怪。列表的高度表现得很奇怪。当我在做“wrap_content 或 match_parent”时,它给了我一个项目,但是当我放置 400dp 时它可以。为什么会这样?!那是我的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/topbar_bg" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/topbar_leftcircle" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/imageView2"
android:src="@drawable/topbar_logo" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:scaleType="fitStart"
android:src="@drawable/topbar_menu" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/imageView4"
android:src="@drawable/topbar_key" />
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="@drawable/mobileapp_14" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="@drawable/transparent_bg" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView6"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:maxLines="2"
android:text="Sed at vestibulum elit. Nunc quam erat, gravida ac consequat"
android:textColor="@color/RedText"
android:textSize="15dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:maxLines="2"
android:text="Nullam sed erat erat, ut faucibus nunc. Etiam et mi di ante lacinia suscipit. Mauris gravida turpis eget lectus ullam..."
android:textColor="@color/WhiteText"
android:textSize="12dp" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:scaleType="centerCrop"
android:src="@drawable/mobileapp_15" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_below="@id/imageView7" >
</ListView>
</RelativeLayout>
</ScrollView>