1

我制作了自定义列表视图,它从手机存储中加载了很多东西,当我向下滚动时,列表视图文本会像这样消失:

在此处输入图像描述

那么如果我向上滚动可见的文本也会消失,经典的列表视图会显示所有文本。这是我的适配器代码:

public MusicAdapter(Context context, int layoutResourceId, ListView data[]){
               super();
               mContext = context;

                this.layoutResourceId = layoutResourceId;
                this.context = context;
                this.data = data;
}

public int getCount() {
    return count;
}

public Object getItem(int position) {
     return position;
}

public long getItemId(int position) {
     return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ListViewHolder holder = null ;
    String id = null;

    if(row == null){
        LayoutInflater inflater = getLayoutInflater();
        row = inflater.inflate(R.layout.listview_item_row, parent, false);
        holder = new ListViewHolder();
        music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
        musiccursor.moveToPosition(position);
        id = musiccursor.getString(music_column_index);
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.txtTitle.setText(id);
        holder.imgIcon.setImageResource(R.drawable.musicc);
        row.setTag(holder);
    } else {
         holder = (ListViewHolder)row.getTag(); 
    }
    holder.txtTitle.setVisibility(View.VISIBLE);
    holder.txtTitle.setText(id);
    holder.imgIcon.setImageResource(R.drawable.musicc);
    return row;
}

和行:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <ListView
        android:id="@+id/PhoneMusicList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:scrollingCache="false"
        >
    </ListView>

</LinearLayout>
4

1 回答 1

1

您需要将此代码移到 if 语句之外(上方或下方):

music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);

否则id就是null当你的代码通过 else 分支时。

于 2013-02-23T19:04:38.183 回答