2012-03-01 72 views
0

我有兩個活動。首先,我把一個ListView的項目列表。第二,我也想列出一些項目,但其內容將取決於在第一個Activitу中按下的元素的位置。這是第一個ListView活動的代碼:如何將數字賦給數組並將其顯示在列表視圖中?

public class ListView1 extends Activity implements OnClickListener { 
    private ListView lv1; 
    private String cats[]; 

//some code 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list); 

     Bundle bundle = getIntent().getExtras(); 
     final String category = bundle.getString("category"); 

     lv1 = (ListView) findViewById(R.id.listView); 

      ArrayAdapter<CharSequence> adapter = ArrayAdapter 
        .createFromResource(this, R.array.first_list, 
          R.layout.list_items); 
      lv1.setAdapter(adapter); 
      cats = getResources().getStringArray(R.array.first_list); 

     lv1.setTextFilterEnabled(true); 
     lv1.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> a, View v, int position, 
        long id) { 
       String defStrID = new Integer(position).toString(); 

       Intent intent = new Intent(); 
       intent.setClass(ListView1.this, ListView2.class); 

       Bundle b = new Bundle(); 
       b.putString("defStrID", defStrID); 
       intent.putExtras(b); 

       startActivity(intent); 
      } 
     }); 
    } 
} 

這是獲取列表中的元素的位置的代碼:

String defStrID = new Integer(position).toString(); 

我得到它在第二ListView有:

Bundle bundle = getIntent().getExtras(); 
String elementID = bundle.getString("defStrID"); 

我在arrays.xmlListView有一些陣列。例如:

R.array.category1 
R.array.category2 
R.array.category3 
R.array.category4 

或數組這樣的(這不是很重要):

String category1[] = { //some items here } 
String category2[] = { //some items here } 
etc. 

舉個例子,只有幾個陣列,其實更多,所以我不希望使用if else建設。如何以另一種方式做到這一點,我不知道。第二個ListView活動將與第一個相同,除了上述內容。


次活動:

public class List2 extends ListActivity { 
    private ListView lv1; 
    private String category1[] = {"Name1", "Name2"}; 
    private String category2[] = {"Surname1", "Surname2"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Bundle bundle = getIntent().getExtras(); 
     ; 
     String elementID = bundle.getString("defStrID"); 
     String arrayName = "category" + elementID; 
     int id = getResources().getIdentifier(arrayName, "array", 
       this.getPackageName()); 
     Log.v("LOOK_FOR_ME", "category" + " and the id i got is " + id); 
     String[] stats = getResources().getStringArray(id); 
     lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_items, 
       stats)); 
    } 
} 
+0

在第二個actitivy你想要的數組從xml> – 2012-03-01 19:44:10

+0

@Samir根據在第一個活動中擊中的元素的位置,我想放入第二個活動適當的數組。例如:如果我在第一個活動中按下第一個元素,我將在第二個活動中獲得list1中的category1的數組,等等。 – Sabre 2012-03-01 19:47:51

+0

您在第一個活動中獲得的位置是基於零的。值將是R.array.category0,R.array.category1,... – njzk2 2012-03-02 10:55:06

回答

1

在你的第二個Activity得到其他活動ID:

Bundle bundle = getIntent().getExtras(); 
String elementID = bundle.getString("defStrID"); 

如果你的陣列有名字categoryX(X - 值)再建與陣列名稱對應的字符串:

String arrayName = "category" + elementID; 

然後你就可以得到那個特定的陣列的ID是這樣的:

int id = getResources().getIdentifier(arrayName, "array", getPackageName()); 

,並最終得到了String陣列,並把它的適配器:

String[] stats = getResources().getStringArray(id); 

還要記住的是,在位置該列表從0開始,所以命名您的陣列以0開頭;

+0

我做到了,但我得到錯誤:'android.content.res.Resources $ NotFoundException:字符串數組資源ID# 0x0'。我添加了根據您對問題的回答完成的代碼。敬請期待。 – Sabre 2012-03-01 20:27:13

+0

@Sabre我測試了代碼,它工作。嘗試清理該項目,看看你是否真的從第一個活動中獲得數字。 – Luksprog 2012-03-01 20:31:36

+0

我試圖創建一個newclean項目,但我有NullPointerException。你能否給我你的項目,或者請寫下第二項活動的完整代碼。 – Sabre 2012-03-01 20:42:18

相關問題