1
這是我正在開發的程序的第一個和第二個活動文件..在第一個文件..用戶正在進入一個城市..將在數據庫和相應的郵政編碼將被退回。如果城市不在數據庫中,用戶可以在第二個活動文件中輸入城市和郵政編碼..這工作正常。我遇到了第一個活動文件的問題..請幫助。Android應用程序檢索每個城市的郵政編碼
package com.stdcode;
import java.util.ArrayList;
import java.util.HashMap;
//import com.stdcode.activity2;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class activity1 extends Activity {
EditText city;
Button add,show1;
RadioGroup choose;
String k;
@Override
public void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//final ContentValues values = new ContentValues();
city=(EditText)findViewById(R.id.editText1);
add=(Button)findViewById(R.id.add);
choose=(RadioGroup)findViewById(R.id.radio01);
show1=(Button)findViewById(R.id.button1);
k=city.getText().toString();
createDatabase();
}
catch(Exception e)
{
}
show1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
try{
if(k == "CITY")
{
String[] result_columns=new String[]{"_id","CITY","CODE"};
Cursor cursor = db.query(TABLE_NAME, result_columns,
"CITY" +" like '%?%'", new String[]{"k"}, null, null, null);
if(cursor != null)
{
while(cursor.moveToNext())
{
String xnewcode=cursor.getString(1);
Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show();
}
}
//db.close();
}}
catch(Exception e)
{
Toast.makeText(activity1.this,"Fault in showing " + e,Toast.LENGTH_LONG).show();
}
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(activity1.this,activity2.class);
startActivity(intent);
}
});}
public static final String DATABASE_NAME="myDB.db";
public static final String TABLE_NAME = "citycodes";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + "(_id integer primary key autoincrement, CITY text not null,CODE text not null);";
SQLiteDatabase db;
public void createDatabase() {
db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
db.execSQL(TABLE_CREATE);
city.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//TODO Auto-generated method stub
db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
try{
if(k == "CITY")
{
String[] result_columns=new String[]{"_id","CITY","CODE"};
Cursor cursor = db.query(TABLE_NAME, result_columns,
"CITY" +"=?", new String[]{"k"}, null, null, null);
cursor.moveToFirst();
String xnewcode=cursor.getString(0);
Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show();
cursor.moveToNext();
//if(cursor.moveToFirst()){
//do
//{
//String xnewcity=cursor.getString(0);
//String xnewcode=cursor.getString(1);
//Toast.makeText(activity1.this,xnewcity,Toast.LENGTH_LONG).show();
//Toast.makeText(activity1.this,xnewcode,Toast.LENGTH_LONG).show();
//}while(cursor.moveToNext());
//}
//db.close();
}}
catch(Exception e)
{
Toast.makeText(activity1.this,"Fault in showing " + e,Toast.LENGTH_LONG).show();
}
return true;
}
});
}}
這是第二個活動文件..以防萬一你需要它。
package com.stdcode;
//import static com.stdcode.activity1.DATABASE_NAME;
//import static com.stdcode.activity1.TABLE_NAME;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class activity2 extends activity1 {
EditText newcity;
EditText newcode;
Button save,back;
String city1,code1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
newcity = (EditText) findViewById(R.id.EditText01);
newcode = (EditText) findViewById(R.id.EditText02);
back=(Button)findViewById(R.id.back01);
save=(Button)findViewById(R.id.save01);
back.setOnClickListener(new ClickHandler());
save.setOnClickListener(new ClickHandler());
}
public class ClickHandler implements OnClickListener
{
public void onClick(View v)
{
db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
try
{
ContentValues newValues=new ContentValues();
if(v == save)
{
city1=newcity.getText().toString();
code1=newcode.getText().toString();
newValues.put("CITY", city1);
newValues.put("CODE", code1);
db.insert(TABLE_NAME, null, newValues);
Toast.makeText(activity2.this,"Values Inserted",Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(activity2.this,"Error " + e,Toast.LENGTH_LONG).show();
}
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent(activity2.this, activity1.class);
startActivity(i);
}
});
}
}
}
你需要解釋一下你的問題。此外,請將您的代碼修改到理解此問題所需的最低限度。這只是太多的代碼。 – Aleadam 2011-06-02 18:07:56
問題出在兩個方面..如何在數據庫的editText字段中搜索用戶輸入的數據..然後如何顯示該城市的相應代碼。兩者都與遊標有關。請幫忙。 – mohit 2011-06-02 18:55:10
這兩個問題都出現在第一個活動文件中。第二個活動文件工作正常。 – mohit 2011-06-02 18:55:51