从 table 布局上传多张图片作为可点击的 ImageView

Uploading multiple images as clickable ImageViews from table layout

我是编程新手 android。我正在构建一个应用程序,允许用户通过单击显示在 TableLayout 中的 ImageViews 来上传多张图像。我遇到的问题是如何在 onStartActivity 方法中设置每个选定图像的位图。

我可以使用 setImageBitmap 将单个图像设置为 imageiew,但我不确定如何循环或以编程方式确定在 [=15= 中选择了哪个 ImageView ] 以便在其中显示图像。我创建了一个 ImageView 数组并将 ImageViews 分配给它,但我尝试使用该数组时没有显示任何图像。我还尝试在 startActivityForResult 之后通过每个 onclicklistener 中的 setImageBitmap 方法显示 ImageView - 它仅在再次选择 ImageView 后显示,正如预期的那样。

我怎样才能使这项工作?这是我的第一个post。这是我的代码:

下面的XML布局。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical">

    <TextView
        android:id="@+id/postAdCommand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingBottom="10dp"
        android:text="Post An Ad"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title:"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/etTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/tvDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Description:"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/etDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvImages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingBottom="10dp"
        android:text="Upload Images:"
        android:textColor="#ffffff" />

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">
            <TableRow>
                <ImageView
                    android:id="@+id/imgImages"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:clickable="true"
                    android:src="@drawable/add_image" />

                <ImageView
                    android:id="@+id/imgImages1"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />

                <ImageView
                    android:id="@+id/imgImages2"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />
            </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imgImages3"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />

                <ImageView
                    android:id="@+id/imgImages4"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />

                <ImageView
                    android:id="@+id/imgImages5"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />

            </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/imgImages6"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />
                <ImageView
                    android:id="@+id/imgImages7"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />

                <ImageView
                    android:id="@+id/imgImages8"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="2dp"
                    android:background="#1f1f14" />
            </TableRow>
            <Button
                android:id="@+id/bImages"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Upload Images"
                android:textColor="#000000" />
        </TableLayout>
        <TextView
           android:id="@+id/tvVideos"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:paddingBottom="10dp"
           android:paddingTop="10dp"
           android:text="Upload Images:"
           android:textColor="#ffffff" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">

        <VideoView
            android:id="@+id/vidVideo"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:background="@drawable/back_button_gray"
            android:clickable="true" />

        <VideoView
            android:id="@+id/vidVideo1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:background="@drawable/back_button_gray"
            android:clickable="true" />

        <VideoView
            android:id="@+id/vidVideo2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="2dp"
            android:background="@drawable/back_button_gray"
            android:clickable="true" />
    </LinearLayout>

    <Button
        android:id="@+id/bVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Upload Video"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tvPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price:"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/etPrice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bPostAd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Post Your Advertisement" />
</LinearLayout>
</ScrollView>

然后是Java代码。

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class PostAd extends AppCompatActivity implements View.OnClickListener {

Button imageUpload;
Button vidUpload;
Button postAd;

ImageView img1, img2, img3, img4, img5, img6, img7, img8, img9;

public static final int SELECT_IMAGE = 1;
private Uri selectedImagePath;
public String selectedFileManager;
public String selectedFilePath;
Bitmap bitmap;


public ImageView[] position = {img1, img2, img3, img4, img5, img6, img7, img8, img9};

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


    initializeVariables();
    initOnclickListeners();


}


private void initializeVariables() {
    imageUpload = (Button) findViewById(R.id.bImages);
    vidUpload = (Button) findViewById(R.id.bVideo);
    postAd = (Button) findViewById(R.id.bPostAd);

    img1 = (ImageView) findViewById(R.id.imgImages);
    img2 = (ImageView) findViewById(R.id.imgImages1);
    img3 = (ImageView) findViewById(R.id.imgImages2);
    img4 = (ImageView) findViewById(R.id.imgImages3);
    img5 = (ImageView) findViewById(R.id.imgImages4);
    img6 = (ImageView) findViewById(R.id.imgImages5);
    img7 = (ImageView) findViewById(R.id.imgImages6);
    img8 = (ImageView) findViewById(R.id.imgImages7);
    img9 = (ImageView) findViewById(R.id.imgImages8);

}

private void initOnclickListeners() {
    imageUpload.setOnClickListener(this);
    vidUpload.setOnClickListener(this);
    postAd.setOnClickListener(this);

    img1.setOnClickListener(this);
    img2.setOnClickListener(this);
    img3.setOnClickListener(this);
    img4.setOnClickListener(this);
    img5.setOnClickListener(this);
    img6.setOnClickListener(this);
    img7.setOnClickListener(this);
    img8.setOnClickListener(this);
    img9.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.bImages:

            break;
        case R.id.bVideo:

            break;
        case R.id.bPostAd:

            break;

        case R.id.imgImages:
            Intent image = new Intent();
            image.setType("image/*");
            image.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image, SELECT_IMAGE);
            break;

        case R.id.imgImages1:
            Intent image1 = new Intent();
            image1.setType("image/*");
            image1.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image1, SELECT_IMAGE);
            break;

        case R.id.imgImages2:
            Intent image2 = new Intent();
            image2.setType("image/*");
            image2.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image2, SELECT_IMAGE);
            break;

        case R.id.imgImages3:
            Intent image3 = new Intent();
            image3.setType("image/*");
            image3.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image3, SELECT_IMAGE);
            break;

        case R.id.imgImages4:
            Intent image4 = new Intent();
            image4.setType("image/*");
            image4.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image4, SELECT_IMAGE);
            break;

        case R.id.imgImages5:
            Intent image5 = new Intent();
            image5.setType("image/*");
            image5.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image5, SELECT_IMAGE);
            break;

        case R.id.imgImages6:
            Intent image6 = new Intent();
            image6.setType("image/*");
            image6.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image6, SELECT_IMAGE);
            break;

        case R.id.imgImages7:
            Intent image7 = new Intent();
            image7.setType("image/*");
            image7.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image7, SELECT_IMAGE);
            break;

        case R.id.imgImages8:
            Intent image8 = new Intent();
            image8.setType("image/*");
            image8.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(image8, SELECT_IMAGE);
            break;
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK) {

        try {

            selectedImagePath = data.getData();

            selectedFileManager = selectedImagePath.getPath();

            selectedFilePath = getPath(selectedImagePath);

            bitmap = BitmapFactory.decodeFile(selectedFilePath);

            if (position.length == 0) {
                position[0].setImageBitmap(bitmap);
            } else if (position.length == 1) {
                position[1].setImageBitmap(bitmap);
            } else if (position.length == 2) {
                position[2].setImageBitmap(bitmap);
            } else if (position.length == 3) {
                position[3].setImageBitmap(bitmap);
            } else if (position.length == 4) {
                position[4].setImageBitmap(bitmap);
            } else if (position.length == 5) {
                position[5].setImageBitmap(bitmap);
            } else if (position.length == 6) {
                position[6].setImageBitmap(bitmap);
            } else if (position.length == 7) {
                position[7].setImageBitmap(bitmap);
            } else if (position.length == 8) {
                position[8].setImageBitmap(bitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};

    Cursor c = getContentResolver().query(uri, projection, null, null, null, null);
    if (c != null) {
        int column_index = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        c.moveToFirst();
        return c.getString(column_index);

    } else return null;
}
}

在你的情况下,你需要跟踪每个图像视图的位置,而不是使用数组

imageView.setTag(位置)

imageView.getTag(位置)

基于它你可以设置你的逻辑。