【问题标题】:Listview doesn't gives correct valuesListview 没有给出正确的值
【发布时间】:2016-07-18 15:35:40
【问题描述】:

单击列表视图后,我从列表视图中得到了错误的值。

流程:

a)一旦 Spinner 选择了 Listview 值,我就有了一个 Spinner,它会在 Asynctask 的帮助下得到更改。

Asynctask的OnPostMethod方法加载Listview中的项目

protected void onPostExecute(List<MIC_OrderDetails> lst) {
            dialog.setMessage("Inflating Data...");

            if (lst.get(lst.size() - 1).getResult().contains(("success"))) {

                ordList.addAll(lst);
                ordList.notifyDataSetChanged();
                dialog.dismiss();

                /*
                 * ordList = new MicListAdapter(InventoryCount.this, lst);
                 * lstView.setAdapter(ordList);
                 * 
                 * dialog.dismiss();
                 */

            } else {
                dialog.dismiss();
                toastText.setText("Problem in loading Items");
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(toastLayout);
                toast.show();
            }
        }

在我的适配器 MicListAdapter 中,我创建了 addAll() 方法,因为直接调用对我不起作用

public void addAll(List<MIC_OrderDetails> lst) {
        this.alllst.clear();
        this.alllst.addAll(lst);
        this.notifyDataSetChanged();
    }

b)OnClicking Listview 对应的值不会出现在我的自定义对话框中,即

假设我的 Spinner 包含两个值(1 和 2)。默认情况下,当我将其更改为 2 时,第一次 1 将显示在 Spinner 中,对应于“2”的 Listview 值会受到影响,但是当我单击时Listview 我得到的值属于“1”。

这是 Listview onclick 的代码 sn-p

    lstView.setOnItemClickListener(new OnItemClickListener() {
    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {

MIC_OrderDetails list_obj = new MIC_OrderDetails();
                    list_obj = (MIC_OrderDetails) lstView
                            .getItemAtPosition(position);

                    ItemNo = list_obj.getItemNumber();
                    Desc = list_obj.getItemDescription();
                    PickSeq = list_obj.getPickSeq();
                    StockUnit = list_obj.getUnit();
                    qtyonHand = list_obj.getQtyonHand();// This value gives
                                                        // QOHand
                    qtyCount = list_obj.getQtyCount();
                    loc = mspinner.getItemAtPosition(
                            mspinner.getSelectedItemPosition()).toString();

                    medtItem.setText(ItemNo);
                    medtdesc.setText(Desc);

                    qtyCount = supporter.getCurrencyFormat(Double
                            .parseDouble(qtyCount));

                    medtQtyCount.setText(qtyCount);
                    medtuom.setText(StockUnit);
                    medtQtyCountTotal.setText(qtyCount);
.....
});

这是我的完整适配器类。

public class MicListAdapter extends ArrayAdapter<MIC_OrderDetails> implements
    Filterable {

List<MIC_OrderDetails> alllst;
List<MIC_OrderDetails> list;

List<MIC_OrderDetails> filterlst;
// = new ArrayList<MIC_OrderDetails>();
Context context;

TextView txtitem;
TextView txtdesc;
TextView txtpick;
TextView txtQtyonHand;
TextView txtQtyCounted;
TextView txtuom;
/* TextView txtstatus; */

private ModelFilter filter;

@Override
public Filter getFilter() {
    if (filter == null) {
        filter = new ModelFilter();
    }
    return filter;
}

public MicListAdapter(Context context, List<MIC_OrderDetails> value) {
    // TODO Auto-generated constructor stub
    super(context, R.layout.six_textview, value);
    this.context = context;
    this.list = value;
    this.alllst = new ArrayList<MIC_OrderDetails>(list);
    this.filterlst = new ArrayList<MIC_OrderDetails>(alllst);
    getFilter();
}

public void addAll(List<MIC_OrderDetails> lst) {
    this.alllst.clear();
    this.alllst.addAll(lst);
    this.notifyDataSetChanged();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return alllst.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final int pos = position;

    View view = null;
    if (convertView == null) {

        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.six_textview, parent, false);

        txtitem = (TextView) convertView.findViewById(R.id.txt_fullItemNo);
        txtdesc = (TextView) convertView.findViewById(R.id.txt_fullDesc);
        txtpick = (TextView) convertView.findViewById(R.id.txt_fullLoc);
        txtQtyonHand = (TextView) convertView
                .findViewById(R.id.txt_fullord);
        txtQtyCounted = (TextView) convertView
                .findViewById(R.id.txt_fullrecqty);
        txtuom = (TextView) convertView.findViewById(R.id.txt_fullUom);
        convertView.setTag(new OrdersViewHolder(txtitem, txtdesc, txtpick,
                txtQtyonHand, txtQtyCounted, txtuom));

    } else {
        OrdersViewHolder viewHolder = (OrdersViewHolder) convertView
                .getTag();
    }

    OrdersViewHolder holder = (OrdersViewHolder) convertView.getTag();

    holder.txtitem.setText(alllst.get(position).getItemNumber());
    holder.txtdesc.setText(alllst.get(position).getItemDescription());
    holder.txtpick.setText(alllst.get(position).getPickSeq());
    holder.txtQtyonHand.setText((alllst.get(position).getQtyonHand()));
    /*
     * String o=(lst.get(position).getQtyShiped()).toString(); String
     * u=lst.get(position).getUom();
     */
    holder.txtQtyCounted.setText((alllst.get(position).getQtyCount()));
    holder.txtuom.setText(alllst.get(position).getUnit());
    /* holder.txtstatus.setText(alllst.get(position).getStatus()); */

    return convertView;

}

/** Holds child views for one row. */
static class OrdersViewHolder {

    TextView txtitem;
    TextView txtdesc;
    TextView txtpick;
    TextView txtQtyonHand;
    TextView txtQtyCounted;
    TextView txtuom;

    /* TextView txtstatus; */

    public OrdersViewHolder(TextView txtitem, TextView txtdesc,
            TextView txtpick, TextView txtQtyonHand,
            TextView txtQtyCounted, TextView txtuom) {
        // TODO Auto-generated constructor stub
        this.txtitem = txtitem;
        this.txtdesc = txtdesc;
        this.txtpick = txtpick;
        this.txtQtyonHand = txtQtyonHand;
        this.txtQtyCounted = txtQtyCounted;
        this.txtuom = txtuom;
        /* this.txtstatus=txtstatus; */

    }

谁能告诉我为什么会发生这种情况...在此先感谢

【问题讨论】:

    标签: android listview android-asynctask


    【解决方案1】:

    不要使用lstView,而是使用arg0,这是onItemClick获取点击项值的方法的第一个参数:

    list_obj = (MIC_OrderDetails)arg0.getAdapter().getItem(position);
    

    使用lst 列表对象:

    list_obj = lst.get(position);
    

    但需要将lst 声明为全局变量才能在扩展Asynctask 的类外部访问它

    【讨论】:

    • 我尝试了同样的方法,但它仍然和以前一样。没有任何改变。
    • 请检查我更新的 MicListAdapter 全适配器类@ρяσѕρєя K
    • @PraneshSahu:在 MicListAdapter 中添加 @Override public MIC_OrderDetails getItem(int position) { // TODO Auto-generated method stub return alllst.get(position); } 然后检查它
    • 非常感谢..它工作得很好,伙计@ρяσѕρєя K
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2011-08-13
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多