让我的代码更简单
我有一个包含 10 个列表项的应用程序。所有 onClick 项目之间都有相同的布局。我为每个项目创建了一个新类,并使用 switch 方法移动到每个活动。有什么方法可以使它更简单(并且没有 10 个类,但更少)?谢谢
mylist.add(map);
map = new HashMap<String, Object>();
map.put("name", "aa");
map.put("address", "aaa");
map.put("address3", R.drawable.im1);
mylist.add(map);// i m adding 10 items like this here
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"name", "address","address3"}, new int[] {R.id.TextView1, R.id.TextView2,R.id.imgdiadromes});
listcafe.setAdapter(mSchedule);
listcafe.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0:
Intent newActivity = new Intent(diadromes.this, monte.class);
startActivity(newActivity);
break;
case 1:
Intent newActivity1 = new Intent(diadromes.this, diadromestherisos.class);
startActivity(newActivity1);
break;
//我这里还有 8 个案例
类 mothe.class 和 diadromestherisos.class 完全相同,我得到相同的内容视图,我只更改文本和图像(从.setText 和.setImageResource)。希望我的问题可以理解!
I have an app with 10 list items.All the items onClick have the same layout between them.I have created a new class for every item and using a switch method to move to every activity.Is there any way to make it more simple( and dont have 10 classes but less)?Thanks
mylist.add(map);
map = new HashMap<String, Object>();
map.put("name", "aa");
map.put("address", "aaa");
map.put("address3", R.drawable.im1);
mylist.add(map);// i m adding 10 items like this here
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"name", "address","address3"}, new int[] {R.id.TextView1, R.id.TextView2,R.id.imgdiadromes});
listcafe.setAdapter(mSchedule);
listcafe.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0:
Intent newActivity = new Intent(diadromes.this, monte.class);
startActivity(newActivity);
break;
case 1:
Intent newActivity1 = new Intent(diadromes.this, diadromestherisos.class);
startActivity(newActivity1);
break;
//and i have 8 more cases here
The class mothe.class and the diadromestherisos.class are exactly the same,i m getting the same content view and i m only changing the text and the images(from .setText and.setImageResource).Hope my problem is understandable!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果除了文本和图像之外它们都相同,那么您实际上只需要 1 个 Activity 类来处理所有 10 种情况。您可以在 switch 中执行的操作是使用文本资源和可绘制资源的 id 填充 Bundle,并将其传递到 Activity 中。
因此,您的开关如下所示:
然后在 YourNewActivity 类中,您需要读取附加内容并使用它们来填充您的 UI:
If they're all the same except for the text and image, then you really only need 1 Activity class to handle all 10 cases. What you can do in your switch is populate a Bundle with the ids of the text resource and the drawable resource and pass that into the activity.
So your switch would look like:
then in your YourNewActivity class, you need to read in the extras and use them to populate your UI: