2013-03-09 74 views
3

我得到一個應用程序與ListView和任何行上,你點擊它的彈出不同的東西(與PauseDialog)。任何行中的頭文件都位於「array.xml」中,並且將在PauseDialog中彈出的文本位於Java代碼中。 問題是我需要添加很多行到ListView,我想知道是否有辦法比我做的更有效。在array.xml的 例子:ListView with onItemClick postion

<?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> 

(只是多了很多行...) java代碼:

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文件中,而不是在代碼中) 謝謝!

回答

1

您可以將您的描述爲string-arrayarrays.xml

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

..定義數組MainActivity:

static String[] desciptions = null; 

..中的onCreate初始化():

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 2013-03-14 21:02:00

+0

非常歡迎! :) – CodeShane 2013-03-15 00:54:27

+0

如果我想使它靜態? – elichai2 2013-04-23 17:39:55