如何将数据从活动发送到 java 类
Posted
技术标签:
【中文标题】如何将数据从活动发送到 java 类【英文标题】:How to send data from an activity to a java class 【发布时间】:2017-03-03 04:59:24 【问题描述】:如何通过微调器选择将 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;
【问题讨论】:
【参考方案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);
【讨论】:
@Damien Paul 我可以对这个答案发表你的看法吗?以上是关于如何将数据从活动发送到 java 类的主要内容,如果未能解决你的问题,请参考以下文章