在ListItem上显示PopupWindow单击项目单击动态更改ContentView数据

时间:2018-01-15 10:16:29

标签: android-popupwindow

我在SomeActivity中有一个ListView包含员工姓名。在项目列表中单击我想弹出一个窗口,显示所点击项目的员工详细信息。

我试过这个popupwindow.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">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="First Name"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Last Name"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
    android:id="@+id/firstName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textSize="18sp"
    app:layout_constraintStart_toEndOf="@+id/textView"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/lastName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textSize="18sp"
    app:layout_constraintStart_toEndOf="@+id/textView2"
    app:layout_constraintTop_toBottomOf="@+id/firstName" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text=" email address"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
    android:id="@+id/emailAdress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textSize="18sp"
    app:layout_constraintStart_toEndOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/lastName" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="phone number"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView6" />

<TextView
    android:id="@+id/phoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textSize="18sp"
    app:layout_constraintStart_toEndOf="@+id/textView8"
    app:layout_constraintTop_toBottomOf="@+id/emailAdress" />

在SomeActivity中我编写了这样的代码

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        String poName = ((TextView)view.findViewById(R.id.rowPOName)).getText().toString();
        // View layout = inflater.inflate(R.layout.detailspopup, null);
        initiatePopupWindow(poName);


    }
});



private void initiatePopupWindow(String name) {
    try {
        Cursor cursor2 = null;
        LayoutInflater inflater = (LayoutInflater) SomeSample.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.detailspopup, null);
        String[] selectedArgs = {name};          
        try {
            cursor2 = db.rawQuery("my query to get employee details", selectedArgs);
            if (cursor2.moveToFirst()) {
                String firstname = cursor2.getString(cursor2.getColumnIndex("firstname"));
                String lastname = cursor2.getString(cursor2.getColumnIndex("lastname"));
                String email = cursor2.getString(cursor2.getColumnIndex("email"));
                String phone = cursor2.getString(cursor2.getColumnIndex("phone"));

                TextView fNname = layout.findViewById(R.id.firstName);
                TextView  lName = layout.findViewById(R.id.lastName);
                TextView emailAdd = layout.findViewById(R.id.emailAdress);
                TextView phoneNumber = layout.findViewById(R.id.phoneNumber);

                fNname.setText(firstname);
                lName.setText(lastname);
                emailAdd.setText(email);
                phoneNumber.setText(phone);
            }


        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        } finally {
            if (cursor2 != null && !cursor2.isClosed()) {
                cursor2.close();
            }
        }

        popupWindow = new PopupWindow(layout, 370, 450, true);
        popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

执行代码后,我获得了弹出窗口,但没有填写员工的详细信息,即TextViews没有显示员工数据。它仅显示默认文本,即TextView。

需要帮助来解决这个问题

0 个答案:

没有答案