2012-11-27 62 views
0

首先,我是一个编程noob,我想做一些事情,比如当他/她选择Clear_data时会让用户进行Clear_data活动。ListView适配器和ArrayAdapter

我遇到了尝试和碰到的问题,因为在一般编程中我仍然有点迷路。当我选择Text_Colour的第一个选项并且打开Clear_data活动而不是Text_Colour时,正在测试它。

下面是代码:

public class Settings extends ListActivity { 


String classes[] = { "Text_Colour", "Clear_data", "Contact Developer" }; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(Settings.this, android.R.layout.simple_list_item_1, classes)); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    String colours = classes[0]; 
    String cdata = classes[1]; 

    try { 
    Class Class1 = Class.forName("com.example.test1." + colours); 
    Intent intent1 = new Intent(Settings.this, Class1); 
    startActivity(intent1); 
    }catch(ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    try { 
    Class Class2 = Class.forName("com.example.test1." + cdata); 
    Intent intent2 = new Intent(Settings.this, Class2); 
    startActivity(intent2); 
    } 
    catch(ClassNotFoundException d) { 
     d.printStackTrace(); 
    } 

} 

感谢您的时间家伙。

回答

2

您sholud可能添加开关操作是这样的:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 

    String colours = classes[0]; 
    String cdata = classes[1]; 

switch(position){ 
case 0: 
    try { 
    Class Class1 = Class.forName("com.example.test1." + colours); 
    Intent intent1 = new Intent(Settings.this, Class1); 
    startActivity(intent1); 
    }catch(ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
break; 
case 1: 
    try { 
    Class Class2 = Class.forName("com.example.test1." + cdata); 
    Intent intent2 = new Intent(Settings.this, Class2); 
    startActivity(intent2); 
    } 
    catch(ClassNotFoundException d) { 
     d.printStackTrace(); 
    } 
break; 
} 

} 
+0

感谢您的!完全忘记了使用switch语句! – Ket

+0

@Ket随时欢迎您。如果有帮助,请接受答案;) –