2017-04-18 12 views
-2

私はAndroidのスタジオのプログラミングには初めてです。私は、第2のarraylistからの確認された国会議事堂の都市を別のalertdialogで表示したい。以下は私のJavaコードです。 ない国に、次の個々の首都の都市Javaを使用して、アラートダイアログで複数のarraylistにチェックインした国の首都を表示する方法

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

私はその上司をする方法を知らない。私のサンプルコードでは、私のサンプルコードでハッシュマップを使用/使用するサンプルコードを送る。 – Asiku

+0

ドゥ: のHashMap >マップ=新しいHashMapの>();/*スタート; */ ArrayListのテスト=新しいArrayListを();/* AUX */ テストを作成します。 add( "uganda"); test.add( "Kampala"); map.put(1、test);/*都市と国を追加する1 */ すべての人のためにこれを行い、あなたはHashMapの作業を得ました – Amnor

答えて

0

をretriveする方法を、私を助けてください、私はあなたが理解するために、コメントを入れている:[1:

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); 

が今、あなたのような構造を持っています: {"uganda"、 "Kampala"}]、あなたのケースにこれを適用するために小包を作成します。そして、このような都市や国にすべての数字がリンクされたhashMapがあります。 、名前として値を返します。

関連する問題