notifyDataSetChange不与自定义适配器的ListView刷新 [英] notifyDataSetChange does not refresh ListView with custom adapter

查看:181
本文介绍了notifyDataSetChange不与自定义适配器的ListView刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出解决办法,但我只是不能看到我做错了。我想动态填充的ListView的产品(添加产品阵列然后刷新列表,基本上每点击一个产品添加到阵列,然后显示ListView控件数组的内容),这与从第一个分片数据第二个片段。传递数据的工作(使用的日志确认),但我的主要问题是更新具有自定义适配器ListView控件。有了这个code什么也没有发生(Viewlist产品不清爽,我可以看到每一个日志(以及其他一切作品),并没有错误)。
这里是code(所有这一切都在第2个片段):

I am trying to figure this out, but i just cant see what i am doing wrong. I want to dynamically fill ListView with products (add product to array then refresh list, basically for every click add one product to array then display content of array in ListView) which is in 2nd fragment with the data from the first Fragment. Passing Data is working (used Log to confirm), but my main problem is updating the ListView which has custom adapter. With this code nothing is happening (ViewList is not refreshing, I can see every log (and everything else works) and no errors). Here is the code (All this is in 2nd Fragment):

这code是全球

ReceiptListAdapter receiptListAdapter; 
ArrayList<ReceiptItem> receiptItemArrayList;
int cat = 0;
int prodct = 0;

有了这个,我得到2个号码,从1日片段(该片段2日)

public void use(int num1, int num2) {
     Log.d("LOG","Category: " + num1 + "Product: "  + num2); //This works
     cat = num1;
        prodct = num2;

    ListView receiptItemListView = (ListView) getView().findViewById(
                R.id.receiptItemsList);
        receiptItemArrayList = (ArrayList<ReceiptItem>)generateReceiptItemsForTest();

        receiptItemListView.setAdapter(receiptListAdapter); //i think here is the problem

             receiptListAdapter.notifyDataSetChanged();    

}

OnViewCreated

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ListView receiptItemListView = (ListView) getView().findViewById(
            R.id.receiptItemsList);




    receiptItemArrayList = (ArrayList<ReceiptItem>) generateReceiptItemsForTest();

    receiptListAdapter = new ReceiptListAdapter(
            getActivity().getBaseContext(), R.layout.receipt_item,
            receiptItemArrayList);



    receiptItemListView.setAdapter(receiptListAdapter);


    TextView txtTotalAmmount = (TextView) getView().findViewById(
            R.id.txtReceiptTotalAmmount);
    double totalAmmount = getTotalAmmount(receiptItemArrayList);
    totalAmmount = Math.round(totalAmmount * 100) / 100;
    txtTotalAmmount.append(String.valueOf(totalAmmount));

}

private List<ReceiptItem> generateReceiptItemsForTest() { 
    List<ReceiptItem> receiptItemList = new ArrayList<ReceiptItem>();

    DatabaseAdapter db = new DatabaseAdapter(getActivity());




    if(cat != 0 && prodct != 0 )
    {

    name = db.readFromCategoriesAndGetOnePreduct(cat, prodct).getName();
    price = db.readFromCategoriesAndGetOnePreduct(cat, prodct).getPrice();
    Log.d("Name: " + name,"Price: " + String.valueOf(price)); //Also working (showing what i want) and i can see it in log but the ListView isnt refreshing this data

    receiptItemList.add(new ReceiptItem(name,1,price,1);


    }
                }


    return receiptItemList;
}



private double getTotalAmmount(ArrayList<ReceiptItem> receiptItems) {
    double totalAmmount = 0;
    for (ReceiptItem receiptItem : receiptItems) {
        totalAmmount += receiptItem.getTotalPrice();
    }
    return totalAmmount;
}

ReceiptListAdapter

public class ReceiptListAdapter extends ArrayAdapter<ReceiptItem> {

private Context context;

public ReceiptListAdapter(Context context, int rowResourceId,
        ArrayList<ReceiptItem> items) {
    super(context, rowResourceId, items);
    this.context = context;
}

@SuppressWarnings("deprecation")
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;



    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.receipt_item, null);
    }

    ReceiptItem item = getItem(position);
    if (item != null) {
        TextView itemDescription = (TextView) view
                .findViewById(R.id.txtReceiptItemDescription);
        TextView itemAmmount = (TextView) view
                .findViewById(R.id.txtReceiptItemAmmount);
        TextView itemPrice = (TextView) view
                .findViewById(R.id.txtReceiptItemPrice);
        TableRow listItem = (TableRow) view
                .findViewById(R.id.trReceiptItem);
        if (position % 2 == 0)

listItem.setBackgroundDrawable(view.getResources().getDrawable(
                    R.drawable.list_item_selector_a));
        else
                listItem.setBackgroundDrawable(view.getResources().getDrawable(
                    R.drawable.list_item_selector_b));

        itemDescription.setText(item.getDescription());
        itemAmmount.setText(String.valueOf(item.getAmmount()));
        itemPrice.setText(String.format("%.2f", item.getPrice()));
    }

    return view;
}

 }

只有我拿到名单更新时间是当我用这个code

Only time I got the list to update is when I used this code

receiptListAdapter = new ReceiptListAdapter(getActivity().getBaseContext(),R.layout.receipt_item,

而不是

receiptItemListView.setAdapter(receiptListAdapter);

但它只会增加一次又一次刷新上单击列表,但我知道这是为什么。

But it only adds one time and refreshes the list again on click, but i know why.

推荐答案

我想通了,我不得不做出这个全球性列表&LT; ReceiptItem&GT; receiptItemList =新的ArrayList&LT; ReceiptItem&GT;(); 此外,我要感谢middleseatman用来解释一些事情,这真的帮助了我。

I figured it out i had to make this global List<ReceiptItem> receiptItemList = new ArrayList<ReceiptItem>(); Also I would like to thank middleseatman for explaining some things, it really helped me.

这篇关于notifyDataSetChange不与自定义适配器的ListView刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆