【问题标题】:ListView adapter and ArrayAdapterListView 适配器和 ArrayAdapter
【发布时间】:2012-11-27 18:25:42
【问题描述】:

首先我是一个编程菜鸟,我想做一些事情,比如当他/她选择 Clear_data 时将用户带到 Clear_data 活动中。

我遇到了 try and catch 的问题,因为我在一般编程中仍然有点迷茫。当我选择第一个选项 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();
    }   

}

感谢你们的时间。

【问题讨论】:

    标签: android string listview android-arrayadapter listadapter


    【解决方案1】:

    你应该像这样添加一个 switch 操作符:

    @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;
    }
    
    }
    

    【讨论】:

    • @Ket 永远欢迎你。如果有帮助,请接受答案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多