我想通过按下要删除的行中的按钮从自定义列表视图中删除一行。我把我的代码:
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);
}
那怎么办?我展示了一张代表我的意思的图片:
答案 0 :(得分:1)
首先,不是将数据的每个部分存储在不同的数组中,而应将它们存储在表示每行的Object中,然后在适配器中有一个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;
}
在您的活动中,您需要使用当前拥有的阵列
创建Row对象列表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中填写详细信息,您需要将getter添加到提供的Row类中,并使用它们填写您的textviews等。
holder.ntavolo.setText(row.getNtavolo());
答案 1 :(得分:0)
完成第一个答案后,为右视图“negativo”创建一个OnItemClickListener。此项侦听器删除Array(和字符串)中的对象。然后更新列表。祝你好运!