刷新自定义列表首选项

时间:2014-01-02 11:24:40

标签: android listpreference

我从数据库填充listpreference。之后我想从数据库中删除一些行并使用listpreference刷新活动。我想我需要从数据库中选择数据并再次填充lispreference。但我不知道该怎么做。你能帮我个忙吗?这是我的代码。

public class CustomListPreference extends ListPreference {
    CustomListPreferenceAdapter customListPreferenceAdapter = null;
   ...

@Override
protected void onPrepareDialogBuilder(Builder builder)
{

    List<String> entries = new ArrayList<String>();

    db = dbHelp.getReadableDatabase();

//      Here I fill entries from database

    customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);

    builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {

        }
    });

}

private class CustomListPreferenceAdapter extends BaseAdapter
{        
    public CustomListPreferenceAdapter(Context context)
    {

    }

    public void updateResults() {
        notifyDataSetChanged();
    }

   public View getView(final int position, View convertView, ViewGroup parent)
    {  
//        Here I fill my listpreference with elements from entries.

        return row;
    }


}

public void openDialog(View v){
    AlertDialog.Builder builder = new AlertDialog.Builder(Context)
   // Dialog to delete row from database. After that I want to refresh activity with listpreference.

    .setItems(R.array.edit, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

       db = dbHelp.getWritableDatabase();
       db.delete("tuser", "name = ?", new String[] { selected_elem });
       db.close();

       customListPreferenceAdapter.updateResults(); // This doesn't work. 


        }
    });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

0 个答案:

没有答案