我想通過按下要刪除的行中的按鈕來從定製列表視圖中刪除行。我把我的代碼:如何刪除自定義列表視圖中的行
的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);
}
那麼該怎麼辦呢?我表明,代表我的意思的圖片:
從數據列表中刪除該行,然後更新您的列表視圖 –
是的,但如何做到這一點? –