简体   繁体   English

从自定义适配器访问ArrayList

[英]Access ArrayList from Custom Adapter

What I am trying to do is access an ArrayList that is in my main activity from a customer adapter for a listview. 我想做的是从客户适配器访问ListView的主要活动中的ArrayList。

I have a listview that I am making clickable, which I have done within the customer adapter(where the onclick resides). 我有一个可单击的列表视图,该列表视图是在客户适配器(onclick所在的位置)内完成的。 When the user clicks it they will enter a caption(text) which will update an arraylist(string) in the main activity. 当用户单击它时,他们将输入标题(文本),该标题将更新主活动中的arraylist(字符串)。

I have read a few other questions about this topic but I'm lost on how to make this work. 我已经阅读了有关此主题的其他一些问题,但是我对如何使该工作迷失了方向。

I didn't see the need to post a code snippet because it's just a basic arraylist(string) and a custom adapter for a listview. 我认为不需要发布代码段,因为它只是基本的arraylist(string)和用于listview的自定义适配器。 If code is needed I can post though. 如果需要代码,我可以发布。 Thanks! 谢谢!

Here part of the adapter code: 这里是适配器代码的一部分:

public class PhotoAdapter extends ArrayAdapter<Photo> {

    private final ArrayList<Photo> objects;

    public PhotoAdapter(Context context, int textViewResourceId,
            ArrayList<Photo> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Photo i = objects.get(position);
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.listview_row, null);
                v.setClickable(true);
                v.setFocusable(true);
        }

Here is come code from the main activity 这是来自主要活动的代码

public ArrayList<Photo> m_photos = new ArrayList<Photo>();
public ArrayList<String> m_photo_captions = new ArrayList<String>();
private PhotoAdapter m_adapter;

this.list1 = (ListView) findViewById(R.id.lstPhotos);
        m_adapter = new PhotoAdapter(this, R.layout.listview_row, m_photos);
        LayoutInflater infalter = getLayoutInflater();
list1.setAdapter(m_adapter);

if (Intent.ACTION_SEND_MULTIPLE.equals(action)
                && intentGallery.hasExtra(Intent.EXTRA_STREAM)) {
            ArrayList<Parcelable> list = intentGallery
                    .getParcelableArrayListExtra(Intent.EXTRA_STREAM);
            for (Parcelable p : list) {
                Uri uri = (Uri) p;
                imagePath = getPath(uri);
                m_photos.add(new Photo(imagePath, ""));
                m_photo_locations.add(imagePath);
                m_photo_captions.add("");
                m_adapter.notifyDataSetChanged();
            }
        }

onclick listener onclick侦听器

    list1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // Do whatever you want with m_photo_captions here
            Log.v("listview", "listview item clicked");
            appErrorAlert("test", "test");
        }
    });

You can define the ArrayList you want to access as a global instance variable, that way you can access it from inside your custom adapter. 您可以将要访问的ArrayList定义为全局实例变量,这样就可以从自定义适配器内部访问它。

It'll also be helpful if you provided a code snippet for your problem. 如果您为问题提供了代码段,这也将有所帮助。

Now that you have posted your code, I would suggest a different approach than Mohamed A. Karim's. 既然您已经发布了代码,我将建议使用不同于Mohamed A. Karim的方法。


You are trying to set a simple OnClickListener on the entire row in your Adapter but access members of your Activity. 您试图在Adapter的整个行上设置一个简单的OnClickListener ,但是访问您的Activity成员。 Why not just set an OnItemClickListener in your ListView which already has access to the List that you want? 为什么OnItemClickListener在已经可以访问所需列表的ListView设置OnItemClickListener

list1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // Do whatever you want with m_photo_captions here
    }
});

Next remove everything that will intercept the touch event before it reaches your ListView . 接下来,删除所有将在touch事件到达ListView之前将其拦截的事件。 You can also make your getView() a little smaller and faster, like this: 您还可以使getView()变得更小,更快,如下所示:

public class PhotoAdapter extends ArrayAdapter<Photo> {
    LayoutInflater mInflater;

    public PhotoAdapter(Context context, int textViewResourceId,
            ArrayList<Photo> objects) {
        super(context, textViewResourceId, objects);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Photo i = get(position);
        View v = convertView;

        if (v == null) {
            v = mInflater.inflate(R.layout.listview_row, null);
            // Initialize your ViewHolder here!
        }

        // Update your TextViews, ImageViews, etc here
        ...
    }
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当ArrayList存储在扩展Application的类中时,如何从自定义适配器类访问ArrayList - How to access ArrayList from custom adapter class, when the ArrayList is stored in a class that extends Application 如何从适配器访问arrayList到活动? - How to access an arrayList from an adapter to an activity? 将文本视图从 ArrayList 动态添加到自定义适配器 - Dynamically add textviews to custom adapter from ArrayList 在自定义适配器中传递arrayList - passing arrayList in Custom adapter 在 Recyclerview 适配器中从 BaseFragment 访问 arraylist,无需复制代码 - Access arraylist from BaseFragment in a Recyclerview Adapter without duplicating the code 将图像作为ArrayList传递 <Bitmap> 从片段到自定义列表适配器 - Passing images as ArrayList<Bitmap> from Fragment to Custom List Adapter 如何将数组列表从活动传递到活动的自定义适配器 - how pass arraylist from activity to custom adapter of activity 使用适配器时如何访问片段 ArrayList? - How to access fragment ArrayList when using adapter? 使用SharedPreferences自定义ArrayList-适配器或编辑器错误? - Using SharedPreferences for custom ArrayList — Adapter or Editor error? Android将Arraylist数据设置为自定义适配器 - Android set Arraylist data to custom adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM