【问题标题】:Click listview item and display image单击列表视图项目并显示图像
【发布时间】:2014-09-21 19:34:18
【问题描述】:

我正在尝试创建一个列表视图,如果您在该列表中按下某个项目,它会全屏显示图像。我的列表中有大约 10 个不同的项目,我想为每个项目显示不同的图像。

我用过这个教程:http://www.androidhive.info/2011/10/android-listview-tutorial/

但我不想显示我的字符串项目的名称,而是希望它显示图像。我该如何解决这个问题?

我一直在尝试检查其他问题,但由于我是新手,所以很难理解。

【问题讨论】:

  • 你必须在 onItemClickListener 中启动一个新的 Activity

标签: java android eclipse listview


【解决方案1】:

首先,您需要在您的可绘制文件夹中为所有不同的项目提供图像(在列表视图中) 将 single_list_item.xml 更改为

single_list_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />    
</LinearLayout>

改变你的 SingleListItem.java

  package com.androidhive.androidlistview;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;

    public class SingleListItem extends Activity{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.single_list_item_view);

            ImageView productimage = (ImageView) findViewById(R.id.image);

            Intent i = getIntent();
            // getting attached intent data
            String product = i.getStringExtra("product");
            // displaying selected product name
            switch(product){
          case "item1":
              productimage.setImageDrawable(R.drawable.image1);
           break;
          case "item2":
              productimage.setImageDrawable(R.drawable.image2);
          break;
          case "item3":
              productimage.setImageDrawable(R.drawable.image3);
         break;
          .
          .
          .
          .
          .
//upto all 10 images

         }

        }
    }

【讨论】:

  • 这段代码有一些错误。 setImageDrawable 采用 Drawable 参数,而不是整数 id。它应该是setImageResource。 switch 语句的语法也不正确:case:item1 应该是 case item1:。同样,字符串只能在 Java 7 及更高版本的 switch 语句中使用,并且 case 中的值必须是常量字面量。
  • 感谢您的帮助,我得到了它的工作。不知何故,我结合了你的想法或其他东西。真的不知道我是如何设法解决它的。我一开始分别尝试了您的两种方法,但没有一个有效。然后我只是搞砸了,让它工作!非常感谢!
  • 很高兴能帮到你。编码愉快。
【解决方案2】:

只需对教程代码进行一些更改,您就可以做到这一点。

首先,在single_list_item_view.xml 中,您需要使用ImageView 而不是TextView,因为您要显示图像。您可以找到图像视图的文档,包括它们支持的属性,on the Android developer site

其次,您需要更改查找TextView 的行以找到新的ImageView。这只需要更改以下行:

TextView txtProduct = (TextView) findViewById(R.id.product_label);

到:

// Assuming you gave your ImageView the id "product_image" in single_list_item_view.xml
ImageView productImage = (ImageView) findViewById(R.id.product_image);

您需要将要显示的图像添加到项目中。这些最简单的地方是res/drawable 文件夹。图像文件的名称将决定它们的“ids”。例如,如果您有res/drawable/photoshop.png,则可以使用 id R.drawable.photoshop 引用它。

接下来,您需要确定在SingleListItem 中显示哪个图像。单击列表中的项目如何显示其他内容的关键部分在SingleListItem

Intent i = getIntent();
String product = i.getStringExtra("product");

相应地,在AndroidListViewActivity:

Intent i = new Intent(getApplicationContext(), SingleListItem.class);
i.putExtra("product", product);
startActivity(i);

Intent 是您将信息从列表活动发送到显示项目的活动的方式。在这种情况下,发送的信息是要显示的项目。你有几个关于如何做这部分的选择。一种简单的方法是检查product 字符串并选择适当的drawable:

int imageId = -1;
if (product.equals("Adobe After Effects")) {
  imageId = R.drawable.after_effects;
} else if (product.equals("Adobe Bridge")) {
  imageId = R.drawable.bridge;
} else if ...
  ... // All your other cases
}

这种方法显然不能很好地扩展,但对于您的简单示例来说它可以正常工作。另一种可能的方法是在Intent 中传递图像ID,而不是产品名称,例如:

// In AndroidListViewActivity:
i.putExtra("product", R.drawable.after_effects);

// In SingleListItem:
int imageId = i.getIntExtra("product", -1); 
// -1 in the call above is the value to return if "product" is not in the Intent

您需要做的最后一件事是为您之前找到的ImageView 设置Drawable

// Make sure to check that the id is valid before using it
if (imageId != -1) {
  Drawable d = getResources().getDrawable(imageId);
  productImage.setDrawable(d);
}

getResources() 为您的应用程序获取 Resources 对象。您可以使用Resources 对象来查找您在res 文件夹中定义的字符串和可绘制对象等内容。您正在学习的教程还使用它在 res 文件夹中查找字符串数组。同样,您可以在 Android developer site 上看到 Resources 支持的所有内容。

【讨论】:

  • 为什么行:“int imageId = -1;”上图: if (product.equals("Adobe After Effects")) { imageId = R.drawable.after_effects; } else if (product.equals("Adobe Bridge")) { imageId = R.drawable.bridge; } else if ... ... // 你的所有其他情况 } 它似乎对我产生了错误
【解决方案3】:

您可以使用传递的项目来检查应在图像视图中显示的图像,而不是在 SingleListItem 类中显示项目/文本。类似的东西:

public class SingleListItem extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);

        // TextView txtProduct = (TextView) findViewById(R.id.product_label);
        ImageView image = (ImageView) findViewById(R.id.someImage);

        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("product");
        // displaying selected product name
        if (product.equals("item1")) {
            image.setDrawableResource(R.drawable.YOURIMAGE);
        } else if (product.equals("item2"))
            image.setDrawableResource(R.drawable.ANOTHERIMAGE);

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多