2017-04-18 33 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返回。

相关问题