응용 프로그램을 처음 시작할 때 열리는 DialogFragment
이 있습니다. 사용자가 선호도로 저장된 값을 선택한 다음 listview
이 선택한 값에 따라 데이터로 채워집니다.dialogfragment 내부 클래스의 어댑터를 호출하는 방법
내 사용자 지정 DialogFragment
은 내 MainActivity 내부 클래스입니다. 내 어댑터 (내 listview
데이터로드) onclick 내에서 호출 할 수 없습니다.
내부 클래스에서 어댑터 기능에 액세스하려고했지만 "정적"참조 오류가 발생합니다. 동시에, 나는 클래스 내부에 내 어댑터 기능을 내장했지만, 그 같은 결과 :
나는 주위를 장난 삼아 한 비 정적 방법
에 대한 정적 참조를 만들 수 없습니다 몇 가지 해킹과 함께,하지만 난이 일을 더 나은, 표준 방법이 있습니다. 어떤 도움/조언을 부탁드립니다.
내 코드는 아래에 나열되어 있습니다.
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();
}
}
이 작동합니다! 감사 :) – markbratanov