gpt4 book ai didi

java - 如何单击 AutocompleteTextView 列表上的事件并查询 sqlite。(我有一个示例代码。)

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:17 28 4
gpt4 key购买 nike

如何单击 AutocompleteTextView 上的数据库列表并将值发送到查询 sqlite 数据库。现在我可以使用 AutocompleteTextView 来显示数据,当我单击它时将显示 Toast。请帮我。这对我来说非常重要。抱歉我的英语不好。

这是我的代码。

myDBClass.java

package com.myapp;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class myDBClass extends SQLiteOpenHelper {


// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "mydatabase";

// Table Name
private static final String TABLE_GALLERY = "gallery";

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

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_GALLERY +
"(GalleryID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Name TEXT(100)," +
" Path TEXT(100));");

Log.d("CREATE TABLE","Create Table Successfully.");
}


// Insert Data
public long InsertData(String strGalleryID, String strName, String strPath) {
// TODO Auto-generated method stub

try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data

/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "INSERT INTO " + TABLE_GALLERY
+ "(GalleryID,Name,Path) VALUES (?,?,?)";

insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strGalleryID);
insertCmd.bindString(2, strName);
insertCmd.bindString(3, strPath);
return insertCmd.executeInsert();
*/

ContentValues Val = new ContentValues();
Val.put("GalleryID", strGalleryID);
Val.put("Name", strName);
Val.put("Path", strPath);

long rows = db.insert(TABLE_GALLERY, null, Val);

db.close();
return rows; // return rows inserted.

} catch (Exception e) {
return -1;
}

}

// Select All Data
public String[] SelectAllData() {
// TODO Auto-generated method stub

try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data

String strSQL = "SELECT NAME FROM " + TABLE_GALLERY;
Cursor cursor = db.rawQuery(strSQL, null);

if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getCount()];
/***
* [x] = Name
*/
int i= 0;
do {
arrData[i] = cursor.getString(0);
i++;
} while (cursor.moveToNext());

}
}
cursor.close();

return arrData;

} catch (Exception e) {
return null;
}

}


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

// Re Create on method onCreate
onCreate(db);
}

}

MainActivity.java

package com.myapp;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;


public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get Data from SQLite
final myDBClass myDb = new myDBClass(this);
/*
* for insert statement
myDb.InsertData("1","Picture 1", "pic_a.png");
myDb.InsertData("2","Picture 2", "pic_b.png");
myDb.InsertData("3","Picture 3", "pic_c.png");
myDb.InsertData("4","Picture 4", "pic_d.png");
myDb.InsertData("5","Picture 5", "pic_e.png");
myDb.InsertData("6","Picture 6", "pic_f.png");
myDb.InsertData("7","Picture 7", "pic_g.png");
myDb.InsertData("8","Picture 8", "pic_h.png");
myDb.InsertData("9","Picture 9", "pic_i.png");
*/
final String [] myData = myDb.SelectAllData();

// autoCompleteTextView1
final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, myData);

autoCom.setAdapter(adapter);

// button1
final Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this,
String.valueOf("Your Input : " + autoCom.getText().toString()),
Toast.LENGTH_SHORT).show();
}
});


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

最佳答案

在点击事件上使用它

        add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

myDb.InsertData(actv.getText().toString());

}
});

关于java - 如何单击 AutocompleteTextView 列表上的事件并查询 sqlite。(我有一个示例代码。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393589/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com