2013-03-09 3 views
3

ListView로 App을 얻었고 어떤 행에서나 다른 팝업을 클릭합니다. (PauseDialog 사용). 그리고 어떤 행의 헤더도 "array.xml"에 있고 PauseDialog에서 팝업 될 텍스트는 자바 코드에 있습니다. 문제는 ListView에 많은 행을 추가해야한다는 것이고 내가 한 것보다 더 효과적 인 방법이 있는지 알고 싶습니다. array.xml의 예 :ListView with onItemClick 포스터

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
     <array name="Row_List"> 
      <item>row1</item> 
      <item>row2</item> 
      <item>row3</item> 
      <item>row4</item> 
    </array> 
</resources> 

(단지 더 많은 행 ...) 자바 코드 :

public class MainActivity extends Activity implements OnItemClickListener { 
    ListView list; 
    ArrayAdapter<String> adaptr; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     list = (ListView) findViewById(R.id.listView1); 
     adaptr = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1 
         , getResources().getStringArray(R.array.Row_List)); 
      View header = getLayoutInflater().inflate(R.layout.header, null); 
     list.addHeaderView(header); 
     list.setAdapter(adaptr); 
     list.setOnItemClickListener(this); 
     list.setFastScrollEnabled(true); 
    } 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
     Dialog dialog1 = new Dialog(this, R.style.PauseDialog); 
     dialog1.setContentView(R.layout.dialog1); 
     TextView text = (TextView) dialog1.findViewById(R.id.tv1); 
     dialog1.setTitle(R.string.Title); 
     dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
     switch(position) { 
     case 0: 
      String url = "My-Site.com"; 
      Intent i = new Intent(Intent.ACTION_VIEW); 
      i.setData(Uri.parse(url)); 
      startActivity(i); 
      break; 
     case 1: 
      text.setText("Description of Row1"); 
      dialog1.show(); 
      break; 
     case 2: 
      text.setText("Description of Row2"); 
      dialog1.show(); 
      break; 
     case 3: 
      text.setText("Description of Row3"); 
      dialog1.show(); 
      break; 
     case 4: 
      text.setText("Description of Row4"); 
      dialog1.show(); 
      break; 
     } 
    } 
} 

그래서 대화 상자에 설명을 넣을 수있는 더 좋은 방법이 ? (그리고 모든 설명이 xml 파일에 있고 코드에없는 방법이 있다면) 고마워요!()에서 onCreate에서 초기화 ..

static String[] desciptions = null; 

: 정의 ...

<string-array name="desciptions"> 
    <item>item 0 descript</item> 
    <item>item 1 descript</item> 
    <item>item 2 descript</item> 
</string-array> 

배열 MainActivity를 :

답변

1

당신은 string-arrayarrays.xml에서 귀하의 설명을 저장할 수 있습니다

if (null==desciptions) desciptions 
    = getResources().getStringArray(R.array.descriptions); 

.. switch 문을 다음과 같이 바꾸고 y 우리의 '0'특별한 경우 :

if (0==position) { 
    String url = "My-Site.com"; 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(url)); 
    startActivity(i); 
} else { 
    text.setText(descriptions[position]); 
    dialog.show(); 
} 

그냥 당신이 당신의 특별한 0 사건에 대한 수당을해야 명심, 당신의 인덱스는 text.setText(descriptions[position-1]);를 사용하거나 빈 항목을 0 설명을 필요로 정렬 유지합니다.

+0

대단히 감사합니다! – elichai2

+0

대단히 환영합니다! :) – CodeShane

+0

그리고 정적으로 만들고 싶다면? – elichai2