我有一个在ListView中显示记录的类。用户可以删除记录,并且每次用户删除我想要使用新值从布局更新TextView
的记录。
我有一个XML布局页面,这个类然后引用自定义适配器。当用户删除记录时,我从另一个类(HistoryOverview.java)触发一个函数,我想更新textview。 问题是我已经编码没有错误,但我的TextView不会更新值。我在这里错过了什么或做错了吗?
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);
}
所以当我完成删除记录等时,我使用了在HistoryOverview类中的函数notifyChange。然后我来到这个功能:
第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。但不知怎的,它永远不会更新,虽然我的toast测试功能工作,我的日志都显示正确的值textView将不会更新。任何人都可以告诉我我做错了什么或我错过了什么?
答案 0 :(得分:1)
确保在调用findViewById时获得正确的TextView。我相信你的电视不是你需要的TextView对象。您应该在正确的上下文中使用findViewById。 如果您正在处理已经存在的视图,则无需夸大任何新内容。通过id找到它或以任何其他方式传递对视图的引用。
答案 1 :(得分:0)
更改此
getTotalIncomesAndExpenses();
notifyDataSetChanged();
到这个
notifyDataSetChanged();
getTotalIncomesAndExpenses();
可能是一个原因。