我的适配器有问题,它只显示de listview上的最后一个元素
呼叫适配器:
public class listrestadp extends BaseAdapter {
// Declare Variables
Context context;
String[] prdesc;
Integer[] cant;
Integer[] comensal;
Integer[] tiempo;
Integer[] idpr;
LayoutInflater inflater;
ArrayList<datoscomanda> lista;
public listrestadp(Context context, String[] prdesc, Integer[] cant, Integer[] comensal, Integer[] tiempo,Integer[] idpr) {
this.context = context;
this.prdesc = prdesc;
this.cant = cant;
this.comensal = comensal;
this.tiempo = tiempo;
this.idpr = idpr;
lista = new ArrayList<>();
for (int i = 0; i < prdesc.length; i++) {
lista.add(new datoscomanda(prdesc[i],cant[i],comensal[i],tiempo[i],idpr[i]));
}
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtprd;
TextView txtcnt;
TextView txtcomensal;
TextView txtiempo;
Button btnxpr;
//http://developer.android.com/intl/es/reference/android/view/LayoutInflater.html
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.lay_popup, parent, false);
// Locate the TextViews in listview_item.xml
txtprd = (TextView) itemView.findViewById(R.id.txtprd);
txtcnt = (TextView) itemView.findViewById(R.id.txtcnt);
txtcomensal = (TextView) itemView.findViewById(R.id.txtcomensal);
txtiempo = (TextView) itemView.findViewById(R.id.txtiempo);
btnxpr = (Button) itemView.findViewById(R.id.btndelpr);
// Capture position and set to the TextViews
txtprd.setText(lista.get(position).prdesc);
txtcnt.setText(Integer.toString(lista.get(position).cantidad));
txtcomensal.setText(Integer.toString(lista.get(position).comensal));
txtiempo.setText(Integer.toString(lista.get(position).tiempo));
btnxpr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DBhelper dbhelper = new DBhelper(context);
SQLiteDatabase dbs = dbhelper.getWritableDatabase();
lista.remove(position);
dbs.delete(DBhelper.TABLE_COMANDA, DBhelper.KEY_ID+"="+lista.get(position).idpr, null);
listrestadp.this.notifyDataSetChanged();
}
});
return itemView;
}
}
适配器:
public class listrestadp extends BaseAdapter {
// Declare Variables
Context context;
String[] prdesc;
Integer[] cant;
Integer[] comensal;
Integer[] tiempo;
Integer[] idpr;
LayoutInflater inflater;
ArrayList<datoscomanda> lista;
public listrestadp(Context context, String[] prdesc, Integer[] cant, Integer[] comensal, Integer[] tiempo,Integer[] idpr) {
this.context = context;
this.prdesc = prdesc;
this.cant = cant;
this.comensal = comensal;
this.tiempo = tiempo;
this.idpr = idpr;
lista = new ArrayList<>();
for (int i = 0; i < prdesc.length; i++) {
lista.add(new datoscomanda(prdesc[i],cant[i],comensal[i],tiempo[i],idpr[i]));
}
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtprd;
TextView txtcnt;
TextView txtcomensal;
TextView txtiempo;
Button btnxpr;
//http://developer.android.com/intl/es/reference/android/view/LayoutInflater.html
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.lay_popup, parent, false);
// Locate the TextViews in listview_item.xml
txtprd = (TextView) itemView.findViewById(R.id.txtprd);
txtcnt = (TextView) itemView.findViewById(R.id.txtcnt);
txtcomensal = (TextView) itemView.findViewById(R.id.txtcomensal);
txtiempo = (TextView) itemView.findViewById(R.id.txtiempo);
btnxpr = (Button) itemView.findViewById(R.id.btndelpr);
// Capture position and set to the TextViews
txtprd.setText(lista.get(position).prdesc);
txtcnt.setText(Integer.toString(lista.get(position).cantidad));
txtcomensal.setText(Integer.toString(lista.get(position).comensal));
txtiempo.setText(Integer.toString(lista.get(position).tiempo));
btnxpr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DBhelper dbhelper = new DBhelper(context);
SQLiteDatabase dbs = dbhelper.getWritableDatabase();
lista.remove(position);
dbs.delete(DBhelper.TABLE_COMANDA, DBhelper.KEY_ID+"="+lista.get(position).idpr, null);
listrestadp.this.notifyDataSetChanged();
}
});
return itemView;
}
}
我的数据库中有3个条目作为示例:shosho,bosho,gosho,因此我的listview中只有gosho。知道怎么解决这个问题吗?
提前致谢! 感谢您的帮助
答案 0 :(得分:0)
理论上getItem是:
@Override
public Object getItem(int position) {
return lista.get(position);
}
尝试更改getItem,然后使用
txtprd.setText(getItem(position).prdesc);