2013-08-05 83 views
-4

我的程序有一個活動和一個數據庫類。有一個按鈕用於從數據庫中檢索數據。我打算在textView中顯示字符串。但它沒有得到。TextView沒有顯示從數據庫檢索到的字符串

FirstActivity.java

package example.showevent1; 

    import java.util.Calendar; 

    import android.os.Bundle; 
    import android.app.DatePickerDialog; 
    import android.app.Dialog; 
    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.support.v4.app.*; 
    import android.view.View; 
    import android.view.inputmethod.InputMethodManager; 
    import android.widget.AdapterView; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.DatePicker; 
    import android.widget.EditText; 
    import android.widget.Spinner; 
    import android.widget.TextView; 
    import android.widget.AdapterView.OnItemSelectedListener; 

    public class FirstActivity extends FragmentActivity implements OnItemSelectedListener { 
     /** Called when the activity is first created. */ 

     classdbOpenHelper eventsData; 
     classdbOpenHelper eventsData1; 

     TextView userSelection; 
     Button okButton; 
     Button addButton; 

     Button change_date_but; 
     TextView date; 
     TextView show; 
     EditText edittext; 
      public static final int Date_dialog_id = 1; 
      private int mYear; 
      private int mMonth; 
      private int mDay; 

     private static final String[] items={"Yalahanka","Rajai nagar","Sivaji Nagar","Koramangala","RT Nagar", "Banashankari","Yashwanthpura","Hebbal"}; 



     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_first); 
      okButton = (Button)findViewById(R.id.button2); 
      addButton = (Button)findViewById(R.id.button3); 

      change_date_but = (Button)findViewById(R.id.button1); 
      date = (TextView)findViewById(R.id.textView2);//KEY_DATE 
      userSelection=(TextView)findViewById(R.id.textView1);//KEY_DESC 
      edittext=(EditText)findViewById(R.id.editText1);//KEY_EVENT 
      show=(TextView)findViewById(R.id.textView5); 

      Spinner my_spin=(Spinner)findViewById(R.id.spinner1); 
      my_spin.setOnItemSelectedListener(this); 
      ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,items); 
      aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      my_spin.setAdapter(aa); 

      okButton.setOnClickListener(new clicker()); 
      addButton.setOnClickListener(new getclicker()); 

      eventsData = new classdbOpenHelper(this); 

      change_date_but.setOnClickListener(new View.OnClickListener() { 



         @Override 
         public void onClick(View v) { 
         DatePickerDialog DPD = new DatePickerDialog(
         FirstActivity.this, mDateSetListener, mYear, mMonth,mDay); 
            DPD.show(); 
        } 
         }); 
      final Calendar c = Calendar.getInstance(); 
        mYear = c.get(Calendar.YEAR); 
        mMonth = c.get(Calendar.MONTH); 
        mDay = c.get(Calendar.DAY_OF_MONTH); 

        updateDisplay(); 
     } 
     @Override 
      @Deprecated 

      protected void onPrepareDialog(int id, Dialog dialog) { 
       // TODO Auto-generated method stub 
      super.onPrepareDialog(id, dialog); 

       ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 

      } 

      private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

      public void onDateSet(DatePicker view, int year, int monthOfYear, 
       int dayOfMonth) { 
       mYear = year; 
       mMonth = monthOfYear; 
      mDay = dayOfMonth; 
       updateDisplay(); 
       } 
      }; 

      private void updateDisplay() { 
      // TODO Auto-generated method stub 
       date.setText(new StringBuilder() 
       // Month is 0 based so add 1 
      .append(mMonth + 1).append("-").append(mDay).append("-") 
       .append(mYear)); 
      } 







     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) { 
      userSelection.setText(items[pos]); 
     } 
     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 
      userSelection.setText(""); 
     } 



     class clicker implements Button.OnClickListener { 
      public void onClick(View v) { 
       String datevalue = date.getText().toString(); 
       String Userselectvalue = userSelection.getText().toString(); 
       String Userevent = edittext.getText().toString(); 
       SQLiteDatabase db = eventsData.getWritableDatabase(); 

       ContentValues cv = new ContentValues(); 
       cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue); 
       cv.put(classdbOpenHelper.KEY_EVENT, Userevent); 
       cv.put(classdbOpenHelper.KEY_DATE,datevalue); 
       db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv); 
       db.close(); 
      } 


     public void onDestroy() { 
      eventsData.close(); 
     } 

    } 
     class getclicker implements Button.OnClickListener { 
      public void onClick(View v) { 
       String datevalue = date.getText().toString(); 
       String Userselectvalue = userSelection.getText().toString(); 

       String showevent = eventsData.getContact(datevalue,Userselectvalue); 
       show.setText(showevent); 
      } 
      public void onDestroy() { 
       eventsData.close(); 
       } 

     } 

    } 

classdbOpenHelper.java

package example.showevent1; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.widget.TextView; 


public class classdbOpenHelper extends SQLiteOpenHelper { 

    public static final String KEY_ROWID = "id"; 
    public static final String KEY_DESC = "countdesc"; 
    public static final String KEY_EVENT = "countevent"; 
    public static final String KEY_DATE = "countdate"; 

    public static final String DATABASE_NAME= "countdb"; 
    public static final String DATABASE_TABLE = "countable"; 
    public static final int DATABASE_VERSION = 1; 


    public classdbOpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
     /* db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
     KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT " + 
     //KEY_COUNTED + " INTEGER " + 
     KEY_DESC + " TEXT NOT NULL " + 
     KEY_DATE + " TEXT NOT NULL);" 
       ); */ 
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "(" 
       + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_EVENT + " TEXT, " + KEY_DATE + " TEXT " + ")"; 
     db.execSQL(CREATE_CONTACTS_TABLE); 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE); 
     onCreate(db); 

    } 

    public String getContact(String datevalue,String Userselectvalue) 
    { 
     String place = null; 
     String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'" 
      +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'"; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, 
      new String[] {classdbOpenHelper.KEY_EVENT }, selection, 
      null, null, null, null); 
     if (cursor .getCount() > 0) { 
      cursor .moveToFirst(); 
      do { 
        place = cursor.getString(0); 
      } while (cursor.moveToNext()); 
     } 
     cursor.close(); 
    return place; 
    } 
} 

activity_first.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@color/color_1" 
    tools:context=".FirstActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/spinner1" 
     android:layout_below="@+id/spinner1" 
     android:layout_marginTop="28dp" 
     android:background="#ffff99" 
     android:text="@string/dt" 
     android:textColor="#b22924" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/spinner1" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="31dp" 
     android:text="@string/select" 
     android:textAlignment="gravity" 
     android:textColor="#b22924" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="36dp" 
     android:background="#ffff99" 
     android:textColor="#b22924" 
     android:fadingEdge=""/> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="40dp" 
     android:background="#ffff99" 
     android:ems="10" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignParentRight="true" 
     android:text="@string/_add" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_alignParentRight="true" 
     android:background="#ffff99" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_alignParentTop="true" 
     android:text="Select A Place" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/button1" 
     android:layout_alignTop="@+id/button2" 
     android:text="Add Event" /> 

    <Button 
     android:id="@+id/button3" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/button2" 
     android:layout_marginTop="27dp" 
     android:text="Show Event" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="22dp" 
     android:background="#ffff99" 
     android:text="" /> 

</RelativeLayout> 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <string name="app_name">EVENTS</string> 
    <string name="action_settings">Settings</string> 
    <string name="select">SELECT A PLACE</string> 
    <color name="color_1">#ffcccccc</color> 
    <string name="dt">date</string> 
    <string name="_add">Add</string> 


</resources> 
+0

嘗試運行它在調試器中使用斷點來跟蹤函數調用。 – YankeeWhiskey

+0

你有什麼錯誤嗎?你有沒有試圖在logcat中打印這個值? – GrIsHu

回答

0

你的問題如下:你不從數據庫中獲取任何結果:

修改以下行

String selection = classdbOpenHelper.KEY_DESC + " = 'Userselectvalue'" +" AND " + classdbOpenHelper.KEY_DATE + " = 'datevalue'"; 

String selection = classdbOpenHelper.KEY_DESC + " = '" + Userselectvalue + "'" + " AND " + classdbOpenHelper.KEY_DATE + " = '" + datevalue+ "'"; 

應該解決你的問題。

我建議你閱讀一些教程:

  • 代碼風格
  • 變量命名
  • 基本調試(你會發現在幾分鐘內你的錯誤)
相關問題