让我的代码更简单

发布于 2024-12-18 19:08:47 字数 1482 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果除了文本和图像之外它们都相同,那么您实际上只需要 1 个 Activity 类来处理所有 10 种情况。您可以在 switch 中执行的操作是使用文本资源和可绘制资源的 id 填充 Bundle,并将其传递到 Activity 中。

因此,您的开关如下所示:

switch(position){
  case 0: 
      Intent newActivity = new Intent(diadromes.this, YourNewActivity.class);
      newActivity.putExtra("TXT_RESOURCE",R.string.your_text_resource_id);
      newActivity.putExtra("IMG_RESOURCE",R.drawable.your_img_resource_id);
      startActivity(newActivity);
      break;
   case 1:
      //similar to above, but populate with the different resource ids
}

然后在 YourNewActivity 类中,您需要读取附加内容并使用它们来填充您的 UI:

public void onCreate(Bundle savedInstanceState){
   Bundle extras = getIntent().getExtras();
   int textResourceId = extras.getInt("TXT_RESOURCE");
   int imgResourceId = extras.getInt("IMG_RESOURCE");

}

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:

switch(position){
  case 0: 
      Intent newActivity = new Intent(diadromes.this, YourNewActivity.class);
      newActivity.putExtra("TXT_RESOURCE",R.string.your_text_resource_id);
      newActivity.putExtra("IMG_RESOURCE",R.drawable.your_img_resource_id);
      startActivity(newActivity);
      break;
   case 1:
      //similar to above, but populate with the different resource ids
}

then in your YourNewActivity class, you need to read in the extras and use them to populate your UI:

public void onCreate(Bundle savedInstanceState){
   Bundle extras = getIntent().getExtras();
   int textResourceId = extras.getInt("TXT_RESOURCE");
   int imgResourceId = extras.getInt("IMG_RESOURCE");

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文