2012-03-01 60 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

相关问题