2014-10-18 6 views
0

더 쉬운 방법이 있나요? 사용자가 목록 항목을 클릭하면Android 목록 더 쉬운 방법보기

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 세 배열에서 paramteres을 데려 갈거야 ... 클릭 할 때마다이 작업을 수행해야합니다.

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; 

더 쉬운 방법은 없나요?

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

을하고 타일/콘텐츠 배열에 적절하게 위치 :

+0

콘텐츠로 키와 값으로 hashmap이 있습니다. –

+0

배열에 관한 것이 아닌 switch 문에 대해 묻습니다 : D –

+0

매번 다른 활동이 동일하거나 변경됩니다 위치에 따라 다릅니 까? –

답변

1

우선 지금

같은 ArrayList를 생성

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 <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); 
} 
+0

하지만 그는 스위치 구문을 단순화하는 방법을 묻는 것이지 배열을 단순화하는 방법이 아닙니다. 그는 각 항목 클릭마다 대상 활동이 달라지기를 원합니다. – mjp66

+0

스위치 케이스가 신속하게 배열에서 클래스에 액세스하는 것입니다 그 이유는 내가 대답에 포함되지 않았다. –

0

이 같은 다른 배열을 추가 할 수 있습니다. 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); 
} 
관련 문제