2013-12-17 129 views
1

我有一個列表,其中我從共享偏好獲取的特定數字(位置)應該顯示一個imageview(指示當前正在播放哪首歌曲)。但是我得到的位置,但該項目也顯示在其他行。當我滾動列表時發生問題。Android列表視圖行重複項目

*這是當我退出其他活動發生的事情,以及關於簡歷,我來這是我:

@Override 
protected void onResume() { 
    super.onResume(); 
    if(AlbumDetails.mediaPlayer!=null) 
    adapter = new PlaylistAdapter(this, songs); 
    list.setAdapter(adapter); 
} 

這裏是我的代碼:

public class PlaylistAdapter extends BaseAdapter { 

    private Activity activity; 
    private static LayoutInflater inflater = null; 
    private ArrayList<Songs> data; 
    private DatabaseHelper db; 
    private SharedPreferences prefs; 
    int playpos; 

    public PlaylistAdapter(Activity a, ArrayList<Songs> songs) { 
     activity = a; 
     data = songs; 
     db = new DatabaseHelper(a); 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     prefs = activity.getSharedPreferences("com.darkovski.quran", 
       Context.MODE_PRIVATE); 
     playpos = prefs.getInt("posPlaying", -1); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

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

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

    public View getView(final int position, View convertView, 
      final ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.song_item, parent, false); 

     ImageView download = (ImageView) vi 
       .findViewById(R.id.playlist_item_download); 
     db.openDB(); 
     if (db.isDownloaded(data.get(position).getNumber(), data.get(position) 
       .getRecitorName(), data.get(position).getRecitorID())) 
      download.setImageResource(R.drawable.download_yes); 
     else { 
      download.setImageResource(R.drawable.download_no); 
      download.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        new DownloadFileFromURL(activity, data.get(position) 
          .getRecitorName(), data.get(position).getTitle(), 
          data.get(position).getLink(), data.get(position) 
            .getNumber(), data.get(position) 
            .getRecitorID()).execute(); 
        if (!db.isDBOpen()) 
         db.openDB(); 
        db.addDownloaded(data.get(position).getNumber(), 
          data.get(position).getLink(), 0, data.get(position) 
            .getRecitorID(), "", data.get(position) 
            .getTitle()); 

        Toast.makeText(activity, 
          "Downloading " + data.get(position).getTitle(), 
          Toast.LENGTH_SHORT).show(); 

       } 
      }); 
     } 
     db.closeDB(); 

     TextView number = (TextView) vi.findViewById(R.id.playlist_item_num); 
     TextView title = (TextView) vi.findViewById(R.id.playlist_item_reciter); 
     title.setText(data.get(position).getTitle()); 
     number.setText((position + 1) + ""); 
     ImageView eq = (ImageView) vi.findViewById(R.id.playlist_item_equlizer); 
     //this is where i show the item 
     if (playpos == position) { 
      eq.setVisibility(View.VISIBLE); 
     } 

     return vi; 
    } 
} 

,這是它的外觀:

enter image description here

+0

接受答案,如果你發現它的幫助完整。這不是一個巴伯店... – Triode

+0

@Triode如果你向下滾動,你會看到有一個公認的答案 –

回答

7

其因此聲明

if (playpos == position) { 
     eq.setVisibility(View.VISIBLE); 
    } 

listview回收視圖,所以如果你注意到第一行和最後一行顯示相同。這是因爲第一行循環到最後一排

您可以通過添加一個else到如果這樣它看起來像

if (playpos == position) { 
     eq.setVisibility(View.VISIBLE); 
}else{ 
    eq.setVisibility(View.GONE); 
} 
+0

很容易 - 爲什麼我沒有想到這一點:)謝謝! –