Android列表查看更简单的方法

时间:2014-10-18 17:57:04

标签: android listview

有没有更简单的方法呢?

String[] values = new String[] { "1337", "2", "3DPD", "4", "10q",
        "10x", "A C?", "AAF", "ADAD", "ADIH", "ADIP", "AEAP", "AFAICR",
        "AFAICS"

};

final String[] title = new String[] { "1337", "2", "3DPD", "4", "10q",
        "10x", "A C?", "AAF", "ADAD", "ADIH", "ADIP", "AEAP", "AFAICR",
        "AFAICS"

};

final String[] content = new String[] {
        " From the word Leet, derived from the word elite",
        "too, or to",
        " 3-Dimensional Pig Disgusting, denotes two dimensions are superior to reality",
        "For", "Thank you", "Thanks", "AH! SI?", "As A Friend",
        "Another Day Another Dollar", "Another Day In Hell",
        " Another Day in Paradise", "As Early As Possible",
        "As far as I can recall / remember", " As far as I can see",};

当用户点击列表项时,我需要将信息从每个数组传递到另一个活动,例如:用户点击名称为" 1337"现在我从位置0的三个阵列中取出参数......我必须为每次点击都这样做:

switch (itemPosition) {
    case 0:
        Intent intent = new Intent(DisplayMessageActivity.this, AboutSlangs.class);
        intent.putExtra(TITLE, title[0]);
        intent.putExtra(CONTENT, content[0]);
        startActivity(intent);
        break;

更简单的方法?

2 个答案:

答案 0 :(得分:1)

首先创建一个像

这样的模型
class DataModel implements Serialiable{

private String title ;
private String value ;
private String description ;

DataModel(String ... data){

this.title = data[0] ;
this.value = data[1];
this.description = data[2] ;

}


// Generate getter setter for all 

}

现在生成像

这样的arrayList
ArrayList <DataModel> arr = new ArrayList<DataModel>();

for(int i = 0 ; i < size ; i++){

DataModel model = new DataModel("title","value","description"); //change logic according to requirement

arr.add(model); 

}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    DataModel model = arr.get(position);

    Intent intent = new Intent(DisplayMessageActivity.this, Target.class);
    intent.putExtra("DATA", model);

    startActivity(intent);
}

答案 1 :(得分:0)

你可以添加另一个这样的数组:

Class<?>[] target =  { ActivityA.class, ActivityB.class, ActivityC.class };

并将它们适当地放置到您的磁贴/内容数组中。这样你根本不需要一个switch语句,在onItemClick上只需获取位置并执行:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(DisplayMessageActivity.this, target[position]);
    intent.putExtra("TITLE", title[position]);
    intent.putExtra("CONTENT", content[position]);
    startActivity(intent);
}