Tom Jackson
Tom Jackson

Reputation: 230

Listview text disappear

I made custom listview that loads alot of stuff from phone storage and when I scroll down listview text disappears like this :

enter image description here

then if I scroll up text that was visible will disappear too, with classic listview all text was shown. Here is my Adapter code :

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;
}

And 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>

main.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>

Upvotes: 1

Views: 462

Answers (1)

ashtom
ashtom

Reputation: 804

You need to move this code outside the if-statement (above or below):

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

Otherwise id is null when your code runs through the else-branch.

Upvotes: 1

Related Questions