2

所以这个问题有点难以解释,但基本上我想要的是有一个列表视图的数组列表,但更改列表视图上的标题。因此,当用户看到列表视图时,它会说一件事,但会传递不同的信息。这是我的代码:

//the string names
String names[] = { "item1", "item2"};

我有 2 个名为 item1.java 和 item2.java 的类,当单击一个列表项时,它会设置一个等于活动名称的类并像这样打开它:

    protected void onListItemClick(ListView lv, View v, int position, long id){
    super.onListItemClick(lv, v, position, id);
    String openClass = names[position];
    try{
        Class selected = Class.forName("com.example.example." + openClass);
        Intent selectedIntent = new Intent(this, selected);
        startActivity(selectedIntent);
    }catch (ClassNotFoundException e){
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

但是在列表视图中我不想让它说“item1”和“item2”....有没有办法像别名或其他东西一样?抱歉,如果我的问题难以理解,我已尽力解释我需要帮助的内容,如果您对我的问题有任何疑问,请告诉我(:感谢您的帮助

4

2 回答 2

3

您可以像制作任何自定义适配器一样执行此操作,例如应该显示图像而不是某些文本或除了某些文本之外还应显示图像的适配器。

例如,如果您的适配器是ArrayAdapter,您创建一个子类 override getView(),并在其中执行您想要的操作以按照您想要的方式格式化您的行。

于 2013-05-25T01:11:13.390 回答
0

您可以覆盖toString()来执行此操作。请参阅以下示例

public static class Alias {
    String originalValue;
    public Alias(String s) {
        this.originalValue = s;
    } 

    @Override
    public String toString() {
       return "What are you want to show";
    }
}

String names[] = {"item1", "item2"};
Alias array[] = new Alias[names.length];
Alias array[0] = new Alias(names[0]);
Alias array[1] = new Alias(names[1]);
ArrayAdapter<Alias> adapter = new ArrayAdapter<Alias>(this, resId, array);

这将在每个列表视图中显示“你想显示什么”,无论值是什么。

于 2013-05-25T01:25:15.230 回答