简体   繁体   中英

After scrolling listView Items View elements mixed with

I have simple ԼistView in android project .Of ListView Items are two type. 1. Song Type 2. Folder Type ListView items model 'SongModel', there are TYPE field SongModel

public class SongModel {


public String title;
public int type;

public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public int getType() {
    return type;
}
public void setType(int type) {
    this.type = type;
}

}

Of ListView items View I have 3 elements :TextView for song name,ImageView for song or folder photo,and ImageButton for song play.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/song_photo"
    android:layout_width="119dp"
    android:layout_height="match_parent"
    android:src="@drawable/ic_launcher" />



    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />


</LinearLayout>

<ImageButton
    android:id="@+id/add_queue"
    android:layout_width="wrap_content"
    android:layout_height="542dp"
    android:src="@drawable/ic_launcher" />

In Adapter class I check,if TYPE=1 I in items View paint song photo.And if TYPE=2 I paint folder photo,I ImageButton Visibility set View.GONE.

package com.example.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

  public class SongAdapter extends ArrayAdapter<SongModel> {
private ArrayList<SongModel> entries;
private Activity activity;

 static class ViewHolder {

  public ImageButton song_play;
  public TextView song_name;
  public ImageView song_photo;


 }
  public SongAdapter(Activity a, int textViewResourceId,ArrayList<SongModel> entries) {

  super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
}


 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  View rowView = convertView;
  if (rowView == null) {
  LayoutInflater inflater = activity.getLayoutInflater();
  rowView = inflater.inflate(R.layout.list_item, null);
  ViewHolder viewHolder = new ViewHolder();
  viewHolder.song_name = (TextView) rowView.findViewById(R.id.title);
  viewHolder.song_photo = (TextView) rowView.findViewById(R.id.song_photo);
  viewHolder.song_play = (ImageView) rowView .findViewById(R.id.song_play);
  rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();
SongModel song = entries.get(position);

 holder.song_name.setText(song. getTitle());

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
     holder.song_photo.setImageResource(R.drawable.song);
}
 rowView.setTag(getItemViewType(position));
 return rowView;
}
  }

in Activity code

 package com.example.adapter;

 import java.util.ArrayList;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import android.os.Bundle;
  import android.app.Activity;
   import android.view.Menu;
   import android.widget.ListView;

 public class MainActivity extends Activity {
private ArrayList<SongModel> fetch;
private ListView MusicItemlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MusicItemlistView=(ListView)findViewById(R.id.listview);
     fetch = new ArrayList<SongModel>();

    try {
        JSONObject jsonData=new JSONObject(datajson);
    JSONArray dataArray=jsonData.getJSONArray("responseBody");
    SongModel sg;
    JSONObject js;
    for(int i=0;i<dataArray.length();i++)
    {
        sg=new SongModel();
        js=(JSONObject) dataArray.get(i);
        sg.setAlbum(js.getString("artist"));
        sg.setFileName(js.getString("fileName"));
        sg.setType(Integer.parseInt(js.getString("type")));
        sg.setDuration(Integer.parseInt(js.getString("duration")));
        fetch.add(sg);
    }
    MyPerformanceArrayAdapter  adapter = new MyPerformanceArrayAdapter        (this,R.id.listview, fetch);
    MusicItemlistView.setAdapter(adapter);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

When starting application,work all right,all Items painting true,If folder no ImageButton,If song There are ImageButton.

After scrolling listView Items View elements mixed with,folder item adding ImageButton,and song Items no ImageButton.

Please help me,for correct error.

TRY this :

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
 holder.song_play.setVisibility(View.VISISBLE);
 holder.song_photo.setImageResource(R.drawable.song);
}

Make sure you set visibility of song_play to View.Visible when song type is not 2. Your cells get reused and some reused views have song_play elements invisible.

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
     holder.song_play.setVisibility(View.Visible); //Add this here.
     holder.song_photo.setImageResource(R.drawable.song);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM