2016-08-12 36 views
0

*我希望用戶點擊listView中的行,以便我的對話框被調用。我有一個對話框xml文件。我不希望用戶點擊一個按鈕,我想單擊一行。如何在單擊一行時調用displayInputDialog?

                  * 
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> 
+0

此鏈接將對您有用。 https://www.mkyong.com/android/android-custom-dialog-example/ – Vasant

+0

多數民衆贊成在一個按鈕的例子我不使用一個按鈕我只是點擊特定的行 – angela

+0

哪個對話框?嘗試在提問時提供完整的細節 – Max

回答

0

這裏是顯示該對話框最簡單的例子,你可以進一步修改它作爲您的需求

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

電話showDialog();onItemClick

+0

謝謝你,讓它工作! – angela

相關問題