你好朋友我试图根据我的记录选择启用edittext。XML文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/per_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:gravity="center"
android:layout_marginTop="20dp"/>
<Spinner android:id="@+id/columnToUpdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:gravity="center"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/enterToUpdateString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:inputType="text"
android:hint="@string/EnterTheTextHere"/>
<EditText
android:id="@+id/enterToUpdateInt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:inputType="number"
android:hint="@string/EnterTheNumberHere"/>
<Button
android:id="@+id/clickToUpdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/clickToUpdate"/>
</LinearLayout>
我正在申请的Java代码如下: -
package com.indianic.databaseprac;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class UpdateFrom25Table extends Activity {
String id, column, text;
EditText integertext;
EditText stringText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update_from_25_table);
stringText = (EditText) findViewById(R.id.enterToUpdateString);
integertext = (EditText) findViewById(R.id.enterToUpdateInt);
stringText.setVisibility(View.GONE);
integertext.setVisibility(View.GONE);
Button updateButton = (Button) findViewById(R.id.clickToUpdate);
// Here I am setting the spinner where I am taking the content of the
// table per_ID having all the unique IDs and setting it to the spinner
Spinner spinnerid = (Spinner) findViewById(R.id.per_id);
final Database25TableHandler db = new Database25TableHandler(
getApplicationContext());
List<String> ids = db.gettingid();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ids);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerid.setAdapter(dataAdapter);
// Here I am setting all the column names in the second spinner with the
// help of the cursor.
Spinner spinnerColumn = (Spinner) findViewById(R.id.columnToUpdate);
List<String> column = db.getcolumnname();
ArrayAdapter<String> dataAdaptercolumn = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, column);
dataAdaptercolumn
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerColumn.setAdapter(dataAdaptercolumn);
final String spinnerSelectedID = (String) spinnerid.getSelectedItem();
final String spinnerSelectedcolumn = (String) spinnerColumn
.getSelectedItem();
spinnerColumn.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Log.d("TESTING", parent.getItemAtPosition(pos).toString());
// TODO Auto-generated method stub
if(parent.getItemAtPosition(pos).toString() == "reg_id") {
//integertext.setEnabled(true);
Log.d("TESTING", "LOOOOGGGGG");
integertext.setVisibility(View.VISIBLE);
text = integertext.getText().toString();
} else if (parent.getItemAtPosition(pos).toString() == "first_name") {
//stringText.setEnabled(true);
stringText.setVisibility(View.VISIBLE);
text = stringText.getText().toString();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// Here I am setting all the Items that are selected in the string in
// order to perform the update process.
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.updateRow(spinnerSelectedID, spinnerSelectedcolumn, text);
db.close();
}
});
}
}
这里即使我从微调器中选择了reg_id,它也没有输入第一个if部分作为Log.d(“TESTING”,“LOOOOGGGGG”);没有打印
答案 0 :(得分:1)
试试这个..
如果您比较字符串,则应使用.equels
if(parent.getItemAtPosition(pos).toString().equels("reg_id")) {
//integertext.setEnabled(true);
Log.d("TESTING", "LOOOOGGGGG");
integertext.setVisibility(View.VISIBLE);
text = integertext.getText().toString();
} else if (parent.getItemAtPosition(pos).toString().equels("first_name")) {
//stringText.setEnabled(true);
stringText.setVisibility(View.VISIBLE);
text = stringText.getText().toString();
}
==
总是只比较两个引用(对于非基元,即),即它测试两个操作数是否引用同一个对象。
但是,可以覆盖equals
方法 - 因此两个不同的对象仍然可以相等
修改强>
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("reg_id") || parent.getItemAtPosition(pos).toString().equels("first_name")){
}
答案 1 :(得分:0)
你应该使用.equals(String)方法进行字符串比较(执行简单的'浅'比较)而不是'=='比较(执行'深'比较 - 验证两个字符串对象是否指向内存中的相同实例。)
此外,要切换启用/禁用状态,您需要使用setEnabled(boolean)方法。 setEnabled(true)将设置要启用的edittext,setEnabled(false)将禁用edittext。