如何将列表视图值发送到Android中的另一个活动

时间:2019-03-04 13:40:57

标签: android

我正在android中创建一个简单的crud系统。我可以查看Listview中的所有数据。如果我从列表视图中选择一个项目,它将转到另一个活动进行编辑。但成功将数据传递到另一个活动。但是如何分配给文本字段。我只能通过一个文本字段。如何将值传递到相关的文本字段。我在下面附上了屏幕截图和代码

屏幕截图

enter image description here

enter image description here

view.xml

<LinearLayout 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"
android:orientation="vertical"
tools:context=".view">

<ListView
    android:id="@+id/lst1"
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />
</LinearLayout>

View.java

public class view extends AppCompatActivity {
    ListView lst1;
    ArrayList<String> titles = new ArrayList<String>();
    ArrayAdapter arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        SQLiteDatabase db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
        lst1 = findViewById(R.id.lst1);

        Cursor c = db.rawQuery("select * from record",null);
        int id = c.getColumnIndex("id");
        int name = c.getColumnIndex("name");
        int age = c.getColumnIndex("age");
        c.moveToFirst();
        titles.clear();
        arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,titles);
        lst1.setAdapter(arrayAdapter);

        if(c.moveToFirst()) {
            do {
             titles.add(c.getString(id) + " \t " +  c.getString(name) + " \t " +     c.getString(age));
            } while (c.moveToNext());
            arrayAdapter.notifyDataSetChanged();

        lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String tt = titles.get(position).toString();
                Intent i = new Intent(getApplicationContext(),edit.class);
                i.putExtra("age",tt);
                startActivity(i);
            }
        });
    }

Edit.xml

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity = "center"
        android:orientation="vertical">
        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Edit  System"
            android:textColor="@color/colorAccent"
            android:textSize="40dp"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="ID" />
            <EditText
                android:id="@+id/id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Name" />
            <EditText
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Age" />
            <EditText
                android:id="@+id/age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Edit" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Delete" />
        </LinearLayout>

Edit.java

public class edit extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        EditText ed1 = findViewById(R.id.age);
        Intent i = getIntent();
        String ttt = i.getStringExtra("age").toString();
        ed1.setText(ttt);

    }
}

2 个答案:

答案 0 :(得分:0)

创建一个像这样的pojo类

class student{
    String id,
    String name,
    String age,
    String titles
}

现在替换下面的代码

if(c.moveToFirst()) {
        do {
         titles.add(c.getString(id) + " \t " +  c.getString(name) + " \t " +     c.getString(age));
        } while (c.moveToNext());
        arrayAdapter.notifyDataSetChanged();

     lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tt = titles.get(position).toString();
            Intent i = new Intent(getApplicationContext(),edit.class);
            i.putExtra("age",tt);
            startActivity(i);
        }
    });

ArrayList<student> stud = new ArrayList<student>();
if(c.moveToFirst()) {
    do {
     student stu = new student()
     stu.id = c.getString(id);
     stu.name =  c.getString(name);
     stu.age =  c.getString(age);
     //you need to add the Student object stu not the ArrayList Object stud
     stud.add(stu);
     titles.add(c.getString(id) + " \t " +  c.getString(name) + " \t " +     c.getString(age));
    } while (c.moveToNext());
    arrayAdapter.notifyDataSetChanged();

    lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tt = titles.get(position).toString();
            student stu = stud.get(position);
            Intent i = new Intent(getApplicationContext(),edit.class);
            i.putExtra("age",stu.age);
            startActivity(i);
        }
    });

答案 1 :(得分:0)

您需要为自定义项目创建一个类:

class Student{
    String id;
    String name;
    String age;
    String title;

public Student(String id, String name, String age)
{
    this.id = id;
    this.name = name;
    this.age = age;
    title = id + " \t" +name+" \t"+age;
}

}

// View.java:

if(c.moveToFirst()) {
    do {
        Student tmpStudent = new Student(c.getString(id), c.getString(name), c.getString(age));

        titles.add(tmpStudent.title);
    } while (c.moveToNext());
    arrayAdapter.notifyDataSetChanged();

    lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String studentString[] = titles.get(position).toString().Split(" \t");
            Student tmpStringStudent = new Student(studentString[0], studentString[1], studentString[2]);
            Intent i = new Intent(getApplicationContext(),edit.class);
            i.putExtra("id",tmpStringStudent.id);
            i.putExtra("name",tmpStringStudent.name);
            i.putExtra("age",tmpStringStudent.age);
            startActivity(i);
        }
    });
}

// Edit.java:

public class edit extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        EditText ageEditText = findViewById(R.id.age);
        EditText idEditText = findViewById(R.id.id);
        EditText nameEditText = findViewById(R.id.name);
        Intent i = getIntent();
        String id = i.getStringExtra("id").toString();
        String name = i.getStringExtra("name").toString();
        String age = i.getStringExtra("age").toString();
        idEditText.setText(id);
        nameEditText.setText(name);
        ageEditText.setText(age);

    }
}

我希望这会有所帮助。