0

What I am trying to do is access an ArrayList that is in my main activity from a customer adapter for a listview.

I have a listview that I am making clickable, which I have done within the customer adapter(where the onclick resides). When the user clicks it they will enter a caption(text) which will update an arraylist(string) in the main activity.

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. 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

    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");
        }
    });
2
  • What specifically are you trying to do? Are you trying to pass in the value to the adapter, or something else? Commented Aug 28, 2012 at 2:10
  • Basically I have a listview which shows a photo thumbnail and caption text, I need to make the listview clickable(already done) and when clicked it creates a dialog box where the user can enter the caption. When the user clicks ok on the dialog it will update the arraylist in the main activity with the caption entered, then updates the listview to reflect the addition of the caption. Commented Aug 28, 2012 at 2:13

2 Answers 2

3

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.

It'll also be helpful if you provided a code snippet for your problem.

Sign up to request clarification or add additional context in comments.

Comments

1

Now that you have posted your code, I would suggest a different approach than Mohamed A. Karim's.


You are trying to set a simple OnClickListener on the entire row in your Adapter but access members of your Activity. Why not just set an OnItemClickListener in your ListView which already has access to the List that you want?

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. You can also make your getView() a little smaller and faster, like this:

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!

9 Comments

I get this error when I try your suggestion. The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new OnItemClickListener(){})
Have you imported the appropriate files? import android.widget.AdapterView; and import android.widget.AdapterView.OnItemClickListener; (In Eclipse just press Ctrl+Shift+O.)
Thanks, I assumed that Eclipse did it for me already.... we know what assuming does. Thanks!
So the code compiles but the app is not recognizing my click on the listview. Thoughts?
What do you mean by not recognizing? Please post your OnItemClickListener.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.