为什么不设置Spinner Android的值。我用Cursor?

时间:2015-06-20 04:24:51

标签: android database cursor spinner

我在数据库中有一个名为 PrimaryData 的表,其中包括 PrimaryDataId PrimaryDataCode 字段。我的问题是,当光标从数据库返回数据, Spinner 不设置光标的

xml微调文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="14sp"
    android:textColor= "#000000"
    android:spinnerMode="dialog" />

要放置PrimaryDataId和PrimaryDataCode的PrimaryData类。该类相当于数据库表。

package com.example.proposaldashborad;

public class PrimeryData {
    private int primeryDataId;
    private String primeryDataCode;


    @Override
    public String toString() {
        return this.primeryDataCode; // What to display in the Spinner list.
    }

    public int getPrimaryDataId() {
        return primeryDataId;
    }

    public void setPrimaryDataId(int value) {
        this.primeryDataId = value;
    }

    public int getPrimaryDataCode() {
        return primeryDataId;
    }

    public void setPrimaryDataCode(String value) {
        this.primeryDataCode = value;
    }
}

此代码填充Spinner

private void GetPrimaryDataForSpinner(String primaryDataName, Cursor cr) {   
    cr.moveToFirst();
    //array_spinner = new String[cr.getCount()];
    ArrayList<PrimeryData> pds =  new ArrayList<PrimeryData>() ;

    do {
        //array_spinner[i] = cr.getString(cr.getColumnIndex("PrimeryDataCode"));

        PrimeryData pd = new PrimeryData() ;
        pd.setPrimaryDataId(cr.getInt(cr.getColumnIndex("PrimeryDataId")));
        pd.setPrimaryDataCode(cr.getString(cr.getColumnIndex("PrimeryDataCode")));
        pds.add(pd) ;   

    } while (cr.moveToNext());

    cr.close();

    if (primaryDataName == "Admin") {
        adapterAdmin = new ArrayAdapter<PrimeryData>(this,R.layout.spinner, pds);
        sAdmin = (Spinner) findViewById(R.id.spinnerAdmin);
        sAdmin.setAdapter(adapterAdmin);        
    }
}

这是从数据库返回数据时的代码。

 private Spinner sAdmin;
    private ArrayAdapter<PrimeryData> adapterAdmin;
    db = new DBAdapter(getApplicationContext());
    db.open();
    cr = db.getViewMT26(entityID);
    cr.moveToFirst();

    if(cr.getCount()>0) {
                PrimeryData pd = new PrimeryData();
                pd.setPrimaryDataId(cr.getInt(cr.getColumnIndex("AdminId")));
                   pd.setPrimaryDataCode(cr.getString(cr.getColumnIndex("Admin")));
                   sAdmin.setSelection(adapterAdmin.getPosition(pd));
           }
    cr.close();
    db.close();

我的主要问题是为什么

  

adapterAdmin.getPosition(pd));

返回-1?

2 个答案:

答案 0 :(得分:0)

您必须初始化您的adapterAdmin,并通过setAdapter方法将其设置为您的微调器。 此外,如果您只使用PrimeryData列表,那么每个微调器项目上显示的文本将是PrimeryData(实例)toString()。

答案 1 :(得分:0)

按照此代码在Spinner中设置值

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener {
 Spinner spinnerOsversions;
 TextView selVersion;
 private String[] state = { "Cupcake", "Donut", "Eclair", "Froyo",
   "Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
   "kitkat" };

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  System.out.println(state.length);
  selVersion = (TextView) findViewById(R.id.selVersion);
  spinnerOsversions = (Spinner) findViewById(R.id.osversions);
  ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, state);
  adapter_state
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinnerOsversions.setAdapter(adapter_state);
  spinnerOsversions.setOnItemSelectedListener(this);

  }

  public void onItemSelected(AdapterView<?> parent, View view, int position,
   long id) {
  spinnerOsversions.setSelection(position);
  String selState = (String) spinnerOsversions.getSelectedItem();
  selVersion.setText("Selected Android OS:" + selState);
 }

  @Override
 public void onNothingSelected(AdapterView<?> arg0) {
  // TODO Auto-generated method stub

  }

}