问题
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.
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. Custom Adapter
@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);
}
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());
}
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?
回答1:
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.
回答2:
Change this
getTotalIncomesAndExpenses();
notifyDataSetChanged();
to this
notifyDataSetChanged();
getTotalIncomesAndExpenses();
may be a reason.
来源:https://stackoverflow.com/questions/20567843/textview-not-updating-when-deleting-record-in-custom-adapter