【发布时间】:2016-07-28 23:46:06
【问题描述】:
我是 Android 编程和学习 Sqlite 的新手。我正在做一个项目,用户可以在填充的数据库中搜索汽车品牌名称(例如法拉利),结果将在另一个活动中显示为数据库中具有相同品牌的所有汽车型号的列表视图。我一直在寻找不同的方法(ArrayAdapter、Cursor、Intent),但我不确定我是否走在正确的轨道上。我一共有四项活动: CarDbAdapter.java(SqliteDatabase) 包含这个查找汽车品牌的方法
public ArrayList<Car> findCar(String Car) {
String car = new String();
ArrayList<Car> car= new ArrayList<Car>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM cars WHERE brand=?", new
String[]{car});
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
Car c = new Car
(cursor.getInt(cursor.getColumnIndex("id")),cursor.getString(cursor.getColumnIndex("brand")),
cursor.getString(cursor.getColumnIndex("model")),
cursor.getString(cursor.getColumnIndex("reviews")));
car.add(c);
cursor.moveToNext();
}
}
cursor.close();
return car;
}
RequestCar.java这是用户可以通过edittext和按钮从数据库中请求汽车信息的地方
public class RequestCar extends AppCompatActivity {
CarDbAdapter db;
Button search;
EditText brand, model, reviews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request_car);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new CarDbAdapter(this);
search = (Button) findViewById(R.id.searchButton);
brand= (EditText) findViewById(R.id.brand);
model= (EditText) findViewById(R.id.model);
reviews = (EditText) findViewById(R.id.reviews);
searchCar();
}
// This is where it goes wrong. Do I need to initialise another arraylist?As I've already done it on CarDbAdapter.java
public void searchCar() {
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Car> carIntent= db.searchCar(brand.getText().toString());
Intent intent = new Intent(RequestCar.this, CarList.class);
intent.putStringArrayListExtra("car", carIntent);
startActivity(intent);
}
});
CarList.java,我从主要活动中调用意图并将结果作为列表查看。
public class CarList extends AppCompatActivity {
CarDbAdapter db;
ListView carList;
String uriString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new CarDbAdapter(this);
carList = (ListView) findViewById(R.id.carList);
handleIntent();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void handleIntent(){
Intent intent = getIntent();
uriString = intent.getStringExtra("car");
}
}
// This is what I've done so far because I'm getting confused with loads of tutorials and I'm not sure which one to follow. I've read about tutorials suggesting ArrayAdapter for creating the list but first I need to know how to get the Intent from mainactivity then maybe I can learn more about the ArrayAdapter.
Car.java
public class Car{
int id = 0;
String brand = null;
String model = null;
String reviews = null;
public Car(int id, String brand, String model, String reviews) {
this.id = id;
this.brand= brand;
this.model = model;
this.reviews = reviews;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand= brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model= model;
}
public String getReviews() {
return reviews;
}
public void setReviews(String reviews) {
this.reviews= reviews;
}
}
任何帮助将不胜感激。如果有人可以指导我使用我可以遵循的现有教程,那就太棒了。谢谢
【问题讨论】:
标签: android sqlite listview android-intent android-arrayadapter