我尝试为下面给出的发送作业配置文件创建并保存脱机SQLite数据库。但是没有创建数据库。请告诉我如何创建SQLIite数据库并在文件资源管理器中表示它。
Fragment.java
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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;
import android.widget.TextView;
import android.widget.Toast;
import com.sunmobileappnow.mobileappnow.*;
public class FragmentSendJob extends Fragment implements OnClickListener {
// this Fragment will be called from MainActivity
private Button submit,cancel;
private EditText ename,mobno,picktime,unit,street,suburb,destination,fare,city;
private Spinner state,group;
private ViewGroup vgroup ;
private String sename,smobno,spicktime,sunit,sstreet,ssuburb,sstate, ssendjob,scity,sdestination,sfare,sgroup,login_token;
private static FragmentDialog dialog;
private SendJobDataBase db;
static final int DIALOG_ID = 0;
private Typeface ftype;
private SharedPreferences pref1;
String latitude,longitude;
public FragmentSendJob(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.sendjob_fragment, container, false);
pref1=this.getActivity().getSharedPreferences("LocDetails", getActivity().MODE_WORLD_READABLE);
latitude = pref1.getString("latitude","latitude");
longitude = pref1.getString("longitude","longitude");
ename = (EditText) rootView.findViewById(R.id.ename);
mobno = (EditText) rootView.findViewById(R.id.mobno);
picktime = (EditText) rootView.findViewById(R.id.picktime);
unit = (EditText) rootView.findViewById(R.id.unit);
street = (EditText) rootView.findViewById(R.id.street);
suburb = (EditText) rootView.findViewById(R.id.suburb);
destination = (EditText) rootView.findViewById(R.id.destination);
state = (Spinner) rootView.findViewById(R.id.state);
fare=(EditText) rootView.findViewById(R.id.fare);
group = (Spinner) rootView.findViewById(R.id.group);
city = (EditText) rootView.findViewById(R.id.city);
vgroup = (ViewGroup) rootView.findViewById(R.id.rel);
submit = (Button) rootView.findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new SendJob().execute();
Intent intent =new Intent(getActivity(),GetCurrentLocation.class);
startActivity(intent);
System.out.println(">>>>>>>>>>>>>>>>>"+latitude+"***********");
System.out.println(">>>>>>>>>>>>"+longitude+"***********");
}
});
cancel = (Button) rootView.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0, count = vgroup.getChildCount(); i < count; ++i) {
View view = vgroup.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
}
});
String[] mystate= new String[]{"Select state","New South Wales","Victoria","Queensland","Northern Territory","Western Australia","South Australia"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
scity = state.getSelectedItem().toString();
String s1=arg0.getItemAtPosition(pos).toString();
if(s1.equals("New South Wales"))
city.setText("Sydney");
else if(s1.equals("Victoria"))
city.setText("Melbourne");
else if(s1.equals("Queensland"))
city.setText("Brisbane");
else if(s1.equals("Northern Territory"))
city.setText("Darwin");
else if(s1.equals("Western Australia"))
city.setText("Perth");
else if(s1.equals("South Australia"))
city.setText("Adelaide");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
String[] mygroups= new String[]{"Select Groups","My Group","My Secondary group","Everyone"};
ArrayAdapter<String> adapters = new ArrayAdapter<String>(this.getActivity(),
R.layout.listrow, mygroups);
group.setAdapter(adapters);
group.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
ssendjob = group.getItemAtPosition(pos).toString();
//gender.setText(setgender);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
private class SendJob extends AsyncTask<String, String, String[]> {
ProgressDialog pDialog = new ProgressDialog(getActivity());
@Override
protected String[] doInBackground(final String... params)
{
ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
{
HttpClient httpclient = new DefaultHttpClient();
try
{
JSONObject job= new JSONObject();
sename = ename.getText().toString();
smobno = mobno.getText().toString();
spicktime = picktime.getText().toString();
sunit = unit.getText().toString();
sstreet = street.getText().toString();
ssuburb = suburb.getText().toString();
sstate = state.getSelectedItem().toString();
scity = city.getText().toString();
sdestination = destination.getText().toString();
sgroup = group.getSelectedItem().toString();
sfare = fare.getText().toString();
sename.replace("" ,"%20");
smobno.replace("" ,"%20");
spicktime.replace("" ,"%20");
sunit.replace("" ,"%20");
sstreet.replace("" ,"%20");
sstate.replace("" ,"%20");
ssuburb.replace("" ,"%20");
scity.replace("" ,"%20");
sdestination.replace("" ,"%20");
sgroup.replace("" ,"%20");
sfare.replace("" ,"%20");
job.put("name",sename);
job.put("mobile_no",smobno);
job.put("pickup_time",spicktime);
job.put("unit_no",sunit);
job.put("street_name",sstreet);
job.put("state",sstate);
job.put("suburb",ssuburb);
job.put("city",scity);
job.put("destination",sdestination);
job.put("group",sgroup);
job.put("fare",sfare);
job.put("latitude",latitude);
job.put("longitude",longitude);
job.put("status_key","2");
job.put("method","send_job");
job.put("login_token",login_token);
StringEntity se = new StringEntity(job.toString());
HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
Log.i("response", data);
System.out.println("response "+data);
String call;
call = data;
System.out.println("print me............."+call);
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
db = new SendJobDataBase(getApplicationContext());
db.insert(sename,smobno, spicktime, sunit, sstreet,sstate, ssuburb,scity, sdestination, sgroup, sfare);
db.open();
if(jo.getString("err-code").equals("0"))
{
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Alert!!!");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
pDialog.dismiss();
dialog.dismiss();
}
});
getActivity().runOnUiThread(new Runnable() {
public void run() {
alert.show();
}
});
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Alert !");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
});
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
pDialog.dismiss();
alert.show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return params;
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String[] result)
{
super.onPostExecute(result);
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
Android有一个可以使用的内置SQLite DB。
创建数据库表
public abstract class DBManager extends SQLiteOpenHelper{
public DBManager(Context context,String tableName, String [] columns) {
super(context, “test.db”, null, 1);
}
执行SQL代码
<uses-permission android:name=”android.permission.CALL_PHONE”></uses-permission>
db.execSQL( “create table ...” );
插入数据:
getWritableDatabase().insert(tableName, null, values);
获取数据
getReadableDatabase().query(...);
希望这有帮助