使用相机图像填充RecyclerView

时间:2017-07-03 15:55:51

标签: android android-recyclerview

在尝试从相机拍摄照片之后,我尝试用新行填充RecyclerView。显然我的解决方案不起作用,但我无法理解为什么。捕获后,没有显示任何内容。 这是我的四个.java文件: 1.主类 2.适配器 3. ViewHolder 4.模型

这是我认为错误所在的Main类的代码:

public class ComplaintActivity extends AppCompatActivity {

private static final int SELECT_PICTURE = 1;
private static final int CAMERA_REQUEST = 1888;

private Button btnGallery;
private Button btnCamera;
private ImageView imageView;

private String selectedImagePath;

private static RecyclerView recyclerView;

private static ComplaintAdapter adapter;

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

    btnCamera = (Button) this.findViewById(R.id.btnCamera);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewComplaint);
    adapter = new ComplaintAdapter(this, new ArrayList<ComplaintModel>());
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        loadRecyclerView(photo);
    }
}

private void loadRecyclerView(Bitmap image){

    ComplaintModel data = new ComplaintModel();
    ArrayList<ComplaintModel> notificationsList = new ArrayList<>();

    data.setId(1);
    data.setUsername("Try");
    data.setText("Try");
    data.setDate("05-07-2017");
    data.setTime("Prova");

    notificationsList.add(data);

    adapter = new ComplaintAdapter(this, notificationsList);
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
}

/**
 * helper to retrieve the path of an image URI
 */
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    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();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

@Override
public void onBackPressed() {
    super.onBackPressed();

    overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}

这是XML布局(专注于按钮和RecyclerView):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/White"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="20dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">

                <Button
                    android:id="@+id/btnCamera"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_round"
                    android:text="Capture"
                    android:textColor="@color/White" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_round"
                    android:text="Gallery"
                    android:textColor="@color/White" />
            </LinearLayout>

        </LinearLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="my.axicura.app.activity.Complaint.ComplaintActivity">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewDenuncia"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>

    </LinearLayout>
</ScrollView>

感谢您的帮助!

0 个答案:

没有答案