在自定义适配器删除记录时的TextView不更新 [英] TextView not updating when deleting record in custom adapter

查看:272
本文介绍了在自定义适配器删除记录时的TextView不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,显示在ListView纪录。用户可删除的记录,每次用户删除的记录我要更新的TextView 从我的布局,新的值。

I have a class which shows record in a ListView. The user can delete the records and everytime the user deletes a record I want to update a TextView from my layout with the new values.

我有一个XML布局页,一类,然后是指自定义适配器。当用户删除记录触发我从另一个类(HistoryOverview.java),我想更新TextView的功能。
事情是我得到的一切codeD没有错误,但我的TextView不更新值。我失去了一些东西在这里或做错了什么?

I've got a XML layout page, a class which then refers to a custom adapter. When the user deletes a record I trigger a function from another class (HistoryOverview.java) where I would like to update the textview. The thing is I've got everything coded without errors but my TextView does not update the values. Am I missing something here or doing something wrong?

1。 Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow
            android:paddingLeft="5dp"
            android:paddingTop="5dp" >

            <ImageView
                android:id="@+id/barGraphUsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:background="#808080"
                android:contentDescription="@string/emptyTextfield" />
        </TableRow>


            <TextView
                android:id="@+id/overviewFinances"
                android:paddingLeft="5dp"
                android:paddingTop="5dp"
                android:textColor="#97ca3e"
                android:text="@string/emptyTextfield"
                android:textSize="16sp" />
    </TableLayout>

    <ListView
        android:id="@+id/listViewFinances"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="1dp"
        android:paddingBottom="50dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="20dp" />

</LinearLayout>

2。自定义适配器

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.listviewitem_history, parent,
                false);
        //if users delete something
        final ImageView deleteFinance = (ImageView) rowView.findViewById(R.id.deleteFinance);

                //on long click to really delete the category.
                deleteFinance.setOnLongClickListener(new AdapterView.OnLongClickListener(){

                    public boolean onLongClick(View view) {
                        //getting id that we set in the tag a bit higher.
                        String financeId = view.getTag().toString();

                        //now convert to long to input in the db.
                        long deleteId = Long.valueOf(financeId);

                        //delete the note from the db, from the listview and update the adapter.
                        db.deleteFinance(deleteId);
                        values.remove(position);


                        getTotalIncomesAndExpenses();

                        notifyDataSetChanged();
                        return true;
                    }
                });

        //return the view.
        return rowView;
    }

    public void getTotalIncomesAndExpenses(){
        HistoryOverview HO = new HistoryOverview();

        //all the numbers get calculated here etc but I would like to keep this code private
        //its rather sensitive data. But all those variables are okay and do work.
        HO.notifyChange(sumExpenses, sumIncomes, currency);
    }

所以,当我做我删除使用函数有NotifyChange这是我Hi​​storyOverview类中的记录等。然后,我来到了这个功能:

So when I'm done deleting the record etc I use the function notifyChange which is in my HistoryOverview class. Then I come to this function:

3。 HistoryOverview.Java

    public void notifyChange(int sumExpenses, int sumIncomes, String currency){
        Log.i("sumExpenses, incomes", String.valueOf(sumExpenses + " " + sumIncomes));
        LayoutInflater li = (LayoutInflater)contextForAdapter.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View vi = li.inflate(R.layout.activity_historyoverview, null);

        ListView listViewFinances = (ListView)vi.findViewById(R.id.listViewFinances);
        listViewFinances.setVisibility(vi.GONE);
        listViewFinances.setVisibility(View.GONE);
        vi.bringToFront();


        TextView tv = (TextView)vi.findViewById(R.id.overviewFinances);
        String text = "Sum incomes: " + sumIncomes + currency
                + "\tSum expenses: " + sumExpenses + currency
                + "\tMoney left: " + (sumIncomes - sumExpenses)
                + currency;
        tv.setText(text);
        tv.setBackgroundColor(Color.RED);

        Toast.makeText(contextForAdapter, 
                   text, Toast.LENGTH_LONG).show();
        Log.i("tv:", tv.getText().toString());
    }

在那里,我终于更新的TextView。但不知何故,它永远不会更新,虽然我的吐司测试功能工程和我所有的日志显示正确的值TextView的将不会更新。有人能告诉我什么,我做错了还是什么,我缺少什么?

Where I will finally update the textView. But somehow it never updates, although my toast test function works and my logs all show the correct values the textView will NOT update. Can anybody please tell me what I am doing wrong or what I am missing?

推荐答案

请确保你调用findViewById时得到正确的TextView。我相信,您的电视是不是你所需要的TextView的对象。你应该在正确的环境中使用findViewById。
如果你上已经存在的意见的工作,你不必夸大任何新的东西。根据ID找到它或通过参考视图任何其他方式。

Make sure you're getting the right TextView when calling findViewById. I believe your tv is not the TextView object you need. You should use findViewById in the correct context. If you're working on the views that are already there you don't need to inflate anything new. find it by id or pass the reference to the view any other way.

这篇关于在自定义适配器删除记录时的TextView不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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