我有一個列表視圖圖像。我想更改列表項目圖像onActivityResult
圖像捕獲。我得到的圖像文件位圖,但當我更改列表項目圖像的列表視圖圖像不會改變。動態改變從內存Android的列表視圖圖像
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imageView1.setImageBitmap(bitmap);//there a bitmap exist---
dataAdapter.taskList.get(0).setImgBitmap(bitmap);
//這是不會改變列表image.why ??????? dataAdapter.notifyDataSetChanged(); 這裏的imageview1
有一個位圖,我使用CustomAdapter
類的ArrayList<Task> taskList
參數將它設置爲列表的第一項。
這裏完整的示例代碼
NotifyAcitivity.java
package com.aci.notification;
public class NotifyActivity extends Activity {
// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; // file url to store image/video
private ImageView imageView1;
Bitmap bm, bm2;
private Button btnCapturePicture;
ArrayList<Task> taskList = new ArrayList<Task>();
TextView tv;
CustomAdapter dataAdapter = null;
Button btnSave;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
imageView1 = (ImageView) findViewById(R.id.imageView1);
tv = (TextView) findViewById(R.id.textViewtest);
btnSave = (Button) findViewById(R.id.button1);
listView = (ListView) findViewById(R.id.listView1);
new BitmapFactory();
bm = BitmapFactory.decodeResource(getApplicationContext()
.getResources(), R.drawable.ic_launcher);
bm2 = BitmapFactory.decodeResource(getApplicationContext()
.getResources(), R.drawable.bydefault);
Task task;
task = new Task(0, "This is task1", "/pictures/hello camera/", bm,false);
taskList.add(task);
task = new Task(1, "This is task2", "/pictures/hello camera/", bm,false);
taskList.add(task);
task = new Task(0, "This is task3", "/pictures/hello camera/", bm,false);
taskList.add(task);
task = new Task(0, "This is task4", "/pictures/hello camera/", bm2,false);
taskList.add(task);
task = new Task(0, "This is task5", "/pictures/hello camera/", bm,false);
taskList.add(task);
dataAdapter = new CustomAdapter(this, R.layout.list_style, taskList);
listView.setAdapter(dataAdapter);
}
/**
* Receiving activity result method will be called after closing the camera
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
}
}
}
/**
* Display image from a path to ImageView
*/
private void previewCapturedImage() {
try {
imageView1.setVisibility(View.VISIBLE);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imageView1.setImageBitmap(bitmap);
dataAdapter.taskList.get(0).setImgBitmap(bitmap);
dataAdapter.notifyDataSetChanged();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<Task> {
public ArrayList<Task> taskList;
Context context;
public CustomAdapter(Context context, int resource, ArrayList<Task> taskList) {
super(context, resource, taskList);
this.context=context;
// TODO Auto-generated constructor stub
this.taskList = new ArrayList<Task>();
this.taskList.addAll(taskList);
}
private class ViewHolder {
CheckBox task;
ImageView imageView;
int task_id;
Button imageCaptureBtnButton;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_style, null);
viewHolder = new ViewHolder();
viewHolder.task_id=taskList.get(position).getTask_id();//set task id of that task
viewHolder.task = (CheckBox) convertView.findViewById(R.id.checkBox1);
viewHolder.imageView=(ImageView) convertView.findViewById(R.id.imgPreview);
viewHolder.imageCaptureBtnButton=(Button) convertView.findViewById(R.id.btnCapturePicture);
viewHolder.imageView.setImageBitmap(taskList.get(position).getImgBitmap());
convertView.setTag(viewHolder);
viewHolder.task.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Task aTask =(Task) cb.getTag();
Toast.makeText(context,
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
aTask.setSelected(cb.isChecked());
}
});
viewHolder.imageCaptureBtnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "dddd", Toast.LENGTH_LONG).show();
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Task aTask = taskList.get(position);
viewHolder.task.setText(taskList.get(position).getTask());
viewHolder.task.setChecked(taskList.get(position).isSelected());
viewHolder.task.setTag(aTask);
return convertView;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public int getCount() {
return taskList.size();
}
@Override
public Task getItem(int arg0) {
return null;
}
}
Task.java
public class Task {
int task_id = 0;
String task = null;
String imageURL=null;
Bitmap imgBitmap;
boolean selected=false;
public Task(int task_id, String task, String imageURL,Bitmap imgBitmap, boolean selected) {
super();
this.task_id = task_id;
this.task= task;
this.imageURL=imageURL;
this.imgBitmap=imgBitmap;
this.selected = selected;
}
//all getters and setters here
}
我已經失去了我的日子。請幫助我。