【问题标题】:Search android sqlite database, return results to another activity as listview搜索android sqlite数据库,将结果作为listview返回到另一个活动
【发布时间】: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


    【解决方案1】:

    在您的 CarDbAdapter.java 中,前三行是:

    public ArrayList<Car> findCar(String Car) {
    String car = new String();
    ArrayList<Car> car= new ArrayList<Car>();
    

    您正在创建一个返回 Car(类)类型的 ArrayList 的方法。然后你传递一个名为“Car”的字符串(这与你的 Car 类相同)。 在该方法中,您创建一个名为“car”的空字符串,然后创建一个 Car(类)类型的 ArrayList,其名称为“car”(它会覆盖您之前创建的字符串“car”。

    这不仅让我们感到困惑,也让您的编译器感到困惑。尝试重命名变量并再次运行您的应用程序。

    这是我将如何重写您的 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;
    

    }

    这样,您使用“brand_text”作为品牌的字符串值(例如,“ferrari”),并返回 Car 类类型的 ArrayList 汽车。

    至于您的疑问,Intents 用于(也)将参数传递给其他活动,Cursors 用于(也)从您的数据库中返回行,而 ArrayAdapters 用于填充您的 ListViews。这不是您应该使用哪个的问题。如果您打算使用问题中提到的应用程序,您实际上将不得不使用所有这些。

    【讨论】:

    • 这实际上对我帮助很大。谢谢!!
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2015-01-01
    相关资源
    最近更新 更多