2

我有一個DialogFragment,當應用程序第一次啓動時打開。用戶選擇一個值,然後將其存儲爲首選項,然後根據所選值填寫listview數據。如何調用dialogfragment內部類中的適配器

問題是我的自定義DialogFragment是一個內部類,在我的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(); 
} 
} 

回答

2

我試圖訪問的內部類我的適配器的功能,但我 收到一個「靜態」引用錯誤。與此同時,我試圖 嵌入在類中我的適配器功能,但導致 相同:

DialogFragment你已經到了Dialog顯示,你可以使用Activity參考訪問適配器:

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 
} 
+0

This Works!謝謝 :) – markbratanov 2013-04-11 20:12:31

相關問題