gpt4 book ai didi

java - 如何将数据从 Activity 发送到 java 类

转载 作者:行者123 更新时间:2023-11-30 10:37:24 26 4
gpt4 key购买 nike

如何通过微调器选择将 int 值从 locwebsActivity 发送到 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类:

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;
}

}

最佳答案

你可以使用这个:

@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);

关于java - 如何将数据从 Activity 发送到 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com