如何将活动中的数据发送到java类

时间:2016-10-21 02:00:21

标签: java sqlite android-studio

如何通过微调器选择将locwebsActivity的int值发送到DbBackend.java。我试图将旋转器位置发送到类中,以便从数据库中读取相关数据。

由于

public class locwebsActivity extends AppCompatActivity {

private ArrayAdapter<String> listAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_locwebs);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    final DbBackend dbBackend = new DbBackend(locwebsActivity.this);
    String[] spinnerLists = dbBackend.getAllSpinnerContent();

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(locwebsActivity.this,android.R.layout.simple_spinner_item, spinnerLists);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            return;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}
}

java class:

public class DbBackend extends DbObject {

public DbBackend(Context context) {
    super(context);
}

public String[] getAllSpinnerContent(){

    String query = "Select * from content";
    Cursor cursor = this.getDbConnection().rawQuery(query, null);
    ArrayList<String> spinnerContent = new ArrayList<String>();
    if(cursor.moveToFirst()){
        do{
            String word = cursor.getString(cursor.getColumnIndexOrThrow("content"));
            spinnerContent.add(word);
        }while(cursor.moveToNext());
    }
    cursor.close();

    String[] allSpinner = new String[spinnerContent.size()];
    allSpinner = spinnerContent.toArray(allSpinner);

    return allSpinner;
}

}

1 个答案:

答案 0 :(得分:1)

你可以用这个:

@Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            SharedPreferences sendnumber = getSharedPreferences("name", MODE_PRIVATE);
            SharedPreferences.Editor editor = sendnumber.edit();
            editor.putString("edtTextValue", pro);
            editor.commit();

            return;
        }

在DbBackend中使用:

    Context c = Main212Activity.getContext();

    SharedPreferences sendnumber = c.getSharedPreferences("name", c.MODE_PRIVATE);
    String Uname = sendnumber.getString("edtTextValue", "");

    int Unameint = Integer.valueOf(Uname);