How to use on click event button in extend baseadapter.. I tried a lot but no use.. In my project there is custom listview, it contain text,button(btnlist), fastscroll index. when i onclick button(btnlist) it is not gng to other activity, no error is also displaying,no toast..
Plz help me with example . thank u in advance.
Quick ref: getview—> holder.btnList.setOnClickListener
EfficientAdapter.java
public class EfficientAdapter extends BaseAdapter implements SectionIndexer, OnClickListener {
IndexableListView mListView;
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;
private Intent intent;
private Patient patient;
private LayoutInflater mInflater;
private Context context;
private int positions;
ViewHolder holder;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
String patientListJson = CountriesList.jsonData;
JSONObject jssson;
try {
jssson = new JSONObject(patientListJson);
patientListJson = jssson.getString("PostPatientDetailResult");
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
patientListArray = new ArrayList<Patient>();
for (JsonElement obj : Jarray) {
Patient patientList = gson.fromJson(obj, Patient.class);
patientListArray.add(patientList);
// Log.i("patientList", patientListJson);
}
}
public int getCount() {
return patientListArray.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
this.positions = position;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.homemplebrowview, parent, false);
holder = new ViewHolder();
holder.text1 = (TextView) rowView.findViewById(R.id.name);
holder.text2 = (TextView) rowView.findViewById(R.id.mrn);
holder.text3 = (TextView) rowView.findViewById(R.id.date);
holder.text4 = (TextView) rowView.findViewById(R.id.age);
holder.text5 = (TextView) rowView.findViewById(R.id.gender);
holder.text6 = (TextView) rowView.findViewById(R.id.wardno);
holder.text7 = (TextView) rowView.findViewById(R.id.roomno);
holder.text8 = (TextView) rowView.findViewById(R.id.bedno);
holder.btnList = (Button) rowView.findViewById(R.id.listbutton);
/*Button editButton = (Button) rowView.findViewById(R.id.listbutton) ;
editButton.setTag(position);
editButton.setClickable(true);
editButton.setOnClickListener(EfficientAdapter.this);
rowView.setClickable(true);*/
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.text1.setText(Util.formatN2H(patientListArray.get(position)
.getName()));
holder.text2.setText(patientListArray.get(position).getMrnNumber());
holder.text3.setText(Util.formatN2H(patientListArray.get(position)
.getRoom()));
holder.text4.setText(Util.formatN2H(patientListArray.get(position)
.getAge()));
holder.text5.setText(Util.formatN2H(patientListArray.get(position)
.getGender()));
holder.text6.setText(Util.formatN2H(patientListArray.get(position)
.getWard()));
holder.text7.setText(Util.formatN2H(patientListArray.get(position)
.getRoom()));
holder.text8.setText(Util.formatN2H(patientListArray.get(position)
.getBed()));
holder.btnList.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, SeviceDetails.class);
Log.i("patient", " next "+ position + " onclickposition " + patientListArray.get(position).getMrnNumber());
patient = getPatientDetailsByMrn(patientListArray, position);
Log.i("DDDD ", patient.getMrnNumber());
next.putExtra("patient", patient);
next.putExtra("position", position);
System.out.println("patient"+ patient);
context.startActivity(next);
}
});
return rowView;
}
static class ViewHolder {
public Button btnList;
public TextView text8;
public TextView text7;
public TextView text6;
public TextView text5;
public TextView text4;
public TextView text1;
public TextView text2;
public TextView text3;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public int getPositionForSection(int section) {
sortMyData();
Log.i("getPositionForSection", "section" + section);
// If there is no item for current section, previous section will be
// selected
for (int i = section; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
Log.i("getPositionForSection- i", "section" + i);
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(
String.valueOf(patientListArray.get(j)
.getName().charAt(0)),
String.valueOf(k)))
Log.i("getPositionForSection- j", "section" + j);
return j;
}
} else {
if (StringMatcher.match(
String.valueOf(patientListArray.get(j).getName()
.charAt(0)),
String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
/**
* sorting the patientListArray data
*/
public void sortMyData() {
// sorting the patientListArray data
Collections.sort(patientListArray, new Comparator<Object>() {
@Override
public int compare(Object k1, Object k2) {
Patient p1 = (Patient) k1;
Patient p2 = (Patient) k2;
return p1.getName().compareToIgnoreCase(p2.getName());
}
});
}
}
.xml
<Button
android:id="@+id/listbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/name"
android:layout_marginRight="43dp"
android:focusable="false"
android:text="Episode"
android:textColor="#666666" />
default listview
<ListView
android:id="@+id/homelistView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="0dip" />
You should use your Adapter to populate the list with the items. Then in your Activity or Fragment you should implement the
onListItemClickevent to handle the click on the list item.EDIT: I am posting an example below where I am using a
ListFragment– this is the piece of code I have available right now, I can post something specific toListViewlater:Then in your
ListFragment:For details on
ListFragmentsee here.I think that a very nice example (although by extending
ArrayAdapterinstead ofBaseAdapteris hown in this stackoverflow answer.There is one point to remember:
android:focusable="false"– like you have in your XML).onItemClickevent handled as shown below (extract from the link above):Code:
Finally, note one point. This solution means that you click on the list row and the event gets fired.
I think that if you want the user to click on the button and the event to get fired on the button, then you need to set call
listview.setItemsCanFocus(true)right after you inflate your ListView XML and make sure that the Button isfocusablein the XML. Listening for theonClickevent should work then.This is described in Slide 25 of the GoogleIO World of ListView presentation (you can get it here and the video is here)
Hope this helps