Ich habe meine eigene mehrspaltige Listenansicht mit einem Editiertext und einer Schaltfläche in jeder Zeile erstellt. Ich setze den Tag-Wert für jede Taste, um zu identifizieren, auf welche Zeile geklickt wurde. In der onClick-Methode versuche ich jedoch, den Wert einzulesen und in ein int zu konvertieren. Der Wert dieses Edittets ist jedoch immer eine leere Zeichenfolge. Hier ist der Code:Benutzerdefinierte Listenansicht mit Edittext und Schaltfläche funktioniert nicht
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;
}
};
Die Sterne zeigen die zwei Zeilen, über die ich spreche. Ich vermute, dass es diesen Editiertext liest, wenn jede Zeile erstellt wird und deshalb immer eine leere Zeichenfolge zurückgibt. Gibt es eine Möglichkeit, den Wert des Edittext zu erhalten, wenn auf die Schaltfläche geklickt wird?
dies ist die Listenansicht XML-Datei:
<?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>
In welchem Layout ist die 'pointsGiven' ID? Sie versuchen gerade, es in der Aktivität zu finden, und nicht das Listenansicht Zeilenlayout –
pointsGiven ist in der Listview Zeilenlayout XML-Datei – jacmurphy50
Dann müssen Sie 'v.findViewById' statt –