This is my Custom List Adapter Class :
class MyAdapter extends ArrayAdapter<String> {
private int resourceId;
String name="";
String phone;
private ArrayList<String> sites = null;
private Context context;
private static class ViewHolder{
TextView RideDetails;
EditText mEdit;
}
public MyAdapter(Context context, int resource, ArrayList<String> objects,String phone) {
super(context, resource, objects);
this.context = context;
this.resourceId = resource;
this.sites = objects;
this.phone=phone;
}
@Override
public String getItem(int position) {
return sites.get(position);
}
@Override
public int getCount() {
return sites.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
name = getItem(position);
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.home_resource, parent, false);
}
TextView mTextView = (TextView) view.findViewById(R.id.ridedetails);
mTextView.setText(name);
Button mButton = (Button) view.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Name",name);
String ridetrackno=name.substring(name.indexOf(":")+1,name.indexOf("Source")-2);
EditText mEdit;
mEdit = (EditText) view.findViewById(R.id.PriceQuotation);
String fare=mEdit.getText().toString();
String myUrl=".....API URL....."+phone+"/"+ridetrackno+"/"+fare;
Log.d("URL",myUrl);
HttpGetRequest getRequest = new HttpGetRequest();
try{
String result = getRequest.execute(myUrl).get();
Log.d("Details:",result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
return view;
}
}
This is my resource_file in which items are injected through REST API :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip">
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/ridedetails"
android:layout_marginLeft="2dip">
</TextView>
<EditText
android:layout_height="wrap_content"
android:hint="Quote Price"
android:layout_width="wrap_content"
android:id="@+id/PriceQuotation"
android:layout_below="@+id/ridedetails"
android:layout_marginLeft="2dip">
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/PriceQuotation"
android:layout_marginStart="61dp"
android:layout_toEndOf="@+id/PriceQuotation"
android:text="Button" />
</RelativeLayout>
Structure of List View :
Each list item is composed of a TextView(whose details I am able to get),Edit Text(causing Exception) and a simple button whose onClick function has been written.
Now I am facing two problems:
mEdit Reference : The program is failing with a null pointer exception when I am trying to get the text entered in Edit Text when button in a list item is clicked.
Always Last List Item is picked up : Irrespective of which button is clicked, the content of last TextView is only returned.
write this line outside button click listener
final EditText mEdit = (EditText) view.findViewById(R.id.PriceQuotation);
2.
final TextView mTextView = (TextView) view.findViewById(R.id.ridedetails);
Inside button click listener
String vlaue = mTextView.getText().toString();
getView() Code :
@Override
public View getView ( final int position, View convertView, ViewGroup parent){
name = getItem(position);
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.home_resource, parent, false);
}
final TextView mTextView = (TextView) view.findViewById(R.id.ridedetails);
mTextView.setText(name);
final EditText mEdit = (EditText) view.findViewById(R.id.PriceQuotation);
Button mButton = (Button) view.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String vlaue = mTextView.getText().toString();
Log.d("Name", vlaue);
String ridetrackno = vlaue.substring(vlaue.indexOf(":") + 1, vlaue.indexOf("Source") - 2);
String fare = mEdit.getText().toString();
String myUrl = ".....API URL....." + phone + "/" + ridetrackno + "/" + fare;
Log.d("URL", myUrl);
HttpGetRequest getRequest = new HttpGetRequest();
try {
String result = getRequest.execute(myUrl).get();
Log.d("Details:", result);
} catch (Exception e) {
e.printStackTrace();
}
}
});
return view;
}