gpt4 book ai didi

java - 无法将字符串值从一个 java 类传递到另一类

转载 作者:行者123 更新时间:2023-12-02 05:46:10 24 4
gpt4 key购买 nike

我正在尝试发送从微调器访问的字符串值,并将其发送到数据帮助器类以运行查询并将值插入在另一个 Activity 上运行的另一个微调器中

类内获取字符串值的方法

NewTask.java

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

// On selecting a spinner item
label = spinnerComp.getSelectedItem().toString(); //storing value of spinner in string label
System.out.println("NewTask label="+label);

toRetDeptString(); //it will call return string method


// Showing selected spinner item
Toast.makeText(spinnerComp.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();
}

现在返回字符串方法也在 NewTask.java 中声明

    public String toRetDeptString() {
System.out.println("NewTask String label="+label);
return label;
}

现在我正在尝试访问标签字符串 dataHelper 类的值,该类根据字符串标签内的值运行查询

DataHelper.java 内部在 DatabaseHelper 类里面

    public List<String> getAllLabelsDept(){

NewTask a = new NewTask();
String n1= a.toRetDeptString(); //this one doesn't worked

// String n1 = a.label; //this one doesn't worked

// String n1="umbrella"; //this method worked by directly assiging string value
System.out.println("a="+n1);
List<String> labels = new ArrayList<String>();


// Query
String selectQuery = "select " + Dept + " from "+TABLE_NAME +" where " + Comp + " = '" + n1 +"'";

SQLiteDatabase db1 = this.getReadableDatabase();
Cursor cursor = db1.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(cursor.getColumnIndex(Dept)));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db1.close();

// returning lables
return labels;
}

Select_Dept.java

  public class Select_Dept extends Activity implements OnItemSelectedListener{
Spinner spinnerDept;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select__dept);

spinnerDept = (Spinner) findViewById(R.id.spinnerDept);
spinnerDept.setOnItemSelectedListener(this);
loadSpinnerDataDept();
}

private void loadSpinnerDataDept() {
// database handler
DatabaseHelper db = new DatabaseHelper(getApplicationContext());

// Spinner Drop down elements
List<String> lables = db.getAllLabelsDept();

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);

// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinnerDept.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

// On selecting a spinner item
String label = spinnerDept.getSelectedItem().toString();

// Showing selected spinner item
Toast.makeText(spinnerDept.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.select__dept, menu);
return true;
}

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

}

}

Logcat 的输出:-

 - 06-05 12:30:13.369: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.EzyCloud.ezytask/.MainActivity }
- 06-05 12:30:14.160: I/ActivityManager(59): Displayed activity com.EzyCloud.ezytask/.MainActivity: 562 ms (total 562 ms)
06-05 12:30:16.139: I/ActivityManager(59): Starting activity: Intent { cmp=com.EzyCloud.ezytask/.NewTask }
- 06-05 12:30:16.299: I/System.out(280): NewTask label=umbrella
- 06-05 12:30:16.299: I/System.out(280): NewTask String label=umbrella
- 06-05 12:30:16.559: I/ActivityManager(59): Displayed activity com.EzyCloud.ezytask/.NewTask: 406 ms (total 406 ms)
- 06-05 12:30:19.140: I/ActivityManager(59): Starting activity: Intent { cmp=com.EzyCloud.ezytask/.Select_Dept }
- 06-05 12:30:19.339: I/System.out(280): NewTask String label= //no value accesed
- 06-05 12:30:19.339: I/System.out(280): a= **//no value accesed**
- 06-05 12:30:19.699: I/ActivityManager(59): Displayed activity - com.EzyCloud.ezytask/.Select_Dept: 508 ms (total 508 ms)
- 06-05 12:30:24.849: D/dalvikvm(128): GC_EXPLICIT freed 202 objects / 9800 bytes in 145ms

但由于一些问题,无法访问字符串值

最佳答案

NewTask.java 中,您在其自己的范围内作为默认对象调用 toRetDeptString()

DataHelper.java中,您正在创建一个新对象并调用该方法。显然该字符串将为空。

要将字符串数据传递给另一个 Activity ,您可以尝试使用 intents 。否则,您必须将值设置为模型对象并将对象引用本身传递给下一个 Activity ,而不是创建新对象并访问该方法。

关于java - 无法将字符串值从一个 java 类传递到另一类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053957/

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