Reputation: 95
I have created my own multi columned listview with an edittext and a button on each row. I am setting the tag value for each button to identify which row has been clicked. However in the onClick method I am trying to read in the value and convert it to an int. However the value of this edittext is always an empty string. Here is the code:
wrapper.mySimpleAdapter = new SimpleAdapter(Chores.this, chores, R.layout.parent_list_format, new String[]{"chore","child_name","points"},new int[]{R.id.value_name,R.id.child_name,R.id.point_value })
{
@Override
public View getView (final int position, View convertView, ViewGroup parent)
{
//Setting the tag for each button of the list to the chore name
//to be able to identify which chore is to be claimed
final View v = super.getView(position, convertView, parent);
Button b=(Button)v.findViewById(R.id.rate);
String names = ( v.findViewById(R.id.child_name)).toString();
names += ("::") + ( v.findViewById(R.id.value_name)).toString();
b.setTag(names);
//When one of the list buttons have been clicked
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
//Sends the chore data to the php file to update the database and reload page
String names = (String) view.getTag();
String child = names.substring(0,names.indexOf(":"));
String chore = names.substring(names.lastIndexOf(":"), names.length()-1);
**EditText points = (EditText) findViewById(R.id.pointsGiven);
int pointsGiven = Integer.parseInt (points.getText().toString());**
Chore chores = new Chore(chore,pointsGiven,accountConnect.user.username,child);
uploadChore(chores);
startActivity(new Intent(v.getContext(), Chores.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
return v;
}
};
The stars indicate the two lines in particular I am talking about. I'm guessing that it is reading this edittext when each row is being created and that's why it is always returning an empty string. Is there any way I can get the value of the edittext when the button is clicked?
this is the listview xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/value_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/child_name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/point_value"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:typeface="sans"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="points"
android:ems="10"
android:id="@+id/pointsGiven"
/>
<Button
android:id="@+id/rate"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text = "Rate"
android:layout_weight="1" />
</LinearLayout>
Upvotes: 0
Views: 996
Reputation: 349
You have to pass to the on click the position of the item clicked then get the item :
//When one of the list buttons have been clicked
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Item item = getItem((int)view.getTag());
}
Upvotes: 0
Reputation: 1267
@Override
public View getView (final int position, View convertView, ViewGroup parent)
{
//Setting the tag for each button of the list to the chore name
//to be able to identify which chore is to be claimed
final View v = super.getView(position, convertView, parent);
Button b=(Button)v.findViewById(R.id.rate);
String names = ( v.findViewById(R.id.child_name)).toString();
names += ("::") + ( v.findViewById(R.id.value_name)).toString();
b.setTag(names);
EditText points = (EditText) v.findViewById(R.id.pointsGiven);
//When one of the list buttons have been clicked
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
//Sends the chore data to the php file to update the database and reload page
String names = (String) view.getTag();
String child = names.substring(0,names.indexOf(":"));
String chore = names.substring(names.lastIndexOf(":"), names.length()-1);
int pointsGiven = Integer.parseInt (points.getText().toString());**
Chore chores = new Chore(chore,pointsGiven,accountConnect.user.username,child);
uploadChore(chores);
startActivity(new Intent(v.getContext(), Chores.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});
return v;
}
Initialize EditText before the onClick in the adapter and try to get value of the same.
Upvotes: 0
Reputation: 5517
Assuming that your EditText points
is located inside your row, you need to find it in your row View:
@Override
public void onClick(View view) {
//your code
EditText points = (EditText) view.findViewById(R.id.pointsGiven);
//your code
}
Upvotes: 1