使用 ViewFlipper 以滑动视图显示数据库中的图像

Display the images in the database in sliding view using the ViewFlipper

我想将存储在数据库中的图像添加到ViewFlipper 的图像视图中。但是当我添加这些图像时,出现了一些错误。请帮我解决这个问题,或者让我知道是否有其他解决方案。这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    editImagename = findViewById(R.id.image_name);
    btnAddData = findViewById(R.id.addbtn);
    btnview = findViewById(R.id.getbtn);
    imgview = new ImageView(this);
    viewflips = findViewById(R.id.viewflip);
    viewAll();

    TextView txt = findViewById(R.id.text);
    txt.setSelected(true);

    Button buttonLoadImage = findViewById(R.id.btn1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

    myDb = new DatabaseHelper(this);
}

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        final String x = getPath(uri);
        final String ImageName = editImagename.getText().toString();

        btnAddData.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        boolean isInserted = myDb.insertData(ImageName, x);
                        if(isInserted == true)
                            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );

    }
}

private String getPath(Uri uri) {
    if (uri==null) return null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri,projection,null,null,null);
    if (cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

public void viewAll() {
    btnview.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    while(res.moveToNext()){
                        Uri mUri = Uri.parse(res.getString(2));
                        imgview.setImageURI(mUri);
                        viewflips.addView(imgview);
                    }
                }
            }
    );
}

}

DatabaseHelper.java

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    editImagename = findViewById(R.id.image_name);
    btnAddData = findViewById(R.id.addbtn);
    btnview = findViewById(R.id.getbtn);
    imgview = new ImageView(this);
    viewflips = findViewById(R.id.viewflip);
    viewAll();

    TextView txt = findViewById(R.id.text);
    txt.setSelected(true);

    VideoView videoView = findViewById(R.id.videoView);
    String videoPath = "android.resource://" + getPackageName() + "/raw/yeti";
    Uri uri = Uri.parse(videoPath);
    videoView.setVideoURI(uri);
    videoView.start();
    videoView.requestFocus();
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });

    Button buttonLoadImage = findViewById(R.id.btn1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

    myDb = new DatabaseHelper(this);
}

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        final String x = getPath(uri);
        final String ImageName = editImagename.getText().toString();

        btnAddData.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        boolean isInserted = myDb.insertData(ImageName, x);
                        if(isInserted == true)
                            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );

    }
}

private String getPath(Uri uri) {
    if (uri==null) return null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri,projection,null,null,null);
    if (cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

public void viewAll() {
    btnview.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    while(res.moveToNext()){
                        Uri mUri = Uri.parse(res.getString(2));
                        imgview.setImageURI(mUri);
                        viewflips.addView(imgview);
                    }
                }
            }
    );
}

}

我在 运行 这些代码后得到的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 15261
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
    at android.view.ViewGroup.addView(ViewGroup.java:4768)
    at android.widget.ViewAnimator.addView(ViewAnimator.java:183)
    at android.view.ViewGroup.addView(ViewGroup.java:4708)
    at android.view.ViewGroup.addView(ViewGroup.java:4681)
    at com.example.test.MainActivity.onClick(MainActivity.java:118)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

只需在 viewflips.addView(imgview) 中添加视图之前添加 viewflips.removeView()

当 ViewFlipper 已经有视图并且尝试在之前添加的视图上添加更多视图时会出现此错误。 因此,在添加视图之前,您始终必须 clear/remove 来自 ViewFlipper 的所有先前视图。 所以,添加

viewflips.removeViews();

就在

之前
viewflips.addView(imgview);

无处不在。