我是编程新手,我试图使用相机拍摄的图像填充RecyclerView。每当我拍摄新照片时,我都希望被添加到RecyclerView并更新RecyclerView。
RecyclerView适用于TextViews(例如),但它不会显示Imageview。我认为当我尝试获取捕获的图像的URI时会出现问题。
我还没有为这个主题找到太多解决方案,所以我要问你们。
这是模型类。
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <string>
#pragma comment(lib, "comctl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK RichEditProc(HWND, UINT, WPARAM, LPARAM);
WNDPROC richEditOrigProc;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LoadLibrary(TEXT("msftedit.dll"));
WNDCLASSEX mainwcex;
mainwcex.cbSize = sizeof(WNDCLASSEX);
mainwcex.style = CS_HREDRAW | CS_VREDRAW;
mainwcex.lpfnWndProc = WindowProc;
mainwcex.cbClsExtra = 0;
mainwcex.cbWndExtra = 0;
mainwcex.hInstance = hInstance;
mainwcex.hIcon = NULL;
mainwcex.hCursor = (HICON)LoadCursor(NULL, IDC_ARROW);
mainwcex.hbrBackground = GetSysColorBrush(COLOR_MENU);
mainwcex.lpszMenuName = NULL;
mainwcex.lpszClassName = "mainwindow";
mainwcex.hIconSm = NULL;
RegisterClassEx(&mainwcex);
HWND mainWindow = CreateWindowEx(
NULL,
"mainwindow",
NULL,
WS_OVERLAPPEDWINDOW,
100,
100,
600,
400,
NULL,
NULL,
hInstance,
NULL);
HWND richEditControl = CreateWindowEx(
NULL,
"RICHEDIT50W",
"Rich Edit",
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
50,
50,
100,
25,
mainWindow,
NULL,
hInstance,
NULL);
richEditOrigProc = (WNDPROC) SetWindowLongPtr(richEditControl, GWLP_WNDPROC, (LONG_PTR) RichEditProc);
// Changes the width of rich edit control from 100 px to 400 px.
SetWindowPos(richEditControl, NULL, 0, 0, 400, 25,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
ShowWindow(mainWindow, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (!IsDialogMessage(mainWindow, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK RichEditProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_NCCALCSIZE:
{
if (wParam) {
NCCALCSIZE_PARAMS* ncParams = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
ncParams->rgrc[0].left += 50;
ncParams->rgrc[0].top += 3;
ncParams->rgrc[0].right -= 50;
ncParams->rgrc[0].bottom -= 3;
return WVR_HREDRAW;
}
return 0;
}
}
return CallWindowProc(richEditOrigProc, hWnd, uMsg, wParam, lParam);
}
这是适配器。
public class Attachment {
private Uri image;
private String text;
public Attachment(Uri image, String text) {
this.image = image;
this.text = text;
}
public Uri getImage() {
return image;
}
public String getText(){
return text;
}
我在这里传递了意图。
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Context context;
private List<Attachment> lista;
private LayoutInflater layoutInflater;
public ListViewAdapter(List<Attachment> lista, Context context) {
this.lista = lista;
this.context = context;
layoutInflater=LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutInflater.inflate(R.layout.listview_layout, parent, false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ListViewAdapter.ViewHolder holder, int position) {
holder.imageView.setImageURI(lista.get(position).getImage());
holder.textView.setText(lista.get(position).getText());
}
@Override
public int getItemCount() {
return lista.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.listviewAttachmentImageView);
textView=itemView.findViewById(R.id.textView);
}
}
public void update(List<Attachment> newList) {
lista.clear();
lista.addAll(newList);
notifyDataSetChanged(); }
这里我初始化RecyclerView。
buttonAddAttachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager())!=null){
startActivityForResult(intent,CAM_REQUEST);
}
}
});
这是onActivityResult方法(单击相机按钮的结果)。
list=new ArrayList<>();
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
adapter=new ListViewAdapter(list, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.update(list);
答案 0 :(得分:0)
在“附件”模型中,将图像更改为类型Uri
public class Attachment {
private Uri image;
public Attachment(Uri image) { this.image = image; }
public Uri getImage() { return image; }
在if语句之后的onActivityResult中执行
Uri uri = intent.getData();
Attachment att = new Attachment (uri);
List<Attachment> list = new ArrayList<>();
list.add(att);
.... = New ListViewAdapter(list, context);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
Uri uri = data.getData();
Attachment att = new Attachment (uri);
List<Attachment> list = new ArrayList<>();
list.add(att);
adapter=new ListViewAdapter(list,this);
recyclerView.setAdapter(adapter);
}}
不要设置imageView。
在你的适配器类onBind
中holder.imageView.setImageUri(lista.get(position).getImage());
答案 1 :(得分:0)
添加recyclerview.setAdapter(适配器);到你的onActivityResult