4

ArrayAdapter我用一个覆盖的方法扩展了一个对象getView()来填充我的ListView对象,但是适配器会从ArrayList我传递给它的对象中获取前两个或三个对象,然后在整个列表长度中以看似随机的顺序重复这些对象。此外,当我在 中上下滚动时ListView,一些行从一个列表项更改为另一个。该ArrayList<Credit>对象是从 JSONObject 填充的,据我所知,在传递到ArrayAdapter.

我的习惯ArrayAdapter

private class CreditArrayAdapter extends ArrayAdapter<Credit> {
    private ArrayList<Credit> credits;
    private int creditCount;

    public CreditArrayAdapter(Context ctx, int textViewResourceId, List<Credit> items) {
        super(ctx, textViewResourceId, items);
        credits = (ArrayList<Credit>) items;
        creditCount = credits.size();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_movie_medium, null);
            Credit current = credits.get(position);
            Log.d(tag, "current credit: " + current.toString());
            ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
            try {
                creditPicture.setImageBitmap(current.getPosterSmall());
            } catch(Exception e) {
                Log.d(tag, "failed to set cast member picture");
                e.printStackTrace();
            }
            TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
            castMemberName.setText(current.toString());
        }
        return v;
    }
    @Override
    public int getCount() {
        return creditCount;
    }
}

被调用:

appearsIn = (ListView) findViewById(R.id.listview_appears_in);
List<Credit> credits = searcher.getCreditsByPersonId(personId);
CreditArrayAdapter creditAdapter = new CreditArrayAdapter(getBaseContext(), R.layout.list_item_movie_medium, credits);
creditAdapter.notifyDataSetChanged();
appearsIn.setAdapter(creditAdapter);

包含的布局ListView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:layout_centerHorizontal="true"
              android:weightSum="2">
    <ScrollView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">
            <ImageView android:id="@+id/image_person_info"
                       android:layout_height="wrap_content"
                       android:layout_width="wrap_content"
                       android:contentDescription="person image"/>
            <TextView android:id="@+id/text_person_name"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="30dp"/>
            <TextView android:id="@+id/text_person_birth"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="20dp"/>
            <TextView android:id="@+id/text_person_death"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="20dp"/>
            <TextView android:id="@+id/text_person_bio"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="16dp"/>
        </LinearLayout>
    </ScrollView>

    <ListView android:id="@+id/listview_appears_in"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"/>
</LinearLayout>

我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">
    <ImageView android:id="@+id/image_poster_thumbnail"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"/>
    <TextView android:id="@+id/text_credit_info"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/>
</LinearLayout>
4

3 回答 3

8

像这样更改getView:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item_movie_medium, null);
    }
    Credit current = credits.get(position);
    Log.d(tag, "current credit: " + current.toString());
    ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
    try {
       creditPicture.setImageBitmap(current.getPosterSmall());
    } catch(Exception e) {
        Log.d(tag, "failed to set cast member picture");
        e.printStackTrace();
    }
    TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
    castMemberName.setText(current.toString());

    return v;
}

您的问题是,当 convertView 为 != null 时,您没有执行正确设置 View 对象所需的所有代码。

于 2012-07-10T14:37:42.240 回答
2

使用这个片段:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_movie_medium, null);
        }

        Credit current = credits.get(position);
        Log.d(tag, "current credit: " + current.toString());
        ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
        try {
            creditPicture.setImageBitmap(current.getPosterSmall());
        } catch(Exception e) {
            Log.d(tag, "failed to set cast member picture");
            e.printStackTrace();
        }
        TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
        castMemberName.setText(current.toString());

        return v;
    }
于 2012-07-10T14:34:59.903 回答
0

尝试在任何引用高度的地方调整您的 xml 布局,因为 android 每次都会进行测量并重新绘制单元格。尝试在 listview match_parent 和行上的单元格上使用一个精确的高度。对不起我的英语不好。

于 2013-05-25T20:47:37.663 回答