我有一个ListView只显示一项而不是两项。
多数民众赞成在我的MainActivity:

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>

最佳答案

@roiberg
您的代码在我的模拟器中运行正常。
检查您的XML代码

关于android - ListView只有一项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13493718/

10-12 05:00