1

I have been developing an android application and i am stuck in the middle.

The problem is i am using SimpleAdapter to do the adapter stuff and show items in the Listview and as far as i know i cannot override the getView() method of SimpleAdapter class to bound click listeners to the items.

There is a other way to handle click events of sub items like using XML, you can write in the XML like android:clickable="true"and android:onClick="clicklistenr", using this i can get the item but my problem is if i use this then i cannot get the position of the adapter which i need to get adapter item values and handle other tasks. So i am stuck here any help would be appreciable. thanks.

For example i have a ListView which contains one image, TextView, like Button, share Button in each of its items. And there is no way i can find that either its image or button clicked using setOnItemClickListener. So i need a way to handle click events of these sub items of a ListView, i am using SimpleAdapter.

0

4 Answers 4

1

Just call listView.setOnItemClickListener() with your implementation of the listener.

and use like

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

Where list=(ListView)findViewById(R.id.list); and list.setAdapter(your_adapter);

For More details Follow: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Hope It Will Help You.. :)

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

Comments

0

You can use like

 myListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
            String result= (String) obj.get("name");
            Log.d("Yourtag", name);
    }
});

4 Comments

i can do this sir, but how can i get the sub items of this view? i want to handle events of subitems of the view not the main item of LitView Class.
what would i do with these links, i know what is expendable list, and it has nothing to do with my question, stop posting un relevant links pls...
0

First of all your problem is how to handle the items of a custom listview.. not sub. Anyways...

If you are using SimpleAdapter , you may use getView().But if you are using SimpleAdapter you don't need to use the getView() as the it handles the mapping of data to your layout via the resource. For inforamtion check the SimpleAdapter in developer site.

Another thing is there is not mandatory for using any particular adapter. You can create any adapter class which will extends the BaseAdapter which will implement the getView().

Inside getView() you can able to inflate your custom layout(which contains the image,button etc..) to your view or convertview.something like:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.yourcustomlayout, null);

            //Initialize your custom views here
            TextView someText= (TextView) vi.findViewById(R.id.tvSometext);
            Button likeButton = (Button ) vi.findViewById(R.id.btnLike);

            //put data or call onclicklistener or 
            //whatever you want to do with custom views
            likeButton .setOnClickListener(...){
             //on click
            }
       }

Ans simply call this through your constructor with some data and appropriate context.

1 Comment

I know this sir and i already mentioned it in my question, i need a way to handle sub items click events using SimpleAdapter. As in SimpleAdapter i cannot call the getView() method to do so. thanks for your answer, but its not what i require.
0
Amir,
I am not sure you found the solution or not. You can do this way:


@Override
public View getView(int position, View view, ViewGroup viewGroup) {
  //for each subitem, set click listener & set tag having position as
  // value.
  ImageView imv = (ImageView)view.findViewById(R.id.live_view);
  imv.setTag(i);
  imv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("INFO","Live view clicked");
                    //This will give you position in listview.
                    int position = (int) v.getTag();
                }
  });

 }

I hope it will help you and others looking to find relevant answer. May be there is some other solution too, but it will surely work.

Comments

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.