dimanche 31 janvier 2016

How to getPosition in sqlite database knowing a column's item value (Android)

I have a static SQLite database with a table of 10 columns and 70+ rows, where the "Description" column populates a Spinner.

When the user select a description from the spinner I'd like to getPosition of it inside the database's table using cursor, in order to accordingly get other columns' values of the same row.

This is my Activity code:

public class FoodActivity extends Activity implements AppCompatCallback {

Spinner sp;
ArrayList<Map<String, String>> names = new ArrayList<Map<String, String>>();
//ArrayAdapter<String> adapter;
Toolbar toolbar;
private AppCompatDelegate delegate;
String name;
SimpleAdapter adapter;
Cursor c;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //TOOLBAR
    delegate = AppCompatDelegate.create(this, this);
    delegate.onCreate(savedInstanceState);
    delegate.setContentView(R.layout.items_activity);
    toolbar = (Toolbar) findViewById(R.id.toolbar_six);
    delegate.setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    sp = (Spinner) findViewById(R.id.spinnerItems);

    // database read
    DatabaseConnectorItems db = new DatabaseConnectorItems(this);
    db.open();
    c = db.getAllItems();

    while(c.moveToNext()) {
        Map<String, String> data = new HashMap<String, String>(2);
        data.put("Description", c.getString(c.getColumnIndex("Description")));
        data.put("Size", c.getString(c.getColumnIndex("Size")));
        names.add(data);
    }
    db.close();

    adapter = new SimpleAdapter(this, names, R.layout.items_list,
            new String[] {"Description", "Size"},
            new int[] {R.id.itemTextOne, R.id.itemTextTwo});

    // set to spinner
    sp.setAdapter(adapter);
    sp.setBackgroundColor(getResources().getColor(R.color.TextColor_White));
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            name = sp.getSelectedItem().toString();

            //c.getPosition();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // nothing
        }

    });

}

@Override
public void onSupportActionModeStarted(ActionMode mode) {

}

@Override
public void onSupportActionModeFinished(ActionMode mode) {

}

}

I'm trying with c.getPosition( different methods here ) but with no results

Aucun commentaire:

Enregistrer un commentaire