我输入新数据后,我的ListView
将无法刷新。它只会在我更改标签时刷新。我输入了notifyDataSetChange()
但它仍然无效。救命啊!
public class Tab4 extends Fragment {
TextView title;
TextView DateTitle;
TextView AmountTitle;
TextView DescTitle;
ListView ExpensesList;
List<Expenses> Expenses;
Expenses expenses;
ExpensesListAdapter adapter;
Toast mToast;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.tab4, container, false);
title = (TextView) rootView.findViewById(R.id.Title);
DateTitle = (TextView) rootView.findViewById(R.id.DateTitle);
AmountTitle = (TextView) rootView.findViewById(R.id.AmountTitle);
DescTitle = (TextView) rootView.findViewById(R.id.DescTitle);
ExpensesList = (ListView) rootView.findViewById(R.id.ExpensesList);
Expenses = new ArrayList<Expenses>();
expenses = new Expenses();
mToast = Toast.makeText(getActivity().getApplicationContext(), "",
Toast.LENGTH_SHORT);
ExpensesList.setAdapter(adapter);
adapter = new ExpensesListAdapter(
getActivity().getApplicationContext(), Expenses);
if (expenses.getAllExpenses().size() == 0) {
title.setText("Total number of record is 0");
DateTitle.setVisibility(View.GONE);
AmountTitle.setVisibility(View.GONE);
DescTitle.setVisibility(View.GONE);
} else {
for (int i = 0; i < expenses.getAllExpenses().size(); i++) {
Expenses.add(expenses.getAllExpenses().get(i));
System.out.println("Expenses are "
+ expenses.getAllExpenses().get(i));
adapter.notifyDataSetChanged();
ExpensesList.invalidateViews();
}
title.setText("Total number of record is "
+ expenses.getAllExpenses().size());
}
ExpensesList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setMessage("ARE YOU SURE YOU WANT TO DELETE THIS RECORD?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
expenses = new Expenses();
expenses = expenses.getAllExpenses().get(
position);
expenses.delete();
Expenses.remove(position);
adapter.notifyDataSetChanged();
title.setText("Total number of record is "
+ expenses.getAllExpenses().size());
mToast.setText("Record has been deleted");
mToast.show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.show();
return false;
}
});
return rootView;
}
}
class ExpensesListAdapter extends BaseAdapter {
List<Expenses> Expenses;
Context context;
LayoutInflater inflater;
public ExpensesListAdapter(Context context, List<Expenses> Expenses) {
this.context = context;
this.Expenses = Expenses;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Expenses.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView Date;
TextView Amount;
TextView Desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
convertView = inflater.inflate(R.layout.listview_expenseslist, null);
holder.Date = (TextView) convertView.findViewById(R.id.ExpensesDate);
holder.Amount = (TextView) convertView
.findViewById(R.id.ExpensesAmount);
holder.Desc = (TextView) convertView.findViewById(R.id.ExpensesDesc);
holder.Date.setText(Expenses.get(position).Date);
holder.Amount.setText(String.valueOf(Expenses.get(position).Amount));
holder.Desc.setText(Expenses.get(position).Desc);
return convertView;
}
}
答案 0 :(得分:0)
我输入新数据后,我的列表视图不会刷新。它只会 如果我改变标签,请刷新。
因为当前在Expenses
ArrayList中添加了新数据。
要更新ListView Adapter中的新项目,需要在data-sorcude中添加项目,该项目在适配器内用于获取数据和getCount`方法。
要实现同样的目的,请在ExpensesListAdapter
中创建一个方法:
public void addAllItem(List<Expenses> strItem){
this.Expenses.addAll(strItem);
this.notifyDataSetChanged();
}
现在,在addNewItem
点击正面按钮时,调用AlertDialog
方法添加新项目:
adapter.addNewItem(expenses.getAllExpenses());