2017-07-28 42 views
-1

我想在我的上一個按鈕,點擊數據庫中添加數據,我想顯示在ListView。我想提出一個應用程序,其中有一個EditText字段數據和按鈕。如果用戶在字段中輸入名稱並單擊按鈕,數據應該添加到數據庫中,並且listview也會填充數據。我發佈了使用遊標適配器,列表視圖顯示數據和執行按鈕的代碼行動。 我也在使用遊標加載器。我已經實現了遊標加載器的基本功能。添加和顯示數據

Here is my xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 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" 
     tools:context="com.example.nitikakamboj.drawapp.MainActivity"> 
     <LinearLayout 
      android:id="@+id/linear_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="16dp" 
      android:orientation="horizontal"> 
     <EditText 
      android:id="@+id/name_field" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="Enter Name" 
      android:layout_weight="1"/> 
      <Button 
       android:id="@+id/add_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="add"/> 
     </LinearLayout> 
     <TextView 
      android:id="@+id/no_name" 
      android:layout_below="@id/linear_layout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No Name in List" 
      android:layout_centerInParent="true" 
      android:visibility="gone" 
      android:textSize="24sp" 
      android:textAllCaps="true" 
      /> 
     <ListView 
      android:id="@+id/listView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/no_name"></ListView> 
    </RelativeLayout> 

這是我的主Java代碼
我已經發布我的代碼,其使用光標適配器,列表視圖顯示數據和一個按鈕來執行操作。 我也在使用遊標加載器。我已經實現了遊標加載器的基本功能。

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{ 
     private EditText mNameEditText; 
     NameCursorAdapter nameCursorAdapter; 
     SQLiteDatabase database; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final ListView nameListView=(ListView)findViewById(R.id.listView); 
     nameCursorAdapter=new NameCursorAdapter(MainActivity.this,null); 
     nameListView.setAdapter(nameCursorAdapter); 
     mNameEditText=(EditText)findViewById(R.id.name_field); 

     Button mAddButton=(Button) findViewById(R.id.add_button); 
     mAddButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      String name= mNameEditText.getText().toString(); 
      if(name.length()>0) 
      { 

      } 
      } 
     }); 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
     String projection[]={NameEntry._ID, 
     NameEntry.COLUMN_NAME}; 
     return new CursorLoader(this,NameEntry.BASE_CONTENT_URI, 
       projection, 
       null, 
       null, 
       null); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
      nameCursorAdapter.swapCursor(data); 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 
      nameCursorAdapter.swapCursor(null); 
    } 
} 
+0

你不能從XML單獨做到這一點...... – Shark

+0

我知道我已經實現了創建數據庫,設置提供程序和主要活動代碼的代碼。我發佈了我的xml文件,以便可以清楚地瞭解我想在UI中做什麼。 –

+0

我不知道如何在點擊按鈕上更新數據庫並將數據添加到listview.I只需要幫助。 –

回答

1

首先,你需要在你活動的onCreate方法來抓住你的XML按鈕的引用:

Button mAddButton = (Button)findViewById(R.id.add_button); 

然後,添加一個OnClickListener:

mAddButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     //Do database stuff here, for example: grab text from EditText and save to DB table. 
    } 
} 

然後,每當你點擊該按鈕,即OnClickListener將運行代碼來保存您的數據。

快樂編碼!

+0

其實我知道數據庫的東西會在點擊method.Can你請解釋什麼樣的東西我需要做的? –

+0

@NitikaKamboj你需要知道如何做數據庫事務?閱讀本教程:https://developer.android.com/training/basics/data-storage/databases.html – privatestaticint