【发布时间】:2018-03-21 11:05:09
【问题描述】:
这是我的自定义列表适配器类:
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;
}
}
这是我的资源文件,其中项目通过 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>
列表视图的结构:
每个列表项由一个TextView(我能得到它的详细信息)、编辑文本(导致异常)和一个简单的按钮组成,它的onClick函数已经写好了。
现在我面临两个问题:
mEdit 参考:当我尝试在列表项中的按钮时获取在编辑文本中输入的文本时,程序失败并出现空指针异常被点击。
-
总是选择最后一个列表项:无论点击哪个按钮,都只返回最后一个 TextView 的内容。
李>
【问题讨论】:
标签: android listview android-edittext onclicklistener listadapter