angela angela - 3 months ago 6
Android Question

How to call displayInputDialog when a row is clicked?

*I want the user to click on the row in the listView and so that my dialog is called.I have a dialog xml file .I don't want the user to click on a button I want to click on a row.

*
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();

}


Input dialog:This my input dialog ,when the user clicks on a row this has to come up.

<?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>

Answer

Here is the simplest example to display the dialog, you can further modify it as your needs

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

call showDialog(); in your onItemClick.