单击一行时如何调用displayInputDialog?

时间:2016-08-12 11:20:25

标签: android

*我希望用户点击listView中的行,以便调用我的对话框。我有一个对话框xml文件。我不希望用户点击我要点击一行的按钮。

                                                                          * 
public class Edit_Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DatabaseClass  database;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    Dialog d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_list_layout);


        listView = (ListView) findViewById(R.id.list_view);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        listView.setOnItemClickListener(this);
        database = new DatabaseClass(getApplicationContext());

        sqLiteDatabase = database.getReadableDatabase();

        Cursor cursor=database.getInformation(sqLiteDatabase);

        if (cursor.moveToFirst()) {
            do {

                String id, product_name, category;
                id = cursor.getString(0);
                product_name = cursor.getString(1);
                category = cursor.getString(2);
                DataProvider dataProvider = new DataProvider(id, product_name, category);
                listDataAdapter.add(dataProvider);
            } while (cursor.moveToNext()


                    );

        }

    }



     @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view.findViewById(R.id.product_id);
                sqLiteDatabase = database.getReadableDatabase();
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                        Toast.LENGTH_SHORT).show();

            }

输入对话框:这是我的输入对话框,当用户点击一行时必须出现这一点。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="match_parent"
       android:layout_margin="?attr/actionBarSize"
       android:orientation="vertical"
       android:paddingLeft="15dp"
       android:paddingRight="15dp"
       android:paddingTop="10dp"
       >

       <android.support.design.widget.TextInputLayout
           android:id="@+id/nameLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">

           <EditText
               android:id="@+id/nameEditText"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:singleLine="true"
               android:hint="Name"
               />


       </android.support.design.widget.TextInputLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/updateBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UPDATE"
        android:clickable="true"
        android:background="#009968"
        android:layout_marginTop="20dp"
        android:textColor="@android:color/white"/>


</LinearLayout>

   </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

以下是显示对话框的最简单示例,您可以根据需要进一步修改

private void showDialog(){
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.YourDialogLayout);
        dialog.setTitle("Here Goes the Title");
        dialog.show();
    }

showDialog();中致电onItemClick