gpt4 book ai didi

自定义 ListView 中的 Android SharedPreference

转载 作者:行者123 更新时间:2023-11-29 18:12:26 25 4
gpt4 key购买 nike

我有一个自定义的可扩展 ListView ,在 ListView 的 subview 中我有一个删除条目的按钮。当我点击它时,我想删除 SharedPreferences 文件中的内容。

但在 BaseExpandableListAdapter 中我无法调用 getSharedPreferences.. 我该如何解决?谢谢..

   public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final UserMessage usermessage = (UserMessage) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_message, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
TextView tv2 = (TextView) convertView.findViewById(R.id.tv2Child);
String lastMsg = usermessage.getLastNewMsg();
tv.setText(usermessage.getNameUser()+" "+usermessage.getSurnameUser());
tv2.setText(myStringSubstract(lastMsg)+"...");
final TextView del = (TextView)convertView.findViewById(R.id.delete);
final ImageView delete = (ImageView)convertView.findViewById(R.id.deleteMsg);
del.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
del.setVisibility(4);
delete.setVisibility(1);
}
});


delete.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
int idActivity = usermessage.getIdActivity();
int idUser = usermessage.getidUser();
int sizeofChildren = children.get(groupPosition).size();
Log.d("group and cildren size",""+groups.size()+"-"+children.get(groupPosition).size());
children.get(groupPosition).remove(childPosition);


if(sizeofChildren == 1){
groups.remove(groupPosition);
}

try {
usermessage.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
notifyDataSetChanged();
}
});

删除setonclickListener..我想调用它..

最佳答案

在构造函数中使用您传递给适配器的上下文。

SharedPreferences pref = mContext.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);

关于自定义 ListView 中的 Android SharedPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963588/

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