在同一活动布局上,我有一个TextView字段和一个ListView,每个项目上都有一个按钮。在我的ListView数组适配器中,我在getView()方法内部有一个按钮单击侦听器,我试图“动态”更新上面提到的TextView字段的值。如何从ListView按钮动态更新TextView字段?
到目前为止,我已经能够使用虚实实例来更新TextView值,但是当我通过重新访问主菜单中的活动页面来刷新页面时,我只能看到更新的TextView。
作为最后一个解决方案,我已将它设置为在getView()方法内的buton侦听器内重新启动活动页面,而无需手动执行。这不是最好的方式,因为用户每次点击ListView项目按钮时都能看到页面重新加载。
你能告诉我如何动态更新数值吗?
以下是我的适配器类代码:
所有的public adapter(Context context, int resource, ArrayList<value> objects)
{
super(context, resource, objects);
layout = resource;
array = objects;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
box = null;
db = new database(getContext());
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, null);
box = new holder();
box.X = (TextView) convertView.findViewById(R.id.valueName);
box.Y = (TextView) convertView.findViewById(R.id.valueNumber);
box.button = (Button) convertView.findViewById(R.id.itemDeleteButton);
box.button.setTag(position);
convertView.setTag(box);
}
else
{
box = (holder) convertView.getTag();
}
box.X.setText(getItem(position).getX());
box.Y.setText(String.valueOf(getItem(position).getY()));
box.button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
View view = View.inflate(getContext(), R.layout.activity_plan, null);
list = (ListView) view.findViewById(R.id.listView);
month = (Spinner) view.findViewById(R.id.inputSpinner);
remainder = (TextView) view.findViewById(R.id.outputRemainder);
adapt = new adapter(getContext(), R.layout.activity_item, array);
array.remove(position);
month_is = month.getSelectedItem().toString();
db.removePlanData(month_is, position + 1);
notifyDataSetChanged();
cursor = db.getHelpDataRows(month_is);
cursor.moveToFirst();
val_is = Integer.parseInt(cursor.getString(cursor.getColumnIndex("value")));
remainder_is = val_is - db.getCurrent(month_is);
remainder.setText(String.valueOf(remainder_is));
// RESTARTING ACTIVITY TO SEE THE UPDATED TEXTVIEW VALUE OUTSIDE LISTVIEW.
Intent intent= new Intent(getContext(), plan.class);
getContext().startActivity(intent);
}
});
return convertView;
}
public class holder
{
TextView X;
TextView Y;
Button button;
}
它会是有用的,如果你能证明你的代码 –
显示你的代码。 – Chisko
感谢您的回复。我已经在下面发布了我的代码。这么晚才回复很抱歉。我感谢您的帮助! – Ras