gpt4 book ai didi

android - 自定义适配器不刷新 android 中 onclicklistener 的 ListView

转载 作者:行者123 更新时间:2023-11-30 03:05:43 25 4
gpt4 key购买 nike

我使用 onclicklistener 删除了数据库条目,但它没有刷新 ListView 。我怎样才能刷新这个 ListView ?

这是 ListView 的主类:

public class AFragment extends Fragment implements OnItemClickListener {

protected static final String file_name ="user";
ListView list;
Database entry;
View v;
String values[];
MySimpleArrayAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment

SharedPreferences settings = getActivity().getSharedPreferences(file_name, 0);
String name = settings.getString("name", null);


entry = new Database(getActivity());
entry.open();
values=entry.planlist(name);
entry.close();
if(values.length>0){
v = inflater.inflate(R.layout.activity_afragment, container,false);
adapter = new MySimpleArrayAdapter(getActivity(), values);
list=(ListView)v.findViewById(R.id.list);

list.setAdapter(adapter);
adapter.notifyDataSetChanged();
list.setOnItemClickListener(this);


}else{
v = inflater.inflate(R.layout.activity_my_tabs_listener, container,false);
}
// Toast.makeText(getActivity(),String.valueOf(values.length), Toast.LENGTH_LONG).show();
return v;
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(getActivity(),Details.class);
i.putExtra("sub", values[arg2]);
startActivity(i);
Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT).show();
}

}


Here i use onclicklistener to delete data from database but it is not refreshing:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {

private final Context context;
private final String[] values;
public Business aFragment = new Business();
int mypos =0;
ViewHolder holder;
View row;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context,R.layout.activity_my_simple_array_adapter, values);
this.context = context;
this.values = values;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

mypos = position;
row = convertView;
holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.activity_my_simple_array_adapter, parent, false);

TextView textView = (TextView) row.findViewById(R.id.text);
Button btn = (Button) row.findViewById(R.id.button1);
holder.tv = textView;
holder.btn = btn;
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.tv.setText(values[position]);
final int id = position;
holder.btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Database entry = new Database(context);
entry.open();
entry.delete(values[id]);
entry.close();
// Toast.makeText(getContext(), String.valueOf(id), Toast.LENGTH_LONG).show();
}
});

return row;
}

static class ViewHolder{

TextView tv;
Button btn;

}
}

最佳答案

正如 anil 所说,你应该把 notifyDataSetChanged();在 onClickListener 里面这基本上告诉适配器再次呈现列表,并将为列表中的每个可见项目再次调用 getView(),如果您的代码崩溃,您应该检查两件事:首先 - 调试程序并检查新数据是否符合您的要求,在您的情况下,检查条目是否已正确删除。

其次 - 调试 getView 方法,逐步执行每个调用并查看导致崩溃的原因。

在你的情况下,问题是你只是在更新数据库,但实际上你的 ListView 数据是从 values[] 数组中获取的,它在你删除数据库条目后没有更新,你应该创建一个函数来更新它.

关于android - 自定义适配器不刷新 android 中 onclicklistener 的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21847955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com