如何使用从库中选择的新图像更新列表视图

时间:2014-05-27 03:42:59

标签: android listview onactivityresult

我有一个应用程序,其中我想要更新我的列表视图与选择的图像 单击列表视图行上的按钮时的设备库。但是我无法在列表视图中显示它。 要在listview上获取图像需要做哪些更改。 这是我的代码。

    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);
        }


    }
}

这是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;
 }

}

和适配器类

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;
     }
 }

2 个答案:

答案 0 :(得分:0)

每次更新列表时都使用notifyDataSetChanged。然后notifyDataSetChanged将起作用。 请参阅this

答案 1 :(得分:0)

在图像数组中添加新图像...并调用notifyDataSetChanged..it将刷新列表视图 请参阅此链接,详细解释Android. How does notifyDataSetChanged() method and ListViews work?