Android 如何在dialogfragment内部类中调用适配器

Android 如何在dialogfragment内部类中调用适配器,android,android-asynctask,android-fragments,android-dialogfragment,Android,Android Asynctask,Android Fragments,Android Dialogfragment,我有一个对话框fragment,它在应用程序第一次启动时打开。用户选择一个值,然后将该值存储为首选项,然后根据所选值用数据填充列表视图 问题是,我的自定义对话框片段是一个内部类,在我的MainActivity中。我似乎无法在onclick中调用我的适配器(它将数据加载到我的listview) 我尝试访问内部类中的适配器函数,但收到一个“静态”引用错误。同时,我尝试将适配器函数嵌入类中,但结果相同: 无法对非静态方法进行静态引用 我曾玩弄过一些黑客,但我知道有一种更好的标准方法。如有任何帮助/建议

我有一个
对话框fragment
,它在应用程序第一次启动时打开。用户选择一个值,然后将该值存储为首选项,然后根据所选值用数据填充
列表视图

问题是,我的自定义
对话框片段
是一个内部类,在我的MainActivity中。我似乎无法在onclick中调用我的适配器(它将数据加载到我的
listview

我尝试访问内部类中的适配器函数,但收到一个“静态”引用错误。同时,我尝试将适配器函数嵌入类中,但结果相同:

无法对非静态方法进行静态引用

我曾玩弄过一些黑客,但我知道有一种更好的标准方法。如有任何帮助/建议,将不胜感激

下面列出了我的代码

public class MainActivity extends ListActivity {

private Cursor lines;
private MetroSleepDb db;
private ListAdapter adapter;
public static final String PREFS_NAME = "METROSLEEP_PREFS";
public static int PREFS_CITY = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean prefs_isset = settings.contains("DEFAULT_CITY");

    if(prefs_isset) {

        PREFS_CITY = settings.getInt("DEFAULT_CITY", 0);


    } else {

        chooseCityDialog();
        PREFS_CITY = settings.getInt("DEFAULT_CITY", 0);

    }

    Editor editor = settings.edit();
    editor.putInt("DEFAULT_CITY", PREFS_CITY);
    editor.commit();

}


public void setAdapters() {

    Toast.makeText(getApplicationContext(), "Preference set to: "+PREFS_CITY, Toast.LENGTH_LONG).show();

    db = new MetroSleepDb(this);
    lines = db.getLines(); // you would not typically call this on the main thread
    //ListView listView = (ListView) findViewById(R.id.li);
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
            lines, 
            new String[] {"line_id"}, 
            new int[] {android.R.id.text1}, 0);


    getListView().setAdapter(adapter);
    getListView().setOnItemClickListener(onAnswerClicked);

}


public String getItem(int pos) {

    Cursor c = (Cursor) adapter.getItem(pos);
    String value = c.getString(c.getColumnIndex("line_id"));
    return value;
}


private OnItemClickListener onAnswerClicked = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        String line_value = getItem(position);
        Intent intent = new Intent(MainActivity.this, ChooseStations.class);
        intent.putExtra("line", line_value);
        startActivity(intent);
    }

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem  item) {
    switch (item.getItemId()) {
    case android.R.id.home:

        NavUtils.navigateUpFromSameTask(this);
        return true;

    case R.id.menu_settings:
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void chooseCityDialog() {

    DialogFragment newFragment = new DialogSetup();
    newFragment.setCancelable(false);
    newFragment.setRetainInstance(true);
    newFragment.show(getFragmentManager(), "citypref");


}


public static final class DialogSetup extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.prompt_choose_city)
               .setCancelable(false) 
               .setInverseBackgroundForced(true)
               .setItems(R.array.Cities, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {

                       PREFS_CITY = which;

               }
        });

        return builder.create();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Do whatever
    }

}



@Override
protected void onStop(){
   super.onStop();

  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  //editor.putBoolean("silentMode", mSilentMode);

  settings.getInt("DEFAULT_CITY", PREFS_CITY);
  editor.commit();
}
}
public类MainActivity扩展了ListActivity{
专用光标线;
专用MetroSleepDb;
专用列表适配器;
公共静态最终字符串PREFS_NAME=“METROSLEEP_PREFS”;
公共静态int PREFS_CITY=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedReferences设置=GetSharedReferences(首选项名称,0);
布尔值prefs_isset=settings.contains(“默认城市”);
如果(预设置){
PREFS\u CITY=settings.getInt(“默认城市”,0);
}否则{
选择secitydialog();
PREFS\u CITY=settings.getInt(“默认城市”,0);
}
Editor=settings.edit();
编辑器.putInt(“默认城市”,首选城市);
commit();
}
公共void setAdapters(){
Toast.makeText(getApplicationContext(),“首选项设置为:”+PREFS\u CITY,Toast.LENGTH\u LONG.show();
db=新的MetrosReepDB(本);
lines=db.getLines();//通常不会在主线程上调用此函数
//ListView ListView=(ListView)findViewById(R.id.li);
adapter=new SimpleCursorAdapter(这是android.R.layout.simple\u list\u item\u 1,
线,
新字符串[]{“line_id”},
新的int[]{android.R.id.text1},0);
getListView().setAdapter(适配器);
getListView();
}
公共字符串getItem(int-pos){
游标c=(游标)适配器.getItem(pos);
字符串值=c.getString(c.getColumnIndex(“行id”);
返回值;
}
onAnswerClicked上的私有OnItemClickListener=新建OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
字符串行_值=getItem(位置);
意向意向=新意向(MainActivity.this,ChooseStations.class);
意向。额外(“行”,行_值);
星触觉(意向);
}
};
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
NavUtils.navigateUpFromSameTask(本);
返回true;
案例R.id.菜单\u设置:
Intent Intent=新Intent(这是setingsactivity.class);
星触觉(意向);
返回true;
}
返回super.onOptionsItemSelected(项目);
}
public void chooseCityDialog(){
DialogFragment newFragment=新建DialogSetup();
newFragment.setCancelable(false);
newFragment.setRetainInstance(true);
show(getFragmentManager(),“citypref”);
}
公共静态最终类DialogSetup扩展了DialogFragment{
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setTitle(R.string.prompt\u选择城市)
.setCancelable(错误)
.setInverseBackgroundForced(真)
.setItems(R.array.Cities,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
PREFS_CITY=哪个;
}
});
返回builder.create();
}
@凌驾
公共void onDismiss(对话框接口对话框){
//做任何事
}
}
@凌驾
受保护的void onStop(){
super.onStop();
SharedReferences设置=GetSharedReferences(首选项名称,0);
SharedReferences.Editor=settings.edit();
//putBoolean(“silentMode”,mSilentMode);
getInt(“默认城市”,首选城市);
commit();
}
}
我曾尝试在内部类中访问适配器函数,但我 收到“静态”参考错误。同时,我试着 在类中嵌入适配器函数,但这导致 同样:

对话框片段
中,您已经有了对
活动
的引用,其中显示了可用于访问适配器的
对话框

public void onClick(DialogInterface dialog, int which) {
      PREFS_CITY = which;
      MainActivity ma = (MainActivity) getActivity();
      // create a getter method in the MainActivity to access the adapter 
      // or whatever else you need
}