2011-03-09 21 views
16

हाय,
मैं एक साधारण फ़ाइल ब्राउज़र ऐप पर काम कर रहा हूं। मैंने इसमें से अधिकांश सेट अप किया है (जहां यह अलग-अलग निर्देशिकाओं में सबकुछ सूचीबद्ध करता है और क्या नहीं) लेकिन जो अभी मैं फंस गया हूं (कुछ घंटों के लिए इस पर काम किया) जब एक सूची आइटम चुना जाता है, तो मैं चाहता हूं एक कस्टम सूची संवाद दिखाई देते हैं। मुझे एंड्रॉइड डेवलपमेंट पेज पर यह कोड मिला और इसे थोड़ा सा संशोधित किया। वर्तमान में यह सिर्फ चयनित चीज़ों का टोस्ट देता है लेकिन मुझे तीन वस्तुओं को अलग करने की आवश्यकता है। यही है, मैं एक टोस्ट से अधिक करना चाहता हूं और प्रत्येक चयन अलग-अलग आदेश चलाता है। यहां मेरा वर्तमान कोडएंड्रॉइड कस्टम सूची संवाद

final CharSequence[] items = {"Info", "Rename", "Delete"}; 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Options for " + file.getName()); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
     } 
    }).show(); 

किसी भी व्यक्ति के लिए धन्यवाद जो मुझे इसे अलग करने में मदद कर सकता है। मैंने कथन के कुछ अलग-अलग बदलावों की कोशिश की है और जो कुछ भी मैंने कोशिश की है, वह विफल नहीं हुई है।

उत्तर

11

आइटम पूर्णांक आप प्राप्त करते हैं, charsequence सरणी कि अपने कार्यों में शामिल है के सूचकांक है तो (अपने onClick विधि के अंदर) कार्रवाई का चयन किया गया आप इस तरह कर सकता है पाने के लिए:

if (item == 0) 
{ 
    // Info item 
} 
else if (item == 1) 
{ 
    // Rename, and so one 

या आप इस तरह कर सकता है:

if (items[item].equals("Info")) 
{ 
    // Info item 
} 
else if (items[item].equals("Rename") 
{ 
    // Rename, and so one 
} 

लेकिन पहले विधि

+0

धन्यवाद एक टन मैं बहुत करीब मैं कोड है कि बस एक दूसरे की तरह है, लेकिन के बजाय "जानकारी" और "नाम बदलें" मैं 0 था और 1 lol तो मैं दो का एक मिश्रण की तरह था की तरह दिखाई देता का उपयोग कर रहा था वैसे भी धन्यवाद एक टन पूरी तरह से काम किया – user577732

1

एक छोटी सी देर पसंद है, लेकिन यह मदद मिल सकती है। मैं इसे एक संवाद में एक कस्टम सूची पॉप्युलेट करने के लिए उपयोग कर रहा हूँ। मैं एक कर्सर का उपयोग कर रहा है, लेकिन आप भी एक ArrayAdapter उपयोग कर सकते हैं या जो कुछ भी आपके फैंसी सूट:

Dialog aDialog = new Dialog(this); 
AlertDialog.Builder bDialog = new AlertDialog.Builder(this); 

Cursor books = managedQuery(booksprovider.CONTENT_URI_BOOKS, null, null, null, null); 
ListView booksToAdd = new ListView(this); 
SimpleCursorAdapter books_list = new SimpleCursorAdapter(this, R.layout.shelves_add, books, 
    new String[] { BOOKS_TITLE, BOOKS_AUTHOR },//columns to include in view 
    new int[] { R.id.search_results_title, R.id.search_results_author });//views to bind columns to 

booksToAdd.setAdapter(books_list); 
bDialog.setView(booksToAdd); 

bDialog.setPositiveButton("Add to Shelf", new DialogInterface.OnClickListener() { }); 
aDialog = bDialog.create(); 
0
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       String[] name = new String[] {"item1","item2"}; 
     builder.setItems(name, new OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch(which){ 
           case 0: 
        //click item 1 
      break; 
     case 1: 
//click item 2 
break; 
            } 

      } 
     }); 
     builder.show(); 
-1

मैं संवाद में संवाद बॉक्स बोल रहा हूँ यहाँ मेरे कोड है ..

इस प्रयास करें:

public class AddTimerDialog extends DialogFragment { 

    AlertPositiveListener alertPositiveListener; 

    interface AlertPositiveListener { 
     public void onPositiveClick(int position); 
    } 

    public void setPositiveClickListener(
      AlertPositiveListener alertPositiveListener) { 
     this.alertPositiveListener = alertPositiveListener; 
    } 

    OnClickListener positiveListener = new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      AlertDialog alert = (AlertDialog) dialog; 
      int position = alert.getListView().getCheckedItemPosition(); 
      if (alertPositiveListener != null) 
       alertPositiveListener.onPositiveClick(position); 
     } 
    }; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     Bundle bundle = getArguments(); 
     int position = bundle.getInt("position"); 

     AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); 

     b.setSingleChoiceItems(ReminderSnooze.code, position, null); 

     b.setPositiveButton("OK", positiveListener); 

     AlertDialog d = b.create(); 

     return d; 
     } 
    } 
0

और कहा कि संवाद जो यू एक भी चयन संवाद खोलने के लिए चाहते हैं में फोन .. FragmentManager प्रबंधक = getFragmentManager();

 /** Instantiating the DialogFragment class */ 
     AddTimerDialog alert = new AddTimerDialog(); 
     alert.setPositiveClickListener(this); 

     /** Creating a bundle object to store the selected item's index */ 
     Bundle b = new Bundle(); 

     /** Storing the selected item's index in the bundle object */ 
     b.putInt("position", position); 

     /** Setting the bundle object to the dialog fragment object */ 
     alert.setArguments(b); 

     /** Creating the dialog fragment object, which will in turn open the alert dialog window */ 
     alert.show(manager, "alert_dialog_radio"); 
संबंधित मुद्दे