我今天的问题与SimpleCursorAdapter
我实施的自定义有关。这是我的活动onCreate()
和习俗SimpleCursorAdapter
:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
editor = customSharedPreference.edit();
setContentView(R.layout.activity_1);
op = new OperationsClass(getApplicationContext());
op.open();
Cursor cursor = op.getList();
startManagingCursor(cursor);
String[] columns = new String[] { "AAA", "BBB", "CCC"};
int[] to = new int[] { R.id.entry_aaa,R.id.entry_bbb, R.id.entry_ccc};
MyCursorAdapter mAdapter = new MyCursorAdapter(this, R.layout.custom_entry, cursor, columns, to);
this.setListAdapter(mAdapter);
op.close();
}
OperationsClass
管理数据库,getList(
) 函数返回条目的游标。
public class MyCursorAdapter extends SimpleCursorAdapter{
private Context context;
private MyCursorAdapter here = this;
private int layout;
public MyCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
@Override
public View newView(final Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int col1 = c.getColumnIndex("aaa");
String name1 = c.getString(col1 );
int col2 = c.getColumnIndex("bbb");
String name2 = c.getString(col2 );
int col3 = c.getColumnIndex("ccc");
int name3 = c.getInt(col3 );
final TextView text1 = (TextView) v.findViewById(R.id.entry_aaa);
final TextView text2 = (TextView) v.findViewById(R.id.entry_bbb);
final TextView text3 = (TextView) v.findViewById(R.id.entry_ccc);
text1.setText(name);
text2.setText(name2);
if (name3 == 0)
text3.setText("Not checked");
else {
text3.setText("Checked");
text3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
text3.setText("Not checked");
// Here I would like to update my DB using
// OperationsClass and the SharedPrefs,
// and refresh the ListView with the new
// text value.
}
});
}
}
return v;
}
@Override
public void bindView(View v, final Context context, Cursor c) {
// Same operations as higher
}
}
基本上我想要实现的是ListView
当用户点击第三列时刷新,这意味着它的值发生了变化(已经被点击或没有被点击)。同时我希望更新数据库和SharedPreferences
(我可以创建两个类的新对象并从应用程序上下文中恢复,但这似乎很重)。
我还想知道是否有一种方法可以在AlertDialog
打开一个活动时触发其中一个已实现的方法(在同一个应用程序中,我实际上想通过一个向我的数据库添加一个元素AlertDialog
并制作弹出它的活动up 检索一个新游标并刷新其列表)。