5

पहली बार एप्लिकेशन शुरू करने पर मैं उपयोगकर्ता को अपने संवाद का चयन करने के लिए उपयोगकर्ता (संवाद द्वारा) को संकेत देना चाहता था, जिसके परिणामस्वरूप उनकी प्राथमिकता को बचाया जाएगा और उचित डेटा को सूचीदृश्य में लोड किया जाएगा। हालांकि, मैंने देखा है कि बैक बटन द्वारा एक संवाद बंद किया जा सकता है, या संवाद बॉक्स के बाहर कहीं भी धक्का दे सकता है, जो तब डेटाबेस के लिए शून्य मान को ट्रिगर करता है।उपयोगकर्ता को संवाद में विकल्प चुनने के लिए मजबूर करें

क्या वैसे भी मैं संवाद बॉक्स को तब तक खोलने के लिए मजबूर कर सकता हूं जब तक उपयोगकर्ता ने कोई विकल्प नहीं चुना हो?

मैंने पहले से ही .setCancelable(false) लागू किया है और यह काम नहीं कर रहा है। मेरा कोड नीचे है, डायलॉगसेटअप आंतरिक कक्षा है जिसके साथ मैं काम कर रहा हूं।

किसी भी मदद/विचारों की सराहना की जाएगी।

public class MainActivity extends ListActivity { 
private Cursor lines; 
private MetroSleepDb db; 
private ListAdapter adapter; 
public static final String PREFS_NAME = "METROSLEEP_PREFS"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 


    super.onCreate(savedInstanceState); 



    chooseCityDialog(); 




} 




public void setAdapters() { 

    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: 
     // This ID represents the Home or Up button. In the case of this 
     // activity, the Up button is shown. Use NavUtils to allow users 
     // to navigate up one level in the application structure. For 
     // more details, see the Navigation pattern on Android Design: 
     // 
     // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
     // 
     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.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) 
       .setItems(R.array.Cities, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // 
       } 
     }); 

     return builder.create(); 
    } 



} 

}

+0

क्या आप उत्तर पाने के लिए फिर से संवाद शुरू करने के तरीके के रूप में उस शून्य मान ट्रिगर का उपयोग कर सकते हैं? – TronicZomB

+0

मैं यह देखने के लिए जांच सकता हूं कि कोई मान नहीं चुना गया था और संवाद को फिर से ट्रिगर किया गया था। यह वास्तव में एक समाधान है, धन्यवाद। – markbratanov

+2

यदि कोई मूल्य नहीं है, तो आप खारिज नहीं होने के लिए 'ऑनडिस्मिस लिस्टर' का भी उपयोग कर सकते हैं। Althoug, मुझे नहीं पता क्यों 'सेट रद्द करने योग्य (झूठा) 'काम नहीं कर रहा है। मैंने 'डायलॉग फ्रैगमेंट' का उपयोग नहीं किया है, लेकिन अन्य 'संवाद' – codeMagic

उत्तर

6

अहा द्वारा वापस बटन अक्षम करें! आपको पर पर setCancelable(false) पर कॉल करना होगा। यह प्रश्न देखें: AlertDialog's setCancelable(false) method not working

प्रलेखन DialogFragment.setCancelable(boolean cancelable) के लिए कहता है:

नियंत्रित करें कि दिखाया गया संवाद रद्द करने योग्य है। Dialog.setCancelable (बूलियन) को सीधे कॉल करने के बजाय इसका उपयोग करें, क्योंकि डायलॉग फ्रैगमेंट को इसके आधार पर अपना व्यवहार बदलने की जरूरत है। किसी भी तरह शहर Dialog से लौटे हैं,

public void chooseCityDialog() { 
    DialogFragment newFragment = new DialogSetup(); 
    newFragment.setCancelable(false); 
    newFragment.show(getFragmentManager(), "citypref"); 
} 

इसके अलावा, यह एक डिफ़ॉल्ट शहर वापस करने के लिए गिर करने के लिए एक अच्छा सुरक्षा जांच होगी:

तुम बस कोड की 1 लाइन जोड़ने की जरूरत null है।

+0

वू, यह काम किया। धन्यवाद! – markbratanov

2

मैं सिर्फ एक AlertDialog बजाय DialogFragment इस के लिए प्रयोग करेंगे।

AlertDialog dlgBuilder = new AlertDialog.Builder(this); 
    dlgBuilder.setMessage("Title"); 
    dlgBuilder.setCancelable(false) 
      .setPositiveButton("OK", new OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        okCalledMethodInactivity(); 
        dialogInterface.dismiss(); 
       } 
      }) 
      .setNegativeButton("Exit app", new OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        finish(); 
       } 
      }); 
    AlertDialog dlg = dlgBuilder.create(); 
    dlg.show(); 
+1

असहमत में ठीक काम करता है, Google आपको 'डायलॉग फ्रैगमेंट' - http://android-developers.blogspot.com/2012/05/using-dialogfragments.html का उपयोग करने की सलाह देता है। –

+0

यह थोड़ा उलझन में है क्योंकि AlertDialog वास्तव में आपको 'alertDialog.setCanceledOnTouchOutside (false); 'और' alertDialog.setCancelable (false) का उपयोग करने देता है;' लेकिन मुझे बिल्डर का उपयोग किए बिना एक चेतावनी Dialog के अंदर ListView नहीं मिल रहा है, जो कि रिटर्न: ** विधि setCanceledOnTouchOutside (बुलियन) प्रकार AlertDialog.Builder के लिए अपरिभाषित है ** – markbratanov

+0

शायद आप [setMultiChoiceItems] उपयोग करने के लिए (आवश्यकता http://developer.android.com/reference/android/app/AlertDialog .Builder.html # setMultiChoiceItems (java.lang.CharSequence [], बूलियन [], android.content.DialogInterface.OnMultiChoiceClickListener)) ।मैं DialogInterface में विधि सेट रद्द कर दियाऑन टचऑटसाइड देखें। तो आपको पहले अलर्ट संवाद बनाने की आवश्यकता है और फिर उस विधि का उपयोग करें। – 500865

-2

@Override 
public void onBackPressed() { 

} 
+1

तब उपयोगकर्ता कभी भी 'गतिविधि' में बैक बटन का उपयोग नहीं कर सकता ... अच्छा विचार नहीं – codeMagic

+2

सहमत है, यह एक शौकिया हैक है, समाधान नहीं। –

संबंधित मुद्दे