希望ListView打开多个不同的图像

时间:2018-01-17 01:08:26

标签: android

我正在尝试使用onclicklisteners创建一个包含多个listviews的应用。到目前为止,我已正确编写一个列表视图以打开显示图像的活动。因为我想要显示数百张图像,所以我希望减少在清单中创建的活动量。我很好奇我可以在我当前的代码中实现哪些编码行,这些代码将使用imageview打开一个活动并显示“xyz.jpg”,但如果单击一个不同的列表选项则显示“second.jpg”。我的代码如下:

 public class obstetriclist extends AppCompatActivity {

ListView listview;


//String[] fullcardiaclist = new String[]{"APGAR Score", "Childbirth / Labor", "Childbirth Procedure", "Newly Born", "Obstetrical Emergency"};
//42 pictures


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_obstetriclist);

    final Context context = getApplicationContext();

    //list contains image name, image location
    final List<ImageDisplay> myImageList = new ArrayList<ImageDisplay>();
    myImageList.add(new ImageDisplay("Aspirin", R.drawable.aspirin));
    myImageList.add(new ImageDisplay("Drug2", R.drawable.ekghomepic));
    myImageList.add(new ImageDisplay("Drug3", R.drawable.aspirin));
    myImageList.add(new ImageDisplay("Drug4", R.drawable.ekghomepic));

    //define ListView and create onItemClick Listener
    listview = (ListView) findViewById(R.id.obstetriclistview);

    ArrayAdapter<ImageDisplay> arrayAdapters = new ArrayAdapter<ImageDisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
    listview.setAdapter(arrayAdapters);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent myIntent = new Intent(context, imagedisplay.class);

            myIntent.putExtra("name", myImageList.get(position).getName());
            myIntent.putExtra("imagePath", myImageList.get(position).getPath());
            startActivityForResult(myIntent, 0);
        }
    } }
public class ImageDisplay {
    private String Name;
    private int Path; // use String if you use a path, in here i'm storing image in drawable

    public ImageDisplay() {
    }

    public ImageDisplay(String name, int path) {
        this.Name = name;
        this.Path = path;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getPath() {
        return Path;
    }

    public void setPath(int path) {
        this.Path = path;
    }
}

2 个答案:

答案 0 :(得分:1)

只需创建包含<ImageView/>标记的1个活动(或片段)即可。每当打开活动时,您都会在Intent内部发送图像src(如果使用Fragment,则发送到Bundle中)。

然后在此活动onCreate方法中,读取intent并将其放入ImageView src

编辑:尽管这不是使用ListView的最佳方法,但希望这会给你一些想法:

这是我的主要活动,它创建了ListView。我还创建了一个自定义对象类来处理Image资源ID和图像名称:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    final Context context = getApplicationContext();

    //list contains image name, image location
    final List<ImageDisplay> myImageList = new ArrayList<ImageDisplay>();
    myImageList.add(new ImageDisplay("Drug1", R.drawable.drug1));
    myImageList.add(new ImageDisplay("Drug2", R.drawable.drug2));
    myImageList.add(new ImageDisplay("Drug3", R.drawable.drug3));
    myImageList.add(new ImageDisplay("Drug4", R.drawable.drug4));

    //define ListView and create onItemClick Listener
    listview = (ListView)findViewById(R.id.druglistview);

    ArrayAdapter<ImageDisplay> arrayAdapters = new ArrayAdapter<ImageDisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
    listview.setAdapter(arrayAdapters);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent myIntent = new Intent(context, DisplayActivity.class);

            myIntent.putExtra("name", myImageList.get(position).getName());
            myIntent.putExtra("imagePath", myImageList.get(position).getPath());
            startActivityForResult(myIntent, 0);
        }
    });
}

public class ImageDisplay {
    private String Name;
    private int Path; // use String if you use a path, in here i'm storing image in drawable

    public ImageDisplay(){}
    public ImageDisplay(String name, int path){
        this.Name = name;
        this.Path = path;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getPath() {
        return Path;
    }

    public void setPath(int path) {
        this.Path = path;
    }
}

最后,这是您的展示活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    Intent myItent = getIntent();

    String Name = myItent.getStringExtra("name"); //Do whatever you need with image title
    int Path = myItent.getIntExtra("imagePath", 0); //pass this to ImageView

    imageView.setImageResource(Path);
}

答案 1 :(得分:0)

 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Intent in = new Intent(view.getContext(), aspirin.class);
            in.putExtra("name", (String)adapterView.getItemAtPosition(position));
            startActivityForResult(in, 0);
        }
    });
希望它有所帮助。诀窍是将信息与intent对象一起打包并在目标活动上处理它。检索字符串并显示图像。 Starting Activities