如何将listview的值传递给dialogbox setView

时间:2015-08-24 02:55:45

标签: android listview alertdialog

我有一个列表视图,当它被点击时,它将获得所有值,它应该在对话框的视图中显示。但它没有用。我怎么能这样做?

 list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


            String stor = mylist.get(position).get("storcode");
            String code = mylist.get(position).get("code");
            String beg = mylist.get(position).get("beg");
            String del = mylist.get(position).get("del");
            String spo = mylist.get(position).get("spo");
            String end = mylist.get(position).get("end");

            new AlertDialog.Builder(checklist.this)
                  .setTitle("Sample")
                  .setView(R.layout.activity_layoutdialog)
                  .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                      }
                  })
                  .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                      }
                  }).show();



            return true;
        }

    });

activity_layoutdialog     

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/outlet"
        android:layout_marginBottom="10dp"

        />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sp_outlet"

        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/product_code"
        android:layout_marginBottom="10dp"

        />
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sp_product"
        android:layout_marginBottom="10dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/beginning_stock"
        android:layout_marginBottom="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/beginningstock"
        android:textCursorDrawable="@drawable/text_cursor"
        android:background="@drawable/bg_edittext"
        android:singleLine="true"
        android:numeric="integer"
        android:layout_marginBottom="5dp"
        android:maxLength="5" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/deliveries"
        android:layout_marginBottom="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/deliverablestock"
        android:textCursorDrawable="@drawable/text_cursor"
        android:background="@drawable/bg_edittext"

        android:singleLine="true"
        android:numeric="integer"
        android:layout_marginBottom="5dp"
        android:maxLength="5" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/spoilage"
        android:layout_marginBottom="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/spoilage"

        android:textCursorDrawable="@drawable/text_cursor"
        android:background="@drawable/bg_edittext"
        android:singleLine="true"
        android:numeric="integer"
        android:layout_marginBottom="5dp"
        android:maxLength="5" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/ending_stock"
        android:layout_marginBottom="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/endingstock"
        android:background="@drawable/bg_edittext"
        android:textCursorDrawable="@drawable/text_cursor"

        android:singleLine="true"
        android:numeric="integer"
        android:layout_marginBottom="20dp"
        android:maxLength="5" />


</LinearLayout>

我在点击listview项目时尝试实现编辑功能。

1 个答案:

答案 0 :(得分:0)

在实际构建AlertDialog之前,您可以膨胀对话框的视图并为其EditTexts指定值。

LayoutInflater factory = LayoutInflater.from(this);
View dialogView = factory.inflate(R.layout.activity_layoutdialog, null);
EditText editText1 = (EditText) dialogView.findViewById(R.id. beginningstock);
editText1.setText(beg);
// etc

new AlertDialog.Builder(checklist.this)
              .setTitle("Sample")
              .setView(dialogView).show();
// etc