My app has ListView
with children TextView
and Button
. (Refer to image)
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 ;
}