2016-04-28 87 views
-1

我想通過按下要刪除的行中的按鈕來從定製列表視圖中刪除行。我把我的代碼:如何刪除自定義列表視圖中的行

adapter.java

public class Adapter extends ArrayAdapter<String> { 

/** VARIABILI */ 
int[] bandiera = {}; 
int[] positivo = {}; 
int[] negativo = {}; 
String[] piatti = {}; 
String[] qta = {}; 
String[] ntavolo = {}; 
Context c; 
LayoutInflater inflater; 
static int cambiobandiera=0; /** SE 0 E' VERDE, SE 1 E' ROSSA 

/** COSTRUTTORE */ 
public Adapter(Context context, String[] piatti, String[] qta, String[] ntavolo, int[] bandiera, 
       int[] positivo, int[] negativo) 
{ 
    super(context, R.layout.rigatabella, piatti); 

    this.c = context; 
    this.piatti = piatti; 
    this.qta = qta; 
    this.ntavolo = ntavolo; 
    this.bandiera = bandiera; 
    this.positivo = positivo; 
    this.negativo = negativo; 
} 

public class ViewHolder 
{ 
    ImageView bandiera; 
    TextView ntavolo; 
    TextView piatto; 
    TextView qta; 
    ImageView positivo; 
    ImageView negativo; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    if(convertView==null) 
    { 
     inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView=inflater.inflate(R.layout.rigatabella, null); 
    } 

    final ViewHolder holder= new ViewHolder(); 

    /** INZIALIZZO LE VIEWS */ 
    holder.piatto = (TextView) convertView.findViewById(R.id.piatto); 
    holder.ntavolo = (TextView) convertView.findViewById(R.id.ntavolo); 
    holder.qta = (TextView) convertView.findViewById(R.id.qta); 
    holder.bandiera = (ImageView) convertView.findViewById(R.id.bandiera); 
    holder.positivo = (ImageView) convertView.findViewById(R.id.positivo); 
    holder.negativo = (ImageView) convertView.findViewById(R.id.negativo); 

    /** ASSEGNARE I DATI */ 
    holder.bandiera.setImageResource(bandiera[position]); 
    holder.ntavolo.setText(ntavolo[position]); 
    holder.piatto.setText(piatti[position]); 
    holder.qta.setText(qta[position]); 
    holder.positivo.setImageResource(positivo[position]); 
    holder.negativo.setImageResource(negativo[position]); 

    holder.bandiera.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      if (cambiobandiera == 0) { 
       holder.bandiera.setImageResource(R.mipmap.flagrossa); 
       cambiobandiera = 1; 
      } else { 
       holder.bandiera.setImageResource(R.mipmap.flag); 
       cambiobandiera = 0; 
      } 
     } 
    }); 

    holder.positivo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //MAKE THE ROW TURNS GREEN 
     } 
    }); 

    holder.negativo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //REMOVE ROW FROM LISTVIEW 
     } 
    }); 

    return convertView; 
} 

rigatabella.xml

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


<TableRow 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:gravity="center_horizontal"> 
    <ImageView 
     android:id="@+id/bandiera" android:clickable="true" 
     android:src="@mipmap/flag" 
     android:layout_height="53dp" 
     android:layout_width="1dp" android:gravity="center" 
     android:layout_weight="1" /> 
    <TextView 
     android:id="@+id/ntavolo" android:text="tav." 
     android:layout_weight="1" 
     android:textColor="#000000" 
     android:layout_width="1dp" 
     android:padding="20dip" android:gravity="center"/> 
    <TextView 
     android:id="@+id/piatto" android:text="Piatto" 
     android:layout_weight="1" 
     android:textColor="#000000" 
     android:layout_width="250dp" 
     android:padding="20dip" android:gravity="center"/> 
    <TextView 
     android:id="@+id/qta" android:text="qta" 
     android:layout_weight="1" 
     android:textColor="#000000" 
     android:layout_width="1dp" 
     android:padding="20dip" android:gravity="center"/> 
    <ImageView 
     android:id="@+id/positivo" android:src="@mipmap/conferma" 
     android:layout_weight="1" android:layout_height="53dp" 
     android:layout_width="1dp" android:gravity="center"/> 
    <ImageView 
     android:id="@+id/negativo" android:src="@mipmap/delete" 
     android:layout_weight="1" android:layout_height="53dp" 
     android:layout_width="1dp" android:gravity="center"/> 
</TableRow> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

ListView lv; 
String[] ntavolo={"2","4","3","5"}; 
String[] piatti={"Spaghetti allo scoglio","Lasagne","Cacca","Verdure miste"}; 
String[] qta={"2","4","1","1"}; 
int[] bandiera={R.drawable.flag, R.drawable.flag,R.drawable.flag,R.drawable.flag}; 
int[] positivo={R.drawable.conferma,R.drawable.conferma,R.drawable.conferma,R.drawable.conferma}; 
int[] negativo={R.drawable.delete,R.drawable.delete,R.drawable.delete,R.drawable.delete}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lv = (ListView) findViewById(R.id.listView); 

    /** ADAPTER */ 
    Adapter adapter = new Adapter(this, piatti, qta, ntavolo, bandiera, positivo, negativo); 
    lv.setAdapter(adapter); 

} 

那麼該怎麼辦呢?我表明,代表我的意思的圖片:

enter image description here

+0

從數據列表中刪除該行,然後更新您的列表視圖 –

+0

是的,但如何做到這一點? –

回答

1

的第一件事情,而不是存儲數據的各部分在不同的陣列,你應該將它們存儲在代表則每行有一個ArrayList對象行中的適配器。

public class Row { 
    private int bandiera; 
    private int positivo; 
    private int negativo; 
    private String piatti; 
    private String qta; 
    private String ntavolo; 

    public Row(int bandiera, int positivo, int negativo, String piatta, String qta, String ntavolo) { 
     this.bandiera = bandiera; 
     this.positivo = positivo; 
     this.negativo = negativo; 
     this.piatti = piatta; 
     this.qta = qta; 
     this.ntavolo = ntavolo; 
    } 
} 

接下來,您需要對適配器進行一些更改,以使用行列表而不是您擁有的陣列。

public class Adapter extends BaseAdapter { 

/** VARIABILI */ 
ArrayList<Row> rows = new ArrayList<>(); 
Context c; 
LayoutInflater inflater; 
static int cambiobandiera=0; /** SE 0 E' VERDE, SE 1 E' ROSSA 

/** COSTRUTTORE */ 
public Adapter(Context context, ArrayList<Row> rows) 
{ 
    super(context, R.layout.rigatabella); 
    this.c = context; 
    this.rows = rows; 
} 

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

public Row getItem(int position) { 
    return rows.get(position); 
} 

public class ViewHolder 
{ 
    ImageView bandiera; 
    TextView ntavolo; 
    TextView piatto; 
    TextView qta; 
    ImageView positivo; 
    ImageView negativo; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    Row row = getItem(position); 
    // Here use row to fill in your textviews etc. You'll need to add getters to the Row model 


    holder.negativo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      rows.remove(position); // This removes the row from the dataset 
      notifyDataSetChanged(); // This tells the adapter to update (to show the change on screen) 
     } 
    }); 

    return convertView; 
} 

您的活動,您需要使用您目前有

ArrayList<Row> rows = new ArrayList<>(); 
for (int i=0; i < ntavolo.length; i++) { 
// This is not very safe. If any of the arrays are smaller than the ntavolo array it will crash 
    row.add(new Row(bandiera[i], positivo[i], negativo[i], piatti[i], qta[i], ntavolo[i]));  
} 
Adapter adapter = new Adapter(this, rows); 
lv.setAdapter(adapter); 

要在getView細節填充陣列創建行對象的列表,您需要吸氣劑添加到Row類提供並使用那些在您textviews等

holder.ntavolo.setText(row.getNtavolo()); 
+0

這是非常好的!但是「piatti」在超級上是紅色的(上下文,R.layout.rigatabella,piatti);你也可以告訴我一個關於如何填充使用行的例子嗎?我不擅長編程 –

+0

也我把我的MainActivity.java顯示我填充的值 –

+0

我已經添加了一些解釋如何在您的活動中使用數組來創建適配器以及如何使用該行來填寫詳細信息 – hibob

0

填補當你已經完成了第一個答案,創建右視圖「negativo」的OnItemClickListener。該項監聽器刪除數組中的對象(以及字符串)。然後更新列表。祝你好運!

相關問題