2017-04-18 65 views
-2

我是Android工作室編程的新手。我希望在另一個alertdialog中顯示來自第二陣列列表的已檢查的國家首都城市。以下是我的java代碼。 請幫助我如何以檢索國家首都各城市如何使用java在alertdialog中的多重陣列列表中顯示選中國家的首都城市

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 
    List<CharSequence> list = new ArrayList<CharSequence>(); 
    List<CharSequence> list2 = new ArrayList<CharSequence>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //for (int i=0;i<6;){ 
     list.add(0, "Kenya"); 
     list.add(1, "Uganda"); 
     list.add(2, "Tanzania"); 
     list.add(3, "S.Sudan"); 
     list.add(4, "Rwanda"); 

     list2.add(0, "Nairobi"); 
     list2.add(1, "Kampala"); 
     list2.add(2, "Der-es-salaam"); 
     list2.add(3, "Juba"); 
     list2.add(4, "Kigali"); 

     View button = (View) findViewById(R.id.btnFindCapitol); 
     assert button != null; 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // Intialize readable sequence of char values 
       final CharSequence[] diagCountryList= list.toArray(new  CharSequence[list.size()]); 
      //final CharSequence[] diagCapitolList = list2.toArray(new CharSequence[list2.size()]); 
      final AlertDialog.Builder countryDialog = new AlertDialog.Builder(MainActivity.this); 
      final AlertDialog.Builder capitolDialog = new AlertDialog.Builder(MainActivity.this); 
      countryDialog.setTitle("Select Item"); 
      int count = diagCountryList.length; 
      boolean[] is_checked = new boolean[count]; 

      // Creating multiple selection by using setMutliChoiceItem method 
      countryDialog.setMultiChoiceItems(diagCountryList, is_checked, 
        new DialogInterface.OnMultiChoiceClickListener() { 
         public void onClick(DialogInterface dialog, 
              int whichButton, boolean isChecked) { 
         } 
        }); 
      countryDialog.setCancelable(false); 
      countryDialog.setPositiveButton("Capitol cities", 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          ListView list = ((AlertDialog) dialog).getListView(); 
          //Apply logic here 
          StringBuilder cityBuilder = new StringBuilder(); 
          for (int i = 0; i < list.getCount(); i++) { 
           boolean checked = list.isItemChecked(i); 
           //if more than one item is checked separate them 
           if (checked) { 
            if (cityBuilder.length() > 0) cityBuilder.append("\n"+"\n"); 
             cityBuilder.append(list.getItemAtPosition(i)); 
           } 
          } 
         /*Check string builder is empty or not. If string builder is not empty. 
          It will display on the screen. 
         */ 
           if (cityBuilder.toString().trim().equals("")) { 
            capitolDialog.setMessage("Nothing was  selected"); 
            capitolDialog.show(); 

            //stringBuilder.setLength(0); 
           }/* else if  (cityBuilder.toString().trim().equals("")) { 
            capitolDialog.setTitle("The city"); 
            capitolDialog.setMessage(cityBuilder); 
            capitolDialog.show(); 

           }*/else { 
            capitolDialog.setTitle("The cities"); 
            capitolDialog.setMessage(cityBuilder); 
            capitolDialog.show(); 

           } 
          } 
         }); 
       countryDialog.setNeutralButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int  which) { 
          } 
         }); 
       AlertDialog alert = countryDialog.create(); 
       alert.show(); 
      } 
     }); 

    } 
} 
+0

你爲什麼這樣做?如果我是你,我會使用散列表或散列表,使用數字作爲關鍵字,並使用一個字符串數組作爲值。 – Amnor

+0

我不知道該怎麼做老闆..給我一個示例代碼,在我的示例代碼中使用/使用harshmaps。 – Asiku

+0

務必: HashMap中<中等,列表>地圖=新的HashMap <中等,列表>();/*啓動; */ ArrayList的測試=新的ArrayList ();/*創建輔助*/ 測試。加( 「烏干達」); test.add(「Kampala」); map.put(1,測試);/*添加城市和國家1 */ 這對於每個人來說都是這樣,而且你得到了HashMap的工作 – Amnor

回答

0

做下,我已經把意見讓你瞭解:

HashMap<Int, List<String>> map = new HashMap<Int, List<String>>();/*start;*/ 
    ArrayList <String>test = new ArrayList<String>();/*create aux*/ 
    test.add("uganda"); 
    test.add("Kampala"); 
    map.put(1, test);/*adding city and country 1*/ 
    //Do this in a bucle for everyone, and you got the HashMap working 
    //to get 
    map.get(1); 

現在你有這樣的結構:[1: {「uganda」,「Kampala」}],請將它應用於您的案例,並且您將擁有一個哈希映射,其中包含所有與城市和國家相關的數字,例如這個數字,並且將該數字用作關鍵字它會將名稱作爲Value返回。

相關問題