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

Posted

技术标签:

【中文标题】如何使用从图库中选择的新图像更新列表视图 [重复]【英文标题】:how to update listview with a new image selected from gallery [duplicate] 【发布时间】:2014-07-15 20:31:03 【问题描述】:

我有一个应用程序,我想在其中使用从中选择的图像更新我的列表视图 当我单击列表视图行上的按钮时,设备库。但我无法在列表视图中显示它。 要在 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;
     
 

【问题讨论】:

【参考方案1】:

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于如何使用从图库中选择的新图像更新列表视图 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何获得关于画廊应用程序可见的每个新图像的通知?

android:从图库中选择图像,然后裁剪并显示在图像视图中

从图库中选择图像后如何打开下一个视图控制器(swift)

从手机图库中获取随机图片并在视图中显示

Android:如何在设置图像视图时检测从图库中选择的图像方向(纵向或横向)?

如何从图库中加载图像视图中的图像?