我是Android编程和学习Sqlite的新手。我正在开发一个项目,用户可以在填充的数据库中搜索汽车品牌名称(例如ferrari),结果将在另一个活动中显示为数据库中具有相同品牌的所有汽车模型的列表视图。我一直在寻找不同的方法(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;
}
}
任何帮助将不胜感激。如果有人可以指导我已经存在的教程,我可以遵循这将是太棒了。谢谢
答案 0 :(得分:0)
在 CarDbAdapter.java 中,这些是前三行:
public ArrayList<Car> findCar(String Car) {
String car = new String();
ArrayList<Car> car= new ArrayList<Car>();
您正在创建一个返回Car(class)类型的ArrayList的方法。然后你传递一个名字为&#34; Car&#34; (这与您的Car类相同)。 在方法内部,您创建一个名为&#34; car&#34;的空字符串,然后创建一个Car(类)类型的ArrayList,名称相同&#34; car&#34; (它会覆盖你之前创建的字符串&#34; car&#34;。
这不仅仅是我们的困惑,也是你的编译器。尝试重命名变量并再次运行应用程序。
这是关于如何重写 findCar 方法的建议:
public ArrayList<Car> findCar(String brand_text) {
ArrayList<Car> cars= new ArrayList<Car>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM cars WHERE brand=?", new
String[]{brand_text});
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")));
cars.add(c);
cursor.moveToNext();
}
}
cursor.close();
return cars;
}
这样,你就可以使用&#34; brand_text&#34;作为您品牌的字符串值(例如&#34; ferrari&#34;),并返回类型为Car的ArrayList汽车。
关于您的疑问,Intents用于(也)将参数传递给其他活动,Cursors(也)用于从数据库返回行,ArrayAdapters用于填充ListView。这不是哪个你应该使用的问题。如果你计划使用你在问题中提到的应用程序,你实际上必须使用所有这些。