我是Android开发的新手。我面临的问题是我想从停止名称获取stop_id。我有一个数据库,它包含所有必需的数据。 当我直接在数据库中运行查询,所以它给了我正确的答案,但它在android中不起作用。 如果我使用getString(0)函数,它会给我null 如果使用getInt(0)函数给我0,请帮帮我..
这是我的代码:
public class Bus_Schedule_tab3 extends Fragment {
DataBase_Class db;
Button btn_go;
EditText source_txt;
EditText destination_txt;
int source_id ;
int destination_id ;
String Source_Destination_querry;
String sourceTxt , destinationTxt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
btn_go = (Button) rootView.findViewById(R.id.btn_go);
db = new DataBase_Class(getActivity());
source_txt = (EditText) rootView.findViewById(R.id.edtxt_source);
destination_txt = (EditText) rootView.findViewById(R.id.edtxt_destination);
getOnlySource();
return rootView;
}
public void getOnlySource() {
btn_go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer buffer = new StringBuffer();
Cursor c_sourse_id = db.GetOnlySource("select stop_id from stops_table where stop_name = '"+source_txt.getText().toString()+"'");
Cursor c_destination_id = db.GetOnlySource("select stop_id from stops_table where stop_name = '"+destination_txt.getText().toString()+"'");
while (c_sourse_id.moveToNext()) {
source_id = c_sourse_id.getInt(0);}
while (c_destination_id.moveToNext()) {
destination_id = c_destination_id.getInt(0);}
buffer.append(source_id +" "+ destination_id);
showMessage("Data",buffer.toString());
}
});
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
答案 0 :(得分:0)
使用它:
cursor.getInt(cursor.getColumnIndex(USER_ID))
其中USER_ID
是列的名称
答案 1 :(得分:0)
检查你的db对象是否为null,因为你没有将非null对象作为getOnlySource()方法中的参数传递。