【问题标题】:A customadapter only show last element自定义适配器仅显示最后一个元素
【发布时间】:2016-12-01 22:54:26
【问题描述】:

我的适配器有问题,它只显示 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,因此我的列表视图中只有 gosho。知道如何解决这个问题吗?

提前致谢! 感谢大家的帮助

【问题讨论】:

    标签: android arraylist adapter android-arrayadapter


    【解决方案1】:

    理论上 getItem 是:

    @Override
    public Object getItem(int position) {
        return lista.get(position);
    }
    

    尝试更改getItem,然后使用

    txtprd.setText(getItem(position).prdesc);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      相关资源
      最近更新 更多