2016-08-25 99 views
2

我有一個列表視圖。我想單擊每個列表項目並打開不同的活動。其實,我寫了代碼,但它不工作。我的編碼技能無法處理這個問題。如何用字符串和類對創建Hahmap,然後將其放入ArrayAdapter。任何人都可以給我看代碼嗎?如何創建一個列表視圖,以便點擊每個列表項目將打開不同的活動?

ListViewAcivity.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class ListViewActivity extends Activity { 
    ListView listView ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_screen_activity); 

     // Get ListView object from xml 
     listView = (ListView) findViewById(R.id.list); 

     // Defined Array values to show in ListView 
     HashMap<String, Class> hashMap=new HashMap<String, Class>(); 


     hashMap.put("A Function", MActivity.class); 
     hashMap.put("B Function",AActivity.class); 
     hashMap.put("c Function",XActivity.class); 
     hashMap.put("D Function",ZActivity.class); 
     hashMap.put("E Function", PActivity.class); 
     hashMap.put("F Function", QActivity.class); 


     // Define a new Adapter 
     // First parameter - Context 
     // Second parameter - Layout for the row 
     // Third parameter - ID of the TextView to which the data is written 
     // Forth - the Array of data 

//錯誤是在這裏...... //此構造函數不能得到解決。 //我不知道如何使用// String,Class對和Array Adapter。

ArrayAdapter<HashMap<String,Class>> adapter = new ArrayAdapter<HashMap<String,Class>>(this, android.R.layout.simple_list_item_1, android.R.id.text1, hashMap); 


     // Assign adapter to ListView 
     listView.setAdapter(adapter); 

     // ListView Item Click Listener 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 

       // ListView Clicked item index 
       int itemPosition  = position; 

       // ListView Clicked item value 
       String itemValue = (String) listView.getItemAtPosition(position); 
       switch(itemPosition){ 
        case 0: Intent newActivity = new Intent(ListViewActivity.this, MActivity.class); 
         startActivity(newActivity); 
         break; 
        case 1: Intent newActivity1 = new Intent(ListViewActivity.this, AActivity.class); 
         startActivity(newActivity1); 
         break; 
        case 2: Intent newActivity2 = new Intent(ListViewActivity.this, XActivity.class); 
         startActivity(newActivity2); 
         break; 
        case 3: Intent newActivity3 = new Intent(ListViewActivity.this, ZActivity.class); 
         startActivity(newActivity3); 
         break; 
        case 4: Intent newActivity4 = new Intent(ListViewActivity.this, PActivity.class); 
         startActivity(newActivity4); 
         break; 
        case 5: Intent newActivity5 = new Intent(ListViewActivity.this, QActivity.class); 
         startActivity(newActivity5); 
         break; 


       } 

      } 
      @SuppressWarnings("unused") 
      public void onClick(View v){ 
      }; 
     });} 
} 

main_screen_Activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"></ListView> 

</LinearLayout> 

回答

1

簡單的靜態實例。

public class MainActivity extends AppCompatActivity { 

    ListView lv1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Initialize the list view 
     lv1 = (ListView) findViewById(R.id.mainlist); 

     //Add the List data 
     //as the array is stored starting with 0, the Layouts will be having 0th position, Intents being 1 and so on.. 
     String[] sessiontuts = new String[]{"Activity 1", "Activity2"}; 

     //use the Simple array adapter 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,sessiontuts); 

     //now to bind the data to list, just set the adapter we just created to the listview, 
     lv1.setAdapter(adapter); 

     //we need to have click listner on the particular item, 
     //all the items in list will have a position starting from 0 to n, 
     //so, write the intent code to launch particular activity depending on list item position 
     lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) { 
       //using switch case, to check the condition. 
       switch (pos){ 
        case 0: 
         startActivity(new Intent(getApplicationContext(), Act1.class)); 
         break; 
        case 1: 
         startActivity(new Intent(getApplicationContext(), Act2.class)); 
         break; 
       } 
      } 
     }); 
    } 
} 
2

一種ArrayAdapter期望一個陣列或對象來顯示的列表。所以你需要提供一個數組/列表

是什麼讓你認爲它可以處理地圖?列表/數組表示事物的序列;而地圖則代表了某種東西到其他東西的映射。

換句話說:你不能傳遞一個地圖對象。

只需傳遞一個包含要顯示的字符串的列表或數組(「Medical Reminders」,...)。

但是有那張地圖有價值;你只需要改變你的代碼位:

首先,你可以使用鍵映射爲輸入的創建一個ArrayAdapter,像這樣的時候:

List<String> arrayItems = new ArrayList<>(hashMap.keySet()); 

,然後在onClickItem()你做不是需要檢索索引 - 因爲你已經有了那張告訴你哪個類屬於哪個字符串的地圖!含義:ListView爲您提供所選項目字符串hashMap將所有可能的字符串映射到相應的活動類別!

所以,你可以扔掉你的整個「開關」的代碼,而是做這樣的事情:爲你所要求的功能

String itemValue = (String) listView.getItemAtPosition(position); 
Class classForSelectedValue = hashMap.get(itemValue); 
Intent activity = new Intent(ListViewActivity.this, classForSelectedValue); 
+0

感謝您的澄清。現在,我明白了。我正在修復它。再次感謝。 – SOURAV

+0

當然,如果它給你你正在尋找的東西,隨時接受我的答案。 – GhostCat

相關問題