I have an application in which i want to update my list view with image selected from device gallery when i clicked on the button present on the list view row. But I'm unable to show it on the list view. what are the changes need to do to get the image on listview. Here is my code.
public class MainActivity extends Activity {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;
Bitmap bmp;
int posi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add image and text in arraylist
imageArry.add(new Contact(R.drawable.ic_launcher, "FaceBook"));
imageArry.add(new Contact(R.drawable.ic_launcher, "Google"));
imageArry.add(new Contact(R.drawable.ic_launcher, "Ical"));
imageArry.add(new Contact(R.drawable.ic_launcher, "Outlook"));
imageArry.add(new Contact(R.drawable.ic_launcher, "Twitter"));
// add data in contact image adapter
adapter = new ContactImageAdapter(this, R.layout.fragment_main, imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
}
public void removeAtomPayOnClickHandler(View v) {
Contact itemToRemove = (Contact)v.getTag();
//adapter.remove(itemToRemove);
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 0);
//adapter.insert(new AtomPayment("bbbb", 0), 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
posi=ContactImageAdapter.pos;
if ( 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();
// ImageView iv= (ImageView) findViewById(R.id.imageView2);
bmp= BitmapFactory.decodeFile(picturePath);
//iv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageArry.add(new Contact(R.drawable.ic_launcher, "Twitter"));
System.out.println("aaaaaaaaaaaaaaaaaaaaaa"+ContactImageAdapter.pos+"-----"+posi);
}
}
}
and here is the code for contact.java
public class Contact {
int image;
String name;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contact(int image, String name) {
super();
this.image = image;
this.name = name;
}
}
and the adapter class
public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
public static int pos;
int layoutResourceId;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
pos=position;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
int outImage=myImage.image;
holder.imgIcon.setImageResource(outImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}