我的数据库中有两个表 Placetype和Place
placetype包含所有类型的地方,如resturaunt,酒店,汽车旅馆等
现在我想要当用户点击resturaunt时,例如,他得到所有的returaunt列表而不是所有其他地方,然后resturaunt。我无法做到这一点。请帮忙......
这是我的代码:
Database.java
package com.example.nearbyplaces;
import java.util.ArrayList;
import java.util.List;
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 DatabaseHandler extends SQLiteOpenHelper {
private static final String db_name = "nearby_place";
private static final int db_version = 1;
//tables
private static final String table_placetypes = "placetypes";
private static final String table_places = "table_places";
//column names
private static final String type_id = "type_id";
private static final String type_name = "type_name";
private static final String place_id = "place_id";
private static final String place_name = "place_name";
private static final String place_address = "place_address";
private static final String place_contact = "place_contact";
public DatabaseHandler(Context context) {
super(context, db_name, null, db_version);
// TODO Auto-generated constructor stub
}
// create table queries
String create_table_placetypes = "CREATE TABLE IF NOT EXISTS " + table_placetypes + "("
+ type_id + " INTEGER PRIMARY KEY NOT NULL," + type_name + " TEXT" + ")";
String create_table_places = "CREATE TABLE IF NOT EXISTS table_places (place_id INTEGER PRIMARY KEY NOT NULL, place_name TEXT, place_address TEXT, place_contact TEXT, type_id INTEGER, FOREIGN KEY (type_id) REFERENCES table_placetypes(type_id))";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(create_table_placetypes);
Log.d("creating", "placetypes created");
db.execSQL(create_table_places);
Log.d("creating", "places created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + table_placetypes);
db.execSQL("DROP TABLE IF EXISTS " + table_places);
onCreate(db);
}
// add placetypes
void addplacetypes (placetypes pt) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(type_name, pt.getTypename());
db.insert(table_placetypes, null, values);
db.close();
}
// Getting single placetypes
placetypes getPlacetypes(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(table_placetypes, new String[] {type_id,
type_name }, type_id + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
placetypes pt = new placetypes(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return pt;
}
// Getting All placetypes
public List<placetypes> getAllPlacetypes() {
List<placetypes> placetypesList = new ArrayList<placetypes>();
// Select All Query
String selectQuery = "SELECT * FROM " + table_placetypes;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
placetypes pt = new placetypes();
pt.setTypeid(Integer.parseInt(cursor.getString(0)));
pt.setTypename(cursor.getString(1));
//String name = cursor.getString(1);
//MainActivity.ArrayofName.add(name);
// Adding contact to list
placetypesList.add(pt);
} while (cursor.moveToNext());
}
// return placetype list
return placetypesList;
}
// Getting placetypes Count
public int getPlacetypesCount() {
String countQuery = "SELECT * FROM " + table_placetypes;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public void addplaces(places p) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(place_name, p.getPlace_name());
values.put(place_address, p.getPlace_address());
values.put(place_contact, p.getPlace_contact());
values.put(type_id, p.getT_id());
Log.d("Type ID", String.valueOf(p.getT_id()));
db.insert(table_places, null, values);
db.close();
}
places getPlaces(int pid) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_places, new String[] {place_id, place_name, place_address, place_contact,type_id}, place_id + "=?", new String[] { String.valueOf(pid) } , null, null, null, null);
if(cursor != null)
cursor.moveToFirst();
places p = new places(Integer.parseInt(cursor.getString(0)),
Integer.parseInt(cursor.getString(1)),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4)
);
cursor.close();
return p;
}
public List<places> getAllPlaces(String typeName) {
List<places> placeList = new ArrayList<places>();
//String selectQuery = "SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id ";
//String selectQuery = "SELECT * FROM table_places WHERE table_places.type_id="+Integer.toString(typeid);
//String selectQuery ="SELECT * FROM table_places WHERE table_places.place_name = " +typeName+ "INNER JOIN placetypes ON placetypes.type_id = table_places.type_id";
//String selectQuery = "SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id = table_places.type_id WHERE table_places.place_name = " + typeName + "";
//String selectQuery = "SELECT * FROM " +table_places;
String selectQuery ="SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id WHERE placetypes.type_name='"+typeName+"'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst() )
{
do{
places p = new places();
/*p.setT_id(cursor.getColumnIndex(type_id));
p.setPlace_id(cursor.getColumnIndex(place_id));
p.setPlace_name(cursor.getColumnIndex(place_name));
*/
p.setT_id(cursor.getInt(0));
p.setPlace_id(cursor.getInt(1));
p.setPlace_name(cursor.getString(2));
p.setPlace_address(cursor.getString(3));
p.setPlace_contact(cursor.getString(4));
/*String t_id = cursor.getString(4);
String p_name = cursor.getString(2);
String p_address = cursor.getString(3);
String p_contact = cursor.getString(1);*/
placeList.add(p);
}while(cursor.moveToNext());
}
cursor.close();
return placeList;
}
public int getPlaceCount () {
String selectQuery = "SELECT * FROM " +table_places;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(create_table_places, null);
cursor.close();
return cursor.getCount();
}
}
MainActivity2
package com.example.nearbyplaces;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity2 extends Activity {
private ListView listView;
public static ArrayList<String> ArrayofName = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent check = getIntent();
String x = check.getStringExtra(MainActivity.PLACETYPE);
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
DatabaseHandler db = new DatabaseHandler(this);
if(db.getAllPlaces(x).isEmpty())
{
/**
* CRUD Operations
* */
// Inserting Places2
Log.d("Insert: ", "Inserting ..");
db.addplaces(new places("Pizza Hut", "abc", "123",1));
//db.addplaces(new places("Pizza Hut",null,null));
/* db.addplacetypes(new placetypes("MALLS"));
db.addplacetypes(new placetypes("GAS STATIONS"));
db.addplacetypes(new placetypes("HOTELS"));
db.addplacetypes(new placetypes("MOTELS"));
*/}
// Reading all Places
Log.d("Reading: ", "Reading all places..");
if(ArrayofName.isEmpty())
{
List<places> places = db.getAllPlaces(x);
for (places p : places)
{
String log = "Id: "+p.getPlace_id() +" ,Name: " + p.getPlace_name() +" ,ADDRESS: " + p.getPlace_address() +"CONTACT NO. : " +p.getPlace_contact();
//Writing Places to log
Log.d("Name: ", log);
Log.d("ADDRESS: ", log);
Log.d("CONTACT NO : ", log);
System.out.println(log);
ArrayofName.add(p.getPlace_name());
// ArrayofName.add(p.getPlace_address());
// ArrayofName.add(p.getPlace_contact());
}
}
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ArrayofName);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
// Intent i = new Intent(this,Activity2.class);
// startActivity(i);
// Intent intent = new Intent(MainActivity.this, Activity2.class);
// startActivity(intent);
}
}
);
}
}
logcat的
10-11 19:34:31.160: D/Reading:(12694): Reading all placetypes..
10-11 19:34:31.164: D/Name:(12694): Id: 1 ,Name: RESTURAUNTS
10-11 19:34:31.164: I/System.out(12694): Id: 1 ,Name: RESTURAUNTS
10-11 19:34:31.164: D/Name:(12694): Id: 2 ,Name: MALLS
10-11 19:34:31.164: I/System.out(12694): Id: 2 ,Name: MALLS
10-11 19:34:31.164: D/Name:(12694): Id: 3 ,Name: GAS STATIONS
10-11 19:34:31.164: I/System.out(12694): Id: 3 ,Name: GAS STATIONS
10-11 19:34:31.164: D/Name:(12694): Id: 4 ,Name: HOTELS
10-11 19:34:31.164: I/System.out(12694): Id: 4 ,Name: HOTELS
10-11 19:34:31.164: D/Name:(12694): Id: 5 ,Name: MOTELS
10-11 19:34:31.164: I/System.out(12694): Id: 5 ,Name: MOTELS
10-11 19:34:31.230: D/libEGL(12694): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
10-11 19:34:31.234: D/libEGL(12694): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
10-11 19:34:31.246: D/libEGL(12694): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-11 19:34:31.300: D/OpenGLRenderer(12694): Enabling debug mode 0
10-11 19:34:33.320: D/Insert:(12694): Inserting ..
10-11 19:34:33.320: D/Type ID(12694): 1
10-11 19:34:33.355: D/Reading:(12694): Reading all places..
10-11 19:34:33.363: D/Name:(12694): Id: 0 ,Name: abc ,ADDRESS: 123CONTACT NO. : 1
10-11 19:34:33.363: D/ADDRESS:(12694): Id: 0 ,Name: abc ,ADDRESS: 123CONTACT NO. : 1
10-11 19:34:33.363: D/CONTACT NO :(12694): Id: 0 ,Name: abc ,ADDRESS: 123CONTACT NO. : 1
10-11 19:34:33.363: I/System.out(12694): Id: 0 ,Name: abc ,ADDRESS: 123CONTACT NO. : 1
10-11 19:34:50.090: D/Insert:(12766): Inserting ..
10-11 19:34:50.090: D/Type ID(12766): 1
10-11 19:34:50.113: D/Reading:(12766): Reading all places..
10-11 19:36:08.488: D/Insert:(12877): Inserting ..
10-11 19:36:08.488: D/Type ID(12877): 1
10-11 19:36:08.511: D/Reading:(12877): Reading all places..
答案 0 :(得分:0)
您需要限制查询。向getAllPlaces添加参数:
public List<places> getAllPlaces(String typeName) {
并将您的查询字符串更改为:
String selectQuery ="SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id WHERE placetypes.place_name='"+typeName+"'";
现在无论你在哪里调用getAllPlaces,都要使用类型名称来调用它:
if(db.getAllPlaces(x).isEmpty())
...
List<places> places = db.getAllPlaces(x);
编辑: 对于你的输出问题,我认为你想要的是:
//String log = "Id: "+p.getPlace_id() +" ,Name: " + p.getPlace_name() +" ,ADDRESS: " + p.getPlace_address() +"CONTACT NO. : " +p.getPlace_contact();
//Writing Places to log
Log.d("Test", "Id: "+p.getPlace_id());
Log.d("Test", "Name: "+p.getPlace_name());
Log.d("Test", "Address: "+p.getPlace_address());
Log.d("Test", "Contact No.: "+p.getPlace_contact());