我有一个列表视图,每行都有一个图像和一个图像按钮。当我们点击该按钮时,它应该从设备的图库中选择一个图像,并且应该替换该特定行的图像。
我可以从图库中选择图像,但它正在替换最后一行的图像而不是
替换我们按下按钮的行的图像。
请为我提供一些解决方案
这里我附上了代码
public class FirstActivity extends Activity {
String[] names;
CustomAdapter adapter;
private static int RESULT_LOAD_IMAGE = 1;
ListView lv;
int a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Resources res = getResources();
names = res.getStringArray(R.array.names);
adapter = new CustomAdapter(this, names);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
public void removeAtomPayOnClickHandler(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
a=lv.getPositionForView(lv);
Toast.makeText(this, "---"+a, Toast.LENGTH_SHORT).show();
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
CHolder.tv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
// ImageView imageView = (ImageView) findViewById(R.id.imgView);
//imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
和适配器类的代码
public class CustomAdapter extends ArrayAdapter<String>{
Context context;
String[] input;
public CustomAdapter(Context c, String[] values) {
super(c, R.layout.atom_pay_list_item, values);
this.context = c;
this.input = values;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
CHolder holder = null;
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.atom_pay_list_item, parent, false);
holder = new CHolder(row);
row.setTag(holder);
}
else{
holder = (CHolder) row.getTag();
}
//holder.tv.setImageDrawable(drawable)(input[position]);
///iv.setImageDrawable(R.drawable.abc_ic_search);
return row;
}
static class CHolder{
public static ImageView tv;
public static ImageButton iv;
CHolder(View v)
{
tv = (ImageView) v.findViewById(R.id.textview);
iv = (ImageButton) v.findViewById(R.id.imageview);
}
}
}
答案 0 :(得分:0)
在实际的适配器中,您可以为图像添加一个单击侦听器,然后从那里对其进行操作。例如,
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.atom_pay_list_item, parent, false);
holder = new CHolder(row);
row.setTag(holder);
// Add this in
iv.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
v.setImageResource(R.id.new_image_id);
// could obviously be more complex, like your onActivityResult() functionality
}
}
}
...
return row;
如果您需要点击在您的接收器中注册,您可以向您的活动发送广播。换句话说,而不是v.setImageResource(R.id.new_image_id);
Intent intent = new Intent("UPDATE_IMAGE");
intent.putExtra("clicked_position", position);
sendBroadcast(intent);
然后在您的活动中,将registerReceiver, new UpdateReceiver(), new IntentFilter("UPDATE_IMAGE");
放入您的onCreate活动方法之后。
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
intent.getExtra("clicked_position", -1);
int positioin = intent.getExtras().getInt("clicked_position", -1);
// do what you need to with your clicked image given its position
// basically do onActivityResult() functionality here
}
}
只需确保在活动的onDestroy中取消注册您的接收器,以避免泄漏。