android自定义数据库中的微调器数据

时间:2016-10-09 11:53:02

标签: android spinner android-custom-view

我正在制作一个自定义微调器,它在一个项目微调器内显示两个数据。下面的代码是我对微调器的实现,但是我似乎没有得到任何数据值。我能够获取数据,如果我只使用一个简单的微调器项获得一行,但在此我试图获得两个值,所以我决定为此创建一个自定义微调器适配器,但不幸的是它无法正常工作。 数据来自数据库

SpinnerAdapter

public class SpinnerAdapter extends BaseAdapter {

    Context context;
    List<String> text;  
    LayoutInflater layoutInflater;

    public SpinnerAdapter(Context applicationContext, List<String> text) {
        this.context = applicationContext;
        this.text = text;
        layoutInflater = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.custom_spinner, null);
        TextView textView = (TextView) convertView.findViewById(R.id.data);
        textView.setText(text.get(position));
        TextView textView2 = (TextView) convertView.findViewById(R.id.subdata);
        textView2.setText(text.get(position));
        return convertView;
    }

}

微调

   List<String> spinnerData= db.getData(id);
   Spinner spin = (Spinner) findViewById(R.id.spinner);
   spin.setOnItemSelectedListener(this);
   SpinnerAdapter spinnerAdapter = new SpinnerAdapter(this, spinnerData);
   spin.setAdapter(spinnerAdapter);

custom_spinner.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/subdata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</LinearLayout>

数据库方法查询

public List<String> getData(String id) {
    List<String> data = new ArrayList<>();
    String selectQuery = "SELECT * FROM TABLE WHERE id = '" + id + "'";
    sqLiteDatabase = this.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            data.add(cursor.getString(0));
            data.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    cursor.close();
    sqLiteDatabase.close();
    return data;
}

0 个答案:

没有答案