-1

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

答えて

0

ありがとう、これらは、最初の3行です。 "Car"という名前のStringを渡します(Carクラスと同じです)。 メソッドの中で、名前が "car"の空文字列を作成し、同じ名前のcar(以前に作成された文字列 "car"を上書きする)Car(class)型のArrayListを作成します。

これは私たちを混乱させ、また、あなたのコンパイラされていない変数の名前を変更して再度アプリケーションを実行してみてください

をこれは私があなたの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; 

を}

このようにして、ブランドの文字列値(「ferrari」など)として「brand_text」を使用して、クラスタイプのCarのArrayList車を返します。

インテントは、他のアクティビティにパラメータを渡すためにも使用されます。カーソルはデータベースから行を返すためにも使用され、ArrayAdapterはListViewに挿入されます。それはあなたが使用すべきであるの問題ではありません。質問で言及したようにアプリケーションを使用する予定の場合は、実際にすべてを使用する必要があります。

+0

これは実際に私を多く助けました。ありがとうございました!! –

関連する問題