本文介绍了EditText字段到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,
我在开发应用程序时遇到了这个问题。我将在下面解释这个问题。
我有两个EditText字段和一个按钮(用于搜索)。然后我想从edittext字段中获取数据并将它们作为搜索查询。我想要读取用户输入的数据来自两者EditText字段,点击搜索按钮后,我需要从数据库中检索相应的数据。所以请在这个问题上帮助我。
Hai,
I have come across with the problem when developing an application.I will explain the problem below.
I had two EditText field and One button(for search).then i want to get the data from the edittext fields and make them as a search query.Means i Want to read the user entered data from both the EditText field and after click the search button i need to retrieve the respective data from the database.So please help me in this issue.
MainActivity.java
package com.example.qwe;
import java.io.IOException;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class MainActivity extends ActionBarActivity {
ExternalDbOpenHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
String query1 = intent.getStringExtra(SearchManager.QUERY);
showData(query, query1);
}
// Intent another = getIntent();
// if (Intent.ACTION_SEARCH.equals(another.getAction())) {
// String query1 = another.getStringExtra(SearchManager.QUERY);
// showData(query1);
// }
final EditText searchQuery = (EditText) findViewById(R.id.search_query);
searchQuery.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
final String searchData = searchQuery.getText().toString();
showData(searchData, null); // passing string to search in
// your
// database to your method
return true;
}
return false;
}
});
final EditText searchQuery2 = (EditText) findViewById(R.id.search_query2);
searchQuery2.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String searchData1 = searchQuery2.getText().toString();
showData(searchData1, null); // passing string to search in
// your
// database to your method
return true;
}
return false;
}
});
Button search = (Button) findViewById(R.id.button1);
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String search = searchQuery.getText().toString();
showData(search, null);
}
});
}
private void showData(String searchData, String searchdata1) {
String[] from = new String[] { "BusNumber" };
int[] to = new int[] { R.id.TextView1 };
dbHelper = new ExternalDbOpenHelper(this);
try {
dbHelper.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cursor c = dbHelper.getData(searchData, searchdata1);
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.list, c, from, to);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}
}
And the ExternalDbOpenHelper.java
package com.example.qwe;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ExternalDbOpenHelper extends SQLiteOpenHelper {
private static String DB_NAME = "busDetail.sqlite3";
private SQLiteDatabase db;
private final Context context;
private String DB_PATH;
public ExternalDbOpenHelper(Context context) {
// TODO Auto-generated constructor s
super(context, DB_NAME, null, 1);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase()
推荐答案
这篇关于EditText字段到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!