2014-04-16 98 views
5

我有一個自定義列表視圖,它有2個textviews和2個按鈕(播放和刪除按鈕) 我想要當我點擊刪除按鈕刪除當前行。從按鈕點擊自定義列表視圖中刪除項目

我的適配器類

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.TextView; 

public class SunetePreferateAdaptor extends BaseAdapter { 

    class ob { 
     String titlu, descriere; 

     public ob(String titlu, String descriere) { 
      this.titlu = titlu; 
      this.descriere = descriere; 
     } 
    } 

    ArrayList<ob> lista; 
    Context context; 

    public SunetePreferateAdaptor(Context context) { 
     this.context = context; 
     lista = new ArrayList<ob>(); 

     for (int i = 1; i <= 20; i++) { 
      lista.add(new ob("text", "text2")); 

     } 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return lista.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return lista.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.single_favsound_row, arg2, false); 

     Button b2 = (Button) row.findViewById(R.id.button2); 
     b2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // here, i want to delete the current row of the listview 
       // 
       // 
      } 
     }); 
     TextView titlu = (TextView) row.findViewById(R.id.singleText2); 
     titlu.setText(lista.get(arg0).titlu); 
     titlu.setTextColor(Color.WHITE); 
     titlu.setTypeface(Global.font1); 
     TextView descriere = (TextView) row.findViewById(R.id.singleText1); 
     descriere.setText(lista.get(arg0).descriere); 
     descriere.setTextColor(Color.WHITE); 
     descriere.setTypeface(Global.font1); 

     return row; 
    } 
} 

嗯,我該怎麼辦呢? 我試着讓ArrayList的靜態和刪除點擊它的項目..但沒有成功..

回答

8

您無需使ArrayList爲靜態。

您需要刪除填充listview的列表中的數據。您可以撥打電話notifyDataSetChanged();來刷新lsitview。

可以消除靜電關鍵字和使用

Button b2 = (Button) row.findViewById(R.id.button1); 
     b2.setTag(arg0); 
     b2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       int pos = (int)arg0.getTag(); 
        lista.remove(pos); 
        SunetePreferateAdaptor.this.notifyDataSetChanged();   } 
     }); 

備選:

,可以在列表傳遞到適配器類的構造函數。

ListView lv = (ListView) this.findViewById(R.id.listView1);  
    ArrayList<ob> lista = new ArrayList<ob>(); 

     for (int i = 1; i <= 20; i++) { 
      lista.add(new ob("text", "text"+i)); 

     } 

lv.setAdapter(new SunetePreferateAdaptor(this,lista));

然後讓這個在獨立的java文件

class ob { 
    String titlu, descriere; 

    public ob(String titlu, String descriere) { 
     this.titlu = titlu; 
     this.descriere = descriere; 
    } 
} 

然後

public class SunetePreferateAdaptor extends BaseAdapter { 


    ArrayList<ob> lista; 
    Context context; 

    public SunetePreferateAdaptor(Context context, ArrayList<ob> lista) { 
     this.context = context; 
     this.lista= lista; 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return lista.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return lista.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.fg, arg2, false); 

     Button b2 = (Button) row.findViewById(R.id.button1); 
     b2.setTag(arg0); 
     b2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       int pos = (int)arg0.getTag(); 
        lista.remove(pos); 
        SunetePreferateAdaptor.this.notifyDataSetChanged();   } 
     }); 
     TextView titlu = (TextView) row.findViewById(R.id.textView1); 
     titlu.setText(lista.get(arg0).titlu); 
     titlu.setTextColor(Color.WHITE); 

     TextView descriere = (TextView) row.findViewById(R.id.textView2); 
     descriere.setText(lista.get(arg0).descriere); 


     return row; 
    } 
} 
+0

SunetePreferateAdaptor.this.notifyDataSetChanged();取得了訣竅 – Roisgoen

+0

使用此唯一的第一項是刪除不具體的項目。 – NagarjunaReddy

+0

@NagarjunaReddy然後你沒有做正確 – Raghunandan

4

使用ArrayList中LISTA試試這個

b2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      lista.remove(position); 
      SunetePreferateAdaptor.notifyDataSetChanged(); 
     } 
    }); 
+0

但聲明我的列表是公共靜態嗎? –

+0

@Paul它應該是'arg0'而不是'position',因爲它內部的匿名內部類必須是'final',或者你可以像我的文章 – Raghunandan

+0

@Paul所建議的那樣做,你還需要改變這個'public void onClick查看arg0){'to'public void onClick(View v){'coz you'public View getView(int arg0,View arg1,ViewGroup arg2){'第一個參數也是arg0 – Raghunandan

1

你可以做到了。首先刪除當前位置的項目並調用adapter.notifyDataSetChanged()函數。

1

此代碼工作絕對對我很好。

public class CustomAdapter extends ArrayAdapter<String> 
{ 
Context c1; 
String s1[]; 
int s2[]; 
CustomAdapter(Context c,String s[],int s3[]) 
{ 
    super(c,R.layout.tcustom,s); 
    this.c1=c; 
    this.s1=s; 
    this.s2=s3; 
} 

public View getView(int position,View v,ViewGroup parent) 
{ 
    LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    v=li.inflate(R.layout.tcustom,null); 
    TextView tv=(TextView)v.findViewById(R.id.textView); 
    ImageView im=(ImageView)v.findViewById(R.id.imageview); 
    tv.setText(s1[position]); 
    im.setImageResource(s2[position]); 

    Button bt = (Button) v.findViewById(R.id.button); 
    bt.setTag(position); //important so we know which item to delete on button click 

    bt.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) 
     { 
      v.setVisibility(View.GONE); 
      notifyDataSetChanged(); 


      int positionToRemove = (int)v.getTag(); //get the position of the view to delete stored in the tag 
      removeItem(positionToRemove); //remove the item 
     } 
    }); 

    return v; 
} 

public void removeItem(int position){ 
    //convert array to ArrayList, delete item and convert back to array 
    ArrayList<String> a = new ArrayList<>(Arrays.asList(s1)); 
    a.remove(position); 
    String[] s = new String[a.size()]; 
    s=a.toArray(s); 
    s1 = s; 
    notifyDataSetChanged(); //refresh your listview based on new data 

} 
public int getCount() { 
    return s1.length; 
} 
public String getItem(int position) { 
return s1[position]; 
}} 
相關問題