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文件中,而不是在代碼中) 謝謝!
非常感謝你! – elichai2 2013-03-14 21:02:00
非常歡迎! :) – CodeShane 2013-03-15 00:54:27
如果我想使它靜態? – elichai2 2013-04-23 17:39:55