Java 如何从customlistview中删除行
我想通过按要删除的行中的按钮从customlistview中删除一行。我把我的代码: 适配器javaJava 如何从customlistview中删除行,java,android,listview,button,adapter,Java,Android,Listview,Button,Adapter,我想通过按要删除的行中的按钮从customlistview中删除一行。我把我的代码: 适配器java public class Adapter extends ArrayAdapter<String> { /** VARIABILI */ int[] bandiera = {}; int[] positivo = {}; int[] negativo = {}; String[] piatti = {}; String[] qta = {}; String[] ntavolo =
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;
}
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);
}
那么怎么做呢?我展示了一幅代表我意思的图片:
首先,与其将数据的每一部分存储在不同的数组中,不如将它们存储在表示每一行的对象中,然后在适配器中有一个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;
}
完成第一个答案后,为右视图“Negatio”创建一个监听器。此项侦听器删除数组中的对象(和字符串)。然后更新列表。祝你好运 从数据列表中删除该行,然后更新listviewyes,但如何做到这一点?这非常好!但“皮亚蒂”在super上是红色的(上下文,R.layout.rigatabella,皮亚蒂);另外,你能告诉我一个如何使用行填充的例子吗?我不擅长编程,我还将MainActivity.java放在这里以显示我填写的值。我已经添加了一些说明,说明如何使用活动中的数组创建适配器,以及如何使用行填写详细信息
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);
holder.ntavolo.setText(row.getNtavolo());