0

My app has ListView with children TextView and Button. (Refer to image)

enter image description here

How can i get position of button if it is clicked ?

Adapter

public class HistoryAdapter extends CursorAdapter {
public HistoryAdapter(Context context, Cursor c, int flags) {
    super(context,c, flags);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    TextView text = (TextView) view.findViewById(R.id.text);
    String body = cursor.getString(cursor.getColumnIndex("name"));
    text.setText(body);
}

}

ListView onClick event

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setCancelable(false);
    getDialog().setTitle("History");
    View view = inflater.inflate(R.layout.history,null);
    back = (Button) view.findViewById(R.id.button);
    back.setOnClickListener(this);
    list = (ListView) view.findViewById(R.id.listView);
    ContentResolver resolver = getActivity().getContentResolver();
    c = resolver.query(Contract.SaveRunning.CONTENT_URI, null, null, null, null);
    adapter = new HistoryAdapter(getActivity(), c, 0);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            setChart(position);
            dismiss();
        }
    });
    return view ;
}
dazzieta
  • 634
  • 4
  • 19
Error
  • 810
  • 1
  • 10
  • 32
  • what you want exactly, do you want to set different action on differrent child click – Sandeep Londhe Aug 22 '15 at 16:36
  • 1
    yes, each view has different action – Error Aug 22 '15 at 16:37
  • 1
    Hello @Error have you tried `list.getItemAtPosition(position);` to get the current position (Clicked) and if you want it as String you can add .toString(). let me know if it worked :P – Skizo-ozᴉʞS Aug 29 '15 at 12:13
  • 1
    hi @Skizo i handled this issue from Adapter class to each view it works now... i do not understand your methode , how use list.getItemAtPosition(position) return certain view ? – Error Aug 29 '15 at 16:04
  • I thought you wanted to get the position... to get the view you can do list.getChildAt(position); to get the view – Skizo-ozᴉʞS Aug 29 '15 at 19:41
  • 1
    Also you con check this[answer](http://stackoverflow.com/a/24864536/4385913) to get the view – Skizo-ozᴉʞS Aug 29 '15 at 19:43
  • 1
    @Skizo this answer looks useful .. i also can get each view and set itemClickListener from adapter Class .. thanks Skizo for help :) – Error Aug 29 '15 at 20:39
  • @Error Oh, okay, but I'm asking to you because your answer must be marked as a correct... so you should mark any question as a correct, do you want me to put my answer? – Skizo-ozᴉʞS Aug 29 '15 at 20:45

3 Answers3

1

To get the view you can do it as follows :

list.getChildAt(position);  //To get the view

Also you could do like this guy does:

public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

if (pos < firstListItemPosition || pos > lastListItemPosition ) {
    return listView.getAdapter().getView(pos, null, listView);
} else {
    final int childIndex = pos - firstListItemPosition;
    return listView.getChildAt(childIndex);
}

This also would work.

Community
  • 1
  • 1
Skizo-ozᴉʞS
  • 16,233
  • 16
  • 66
  • 129
0

You Can use something like this inside your onClick method to display item postion:

String value = lv.getItemAtPosition(position).toString();

Where lv is ListView.

Sandeep Londhe
  • 400
  • 1
  • 5
  • 16
0

I know this question is old but a good solution to this question is:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ixChild = ExpandableListView.getPackedPositionChild( id );
        ixGroup = ExpandableListView.getPackedPositionGroup( id );
        setChart(position);
        dismiss();
    }

In this way, ixGroup contains the index of the clicked group and the ixChild contains the index of the child within the group.