AndroidプログラミングとSqliteについて学ぶのが初めてです。私は、ユーザーが人口の多いデータベースで車のブランド名(例えば、フェラーリ)を検索することができるプロジェクトに取り組んでいます。結果はデータベース内の同じブランドの自動車のすべてのモデルのリストビューとして別のアクティビティに表示されます。私はさまざまなメソッド(ArrayAdapter、Cursor、Intent)を探していましたが、正しいトラックにいるかどうかはわかりません。車のブランドにアンドロイドのsqliteデータベースを検索し、リストビューとして別のアクティビティに結果を返します。
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;
}
を見つけること CarDbAdapter.java(SqliteDatabase)この方法を含むRequestCar.javaユーザーがデータベースからのEditTextとボタンから車の情報を要求することができる場所です:私は、合計で4つの活性を有します
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;
}
}
任意の助けが理解されるであろう。誰かが私を導くことができるなら、私が従う既存のチュートリアルは素晴らしいだろう。あなたが車のArrayListの(クラス)型を返すメソッドを作成している
public ArrayList<Car> findCar(String Car) {
String car = new String();
ArrayList<Car> car= new ArrayList<Car>();
:あなたのCarDbAdapter.javaで
これは実際に私を多く助けました。ありがとうございました!! –