私は1つのスピナーと1つのボタンとListViewを持っています。Androidのsqliteデータベースを使ったSpinner
私はスピナーから項目を選択したいと思って、ボタンをクリックした後、スピナーとデータベースの内容の一致がリストビューに表示されていなければなりません。データベースの値は文字列データ型です。
どうすればいいですか?スピナーのために
私は1つのスピナーと1つのボタンとListViewを持っています。Androidのsqliteデータベースを使ったSpinner
私はスピナーから項目を選択したいと思って、ボタンをクリックした後、スピナーとデータベースの内容の一致がリストビューに表示されていなければなりません。データベースの値は文字列データ型です。
どうすればいいですか?スピナーのために
:
setContentView(R.layout.main);
ListView lv=(ListView)findViewById(R.id.listview);
Button b=(Button)findViewById(R.id.button);
Spinner sp=(Spinner)findViewById(R.id.spinner);
final ArrayList<String> options=new ArrayList<String>();
Cursor cursor=dbHelper.getOptions();//method to get all options to be displayed in spinner,coming from your database
if(cursor.getCount()>0)
{
cursor.moveToFirst();
for(int i=0;i<cursor.getCount();i++)
{
options.add(cursor.getString(cursor.getColumnIndex("field_name")));
cursor.moveToNext();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,options);
sp.setAdapter(adapter);
}
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Cursor c=db_helper.getRelatedItems(spinner.getSelectedItem().toString()); //get related data from database according to the item selected in spinner
if(c.getCount()>0)
{
CustomAdapter ca=new CustomAdapter(context,R.layout.listitem_layout,cursor,new String[]{"field 1","field 2"},new int[]{R.id.textview1,R.id.textview2});
//here you would have to pass your custom layout file containing textviews or other component to display your data using CustomAdapter.class which you had prepared
listview.setAdapter(ca);
ca.notifyDataSetChanged();
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
//catch onClick on items of listview here
}
});
}
}
});
、最初のようにアダプタを作成:R.array.monthsはstring.xmlをファイルに作成し、その後ウルデータベースを開く文字列配列されている。ここ
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.months, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//:
if(mydb_expenditure!=null)
{
mydb_expenditure.close();
}
mydb_expenditure=this.openOrCreateDatabase("Database_name", MODE_PRIVATE, null);
if(mydb_expenditure!=null)
{
Cursor cursor = mydb_expenditure.rawQuery("select column name from "+TableName , null);
cursor.moveToFirst();
int column=cursor.getColumnIndex("column name");
if(cursor!=null)
{
do
{
column=cursor.getInt(column);
adapter12.add(column);
}while(.moveToNext());
}
spinner.setAdapter(adapter12);
}
これはスピナーにur urデータベース列の値を追加します
スピナーに文字列配列が設定されていることを前提として、この手順を実行できます。
イベントでⅰ)データベース
DONEを照会することによってlistadapterを移入するためにstrsel
を使用
int pos=urspinner.getSelectedItemPosition()
で選択した位置の位置を取得し、ボタンクリックリスナーに
strsel=arrstr[pos];
II)で文字列を取得! !!例えば
user932478、uはこの答えはあなたのために有用であると思われる場合は、すべてのユーザーにそれが見えるように、この答えの左にチェックマークをクリックしてもウル答えを高めるためには、比率を受け入れます関連する回答を得る... – Kanika