2014-04-11 48 views
0

好的,這次在標題中我需要在edittext中有文本建議,但建議必須從數據庫中相應的行中提取。 我有48行的數據庫集。由於我禁用了此功能,因此無法添加行。行只能更新。用戶必須有一個選項來保留相應行中的當前內容或輸入新內容。編輯文本與數據庫中相應行的建議文本

有沒有人有這樣的想法? 在此先感謝。 Paddy

編輯------------------------------------------- ---------------------------------------------

here是我的日記文件:

package com.example.classorganizer; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 

import com.cookbook.data.MyDB; 
public class Diary extends Activity { 
    AutoCompleteTextView titleET1; 
    AutoCompleteTextView titleET2; 
    AutoCompleteTextView titleET3; 
    AutoCompleteTextView titleET4; 
    AutoCompleteTextView titleET5; 
    AutoCompleteTextView titleET6; 
    AutoCompleteTextView titleET7; 
    AutoCompleteTextView titleET8; 
    AutoCompleteTextView titleET9; 
    AutoCompleteTextView titleET10; 
    AutoCompleteTextView titleET11; 
    AutoCompleteTextView titleET12; 
    AutoCompleteTextView titleET13; 
    AutoCompleteTextView titleET14; 
    AutoCompleteTextView titleET15; 
    AutoCompleteTextView titleET16; 
    AutoCompleteTextView titleET17; 
    AutoCompleteTextView titleET18; 
    AutoCompleteTextView titleET19; 
    AutoCompleteTextView titleET20; 
    AutoCompleteTextView titleET21; 
    AutoCompleteTextView titleET22; 
    AutoCompleteTextView titleET23; 
    AutoCompleteTextView titleET24; 
    AutoCompleteTextView titleET25; 
    AutoCompleteTextView titleET26; 
    AutoCompleteTextView titleET27; 
    AutoCompleteTextView titleET28; 
    AutoCompleteTextView titleET29; 
    AutoCompleteTextView titleET30; 
    AutoCompleteTextView titleET31; 
    AutoCompleteTextView titleET32; 
    AutoCompleteTextView titleET33; 
    AutoCompleteTextView titleET34; 
    AutoCompleteTextView titleET35; 
    AutoCompleteTextView titleET36; 
    AutoCompleteTextView titleET37; 
    AutoCompleteTextView titleET38; 
    AutoCompleteTextView titleET39; 
    AutoCompleteTextView titleET40; 
    AutoCompleteTextView titleET41; 
    AutoCompleteTextView titleET42; 
    AutoCompleteTextView titleET43; 
    AutoCompleteTextView titleET44; 
    AutoCompleteTextView titleET45; 
    AutoCompleteTextView titleET46; 
    AutoCompleteTextView titleET47; 
    AutoCompleteTextView titleET48; 

    Button submitBT; 
    MyDB dba; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 




     super.onCreate(savedInstanceState); 
     setContentView(R.layout.diary); 
     dba = new MyDB(this); 
     dba.open(); 



titleET1 = (AutoCompleteTextView)findViewById(R.id.diary1); 

     titleET2 = (AutoCompleteTextView)findViewById(R.id.diary2); 
     titleET3 = (AutoCompleteTextView)findViewById(R.id.diary3); 
     titleET4 = (AutoCompleteTextView)findViewById(R.id.diary4); 
     titleET5 = (AutoCompleteTextView)findViewById(R.id.diary5); 
     titleET6 = (AutoCompleteTextView)findViewById(R.id.diary6); 
     titleET7 = (AutoCompleteTextView)findViewById(R.id.diary7); 
     titleET8 = (AutoCompleteTextView)findViewById(R.id.diary8); 
     titleET9 = (AutoCompleteTextView)findViewById(R.id.diary9); 
     titleET10 = (AutoCompleteTextView)findViewById(R.id.diary10); 
     titleET11 = (AutoCompleteTextView)findViewById(R.id.diary11); 
     titleET12 = (AutoCompleteTextView)findViewById(R.id.diary12); 
     titleET13 = (AutoCompleteTextView)findViewById(R.id.diary13); 
     titleET14 = (AutoCompleteTextView)findViewById(R.id.diary14); 
     titleET15 = (AutoCompleteTextView)findViewById(R.id.diary15); 
     titleET16 = (AutoCompleteTextView)findViewById(R.id.diary16); 
     titleET17 = (AutoCompleteTextView)findViewById(R.id.diary17); 
     titleET18 = (AutoCompleteTextView)findViewById(R.id.diary18); 
     titleET19 = (AutoCompleteTextView)findViewById(R.id.diary19); 
     titleET20 = (AutoCompleteTextView)findViewById(R.id.diary20); 
     titleET21 = (AutoCompleteTextView)findViewById(R.id.diary21); 
     titleET22 = (AutoCompleteTextView)findViewById(R.id.diary22); 
     titleET23 = (AutoCompleteTextView)findViewById(R.id.diary23); 
     titleET24 = (AutoCompleteTextView)findViewById(R.id.diary24); 
     titleET25 = (AutoCompleteTextView)findViewById(R.id.diary25); 
     titleET26 = (AutoCompleteTextView)findViewById(R.id.diary26); 
     titleET27 = (AutoCompleteTextView)findViewById(R.id.diary27); 
     titleET28 = (AutoCompleteTextView)findViewById(R.id.diary28); 
     titleET29 = (AutoCompleteTextView)findViewById(R.id.diary29); 
     titleET30 = (AutoCompleteTextView)findViewById(R.id.diary30); 
     titleET31 = (AutoCompleteTextView)findViewById(R.id.diary31); 
     titleET32 = (AutoCompleteTextView)findViewById(R.id.diary32); 
     titleET33 = (AutoCompleteTextView)findViewById(R.id.diary33); 
     titleET34 = (AutoCompleteTextView)findViewById(R.id.diary34); 
     titleET35 = (AutoCompleteTextView)findViewById(R.id.diary35); 
     titleET36 = (AutoCompleteTextView)findViewById(R.id.diary36); 
     titleET37 = (AutoCompleteTextView)findViewById(R.id.diary37); 
     titleET38 = (AutoCompleteTextView)findViewById(R.id.diary38); 
     titleET39 = (AutoCompleteTextView)findViewById(R.id.diary39); 
     titleET40 = (AutoCompleteTextView)findViewById(R.id.diary40); 
     titleET41 = (AutoCompleteTextView)findViewById(R.id.diary41); 
     titleET42 = (AutoCompleteTextView)findViewById(R.id.diary42); 
     titleET43 = (AutoCompleteTextView)findViewById(R.id.diary43); 
     titleET44 = (AutoCompleteTextView)findViewById(R.id.diary44); 
     titleET45 = (AutoCompleteTextView)findViewById(R.id.diary45); 
     titleET46 = (AutoCompleteTextView)findViewById(R.id.diary46); 
     titleET47 = (AutoCompleteTextView)findViewById(R.id.diary47); 
     titleET48 = (AutoCompleteTextView)findViewById(R.id.diary48); 




     submitBT = (Button)findViewById(R.id.submitButton); 
     submitBT.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       try { 
        saveItToDB(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 


     }); 
    } 



    public void saveItToDB() { 
     dba.updateDiaryEntry(titleET1.getText().toString(), 1); 
     dba.updateDiaryEntry(titleET2.getText().toString(), 2); 
     dba.updateDiaryEntry(titleET3.getText().toString(), 3); 
     dba.updateDiaryEntry(titleET4.getText().toString(), 4); 
     dba.updateDiaryEntry(titleET5.getText().toString(), 5); 
     dba.updateDiaryEntry(titleET6.getText().toString(), 6); 
     dba.updateDiaryEntry(titleET7.getText().toString(), 7); 
     dba.updateDiaryEntry(titleET8.getText().toString(), 8); 
     dba.updateDiaryEntry(titleET9.getText().toString(), 9); 
     dba.updateDiaryEntry(titleET10.getText().toString(), 10); 
     dba.updateDiaryEntry(titleET11.getText().toString(), 11); 
     dba.updateDiaryEntry(titleET12.getText().toString(), 12); 
     dba.updateDiaryEntry(titleET13.getText().toString(), 13); 
     dba.updateDiaryEntry(titleET14.getText().toString(), 14); 
     dba.updateDiaryEntry(titleET15.getText().toString(), 15); 
     dba.updateDiaryEntry(titleET16.getText().toString(), 16); 
     dba.updateDiaryEntry(titleET17.getText().toString(), 17); 
     dba.updateDiaryEntry(titleET18.getText().toString(), 18); 
     dba.updateDiaryEntry(titleET19.getText().toString(), 19); 
     dba.updateDiaryEntry(titleET20.getText().toString(), 20); 
     dba.updateDiaryEntry(titleET21.getText().toString(), 21); 
     dba.updateDiaryEntry(titleET22.getText().toString(), 22); 
     dba.updateDiaryEntry(titleET23.getText().toString(), 23); 
     dba.updateDiaryEntry(titleET24.getText().toString(), 24); 
     dba.updateDiaryEntry(titleET25.getText().toString(), 25); 
     dba.updateDiaryEntry(titleET26.getText().toString(), 26); 
     dba.updateDiaryEntry(titleET27.getText().toString(), 27); 
     dba.updateDiaryEntry(titleET28.getText().toString(), 28); 
     dba.updateDiaryEntry(titleET29.getText().toString(), 29); 
     dba.updateDiaryEntry(titleET30.getText().toString(), 30); 
     dba.updateDiaryEntry(titleET31.getText().toString(), 31); 
     dba.updateDiaryEntry(titleET32.getText().toString(), 32); 
     dba.updateDiaryEntry(titleET33.getText().toString(), 33); 
     dba.updateDiaryEntry(titleET34.getText().toString(), 34); 
     dba.updateDiaryEntry(titleET35.getText().toString(), 35); 
     dba.updateDiaryEntry(titleET36.getText().toString(), 36); 
     dba.updateDiaryEntry(titleET37.getText().toString(), 37); 
     dba.updateDiaryEntry(titleET38.getText().toString(), 38); 
     dba.updateDiaryEntry(titleET39.getText().toString(), 39); 
     dba.updateDiaryEntry(titleET40.getText().toString(), 40); 
     dba.updateDiaryEntry(titleET41.getText().toString(), 41); 
     dba.updateDiaryEntry(titleET42.getText().toString(), 42); 
     dba.updateDiaryEntry(titleET43.getText().toString(), 43); 
     dba.updateDiaryEntry(titleET44.getText().toString(), 44); 
     dba.updateDiaryEntry(titleET45.getText().toString(), 45); 
     dba.updateDiaryEntry(titleET46.getText().toString(), 46); 
     dba.updateDiaryEntry(titleET47.getText().toString(), 47); 
     dba.updateDiaryEntry(titleET48.getText().toString(), 48); 

     dba.close(); 
     titleET1.setText(""); 

     titleET2.setText(""); 
     titleET3.setText(""); 
     titleET4.setText(""); 
     titleET5.setText(""); 
     titleET6.setText(""); 
     titleET7.setText(""); 
     titleET8.setText(""); 
     titleET9.setText(""); 
     titleET10.setText(""); 
     titleET11.setText(""); 
     titleET12.setText(""); 
     titleET13.setText(""); 
     titleET14.setText(""); 
     titleET15.setText(""); 
     titleET16.setText(""); 
     titleET17.setText(""); 
     titleET18.setText(""); 
     titleET19.setText(""); 
     titleET20.setText(""); 
     titleET21.setText(""); 
     titleET22.setText(""); 
     titleET23.setText(""); 
     titleET24.setText(""); 
     titleET25.setText(""); 
     titleET26.setText(""); 
     titleET27.setText(""); 
     titleET28.setText(""); 
     titleET29.setText(""); 
     titleET30.setText(""); 
     titleET31.setText(""); 
     titleET32.setText(""); 
     titleET33.setText(""); 
     titleET34.setText(""); 
     titleET35.setText(""); 
     titleET36.setText(""); 
     titleET37.setText(""); 
     titleET38.setText(""); 
     titleET39.setText(""); 
     titleET40.setText(""); 
     titleET41.setText(""); 
     titleET42.setText(""); 
     titleET43.setText(""); 
     titleET44.setText(""); 
     titleET45.setText(""); 
     titleET46.setText(""); 
     titleET47.setText(""); 
     titleET48.setText(""); 


     Intent i = new Intent(Diary.this, Monday.class); 
     startActivity(i); 
    } 
    /** Called when the user clicks the Back button */ 
    public void visitMonday(View view) { 
     Intent intent = new Intent(this, Monday.class); 
     startActivity(intent); 
    } 
} 

在這裏的是,我應該插入onCreate()方法的功能:

private void initializeDescription() { 
    _descriptionText = (AutoCompleteTextView) findViewById(R.id.description); 
    final int[] to = new int[]{android.R.id.text1}; 
    final String[] from = new String[]{VehicleDescriptionsTable.DESCRIPTION}; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_dropdown_item_1line, 
      null, 
      from, 
      to); 

    // This will provide the labels for the choices to be displayed in the AutoCompleteTextView 
    adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { 
     @Override 
     public CharSequence convertToString(Cursor cursor) { 
      final int colIndex = cursor.getColumnIndexOrThrow(VehicleDescriptionsTable.DESCRIPTION); 
      return cursor.getString(colIndex); 
     } 
    }); 

    // This will run a query to find the descriptions for a given vehicle. 
    adapter.setFilterQueryProvider(new FilterQueryProvider() { 
     @Override 
     public Cursor runQuery(CharSequence description) { 
      String vehicle = getSelectedVehicle(); 
      Cursor managedCursor = _helper.getDescriptionsFor(vehicle, description.toString()); 
      Log.d(TAG, "Query has " + managedCursor.getCount() + " rows of description for " + vehicle); 
      return managedCursor; 
     } 
    }); 

    _descriptionText.setAdapter(adapter); 
} 

不幸的是,當我插入此函數爲onCreate()方法錯誤彈出在那裏說在標記{和}處出錯。我究竟做錯了什麼?

我想讓edittext顯示錶中相應行的預定義文本。

編輯--------------------------------------------- ------------------------------------------

我試着用AutoCompleteTextView CursorAdapter,但我看不到從數據庫中的行中獲取信息的選項。我在這裏錯過了什麼?

+0

這裏適配器的一種方法:http://developer.android.com/guide/topics/search/adding-custom-suggestions.html –

+0

@KenWolf這不是我正在尋找的不幸 – paddyfox

回答

2

AutoCompleteTextView是你在找什麼。

如何使用它:

  1. 更改EditText TO AutoCompleteTextView
  2. 創建Adapter類:在這種情況下,CursorAdapter會適合更好地從數據庫中提取數據。您可以在Cursor對象中獲取數據庫數據,然後將其傳遞給SimpleCursorAdapterhere is a tuto)。或者,如果您想要自定義適配器,請創建一個新類,該類基類爲extendsCursorAdapter

  3. 設置爲AutoCompleteTextView

    autoCompleteView.setAdapter(adapter);

+1

+1我正在寫相同的確切的事情歡呼隊友 – Tascalator

+0

這聽起來沒事,但我不知道從哪裏開始。我將所有編輯文本都更改爲AutoCompleteTextView,我不知道下一步該怎麼做。從你說我不知道​​我需要創建一個名爲CursorAdapter的新類。在這裏,我不知道該在課堂上做什麼。 – paddyfox

+0

不完全。你只需要一個適配器來給你的'AutoCompleteTextView'。檢查我的編輯。 –