如何在buttonclick上从列表视图获取edittext数据?

时间:2019-02-05 11:05:43

标签: android listview android-listview

我有一个edittext和一个按钮,假设listview中有EditText,ButtonEdit,在listview中,记录是动态生成的,服务器响应是从中插入数据的。因此,我如何单击任何行中的按钮并获得该edittext值(然后将它们发送到另一个活动,然后再次编辑并保存它们)? (我仅发布相关代码

activity.java

JSONArray jsonArr = jsonObj.getJSONArray("questionbody");
            for (int i = 0; i < jsonArr.length(); i++) {
                HashMap<String, String> question = new HashMap<>();
                JSONObject jsonObj1 = jsonArr.getJSONObject(i);
                question.put("coursename", jsonObj1.getString("coursename"));
                question.put("subjectname", jsonObj1.getString("subjectname"));
                questionList.add(question);
            }

            ListAdapter adapter = new SimpleAdapter(
                    activity.this, questionList,
                    R.layout.list_item, new String[]{"coursename", "subjectname"}, new int[]{R.id.coursename,
                    R.id.subjectname});

            listviewQuestion.setAdapter(adapter);

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        tools:context="com.it.jumtech.equestion.activity.QuestionListActivity">

    <ListView
        android:id="@+id/listQuestion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"/>
</RelativeLayout>

list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.it.jumtech.equestion.activity.QuestionListActivity">

<TextView
    android:id="@+id/coursename"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="6dp"
    android:textColor="@color/colorPrimary"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginStart="5dp" />

<TextView
    android:id="@+id/subjectname"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_marginTop="6dp"
    android:textColor="@color/colorPrimary"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/coursename"
    android:layout_marginLeft="2dp"
    android:layout_marginStart="2dp" />

<Button
    android:id="@+id/buttonEdit"
    android:layout_width="100dp"
    android:layout_height="38dp"
    android:layout_marginBottom="6dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="6dp"
    android:background="@android:color/holo_red_dark"
    android:text="EDIT"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textStyle="bold"
  app:layout_constraintLeft_toRightOf="@+id/questionbody"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案