首先,我在main上有这个代码,我从链接中调用JSON数据并将其插入表中然后我将数据查看到自定义列表视图中..这里是主要活动的代码:
public class PenddinOrdersTest extends Activity {
ArrayList<Info> info=new ArrayList<Info>();
static boolean isDataLoaded = false;
ListView list;
ProgressDialog pd;
private String defValue = "N/A";
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pendding_orders);
setTitle("الطلبات الجديدة");
if ( databaseHelper == null )
{
databaseHelper = new InfoServicesNew(this);
}
System.out.println ( "isDataLoaded:" + isDataLoaded );
list = (ListView)findViewById(R.id.listView1);
pd = new ProgressDialog(this);
if ( isDataLoaded )
{
rs = databaseHelper.selectAll(name,img);
SetAdapterList(rs);
}
else
{
new asy().execute("http://192.168.1.113/JsonRoot/transitions/Pendding_orders.json");
}
}
private void SetAdapterList(ArrayList<Info> result)
{
// TODO Auto-generated method stub
CustomAdapter adapter=new CustomAdapter(getApplicationContext(), result);
list.setAdapter(adapter);
System.out.println ( "Size : " + list.getCount() );
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
// TODO Auto-generated method stub
Intent i=new Intent(PenddinOrdersTest.this,Details.class);
int myId=arg2;
object = list.getItemAtPosition(arg2);
Info detail = (Info) object;
String client = detail.getClient();
String orderAmount = detail.getOrderAmount();
String date=detail.getdate();
String orderNumber=detail.getorderNumber();
String upperLimit=detail.getupperLimit();
String debt=detail.getdept();
i.putExtra("myid", ""+myId);
i.putExtra("client", ""+ client);
i.putExtra("orderAmount", ""+orderAmount);
i.putExtra("date", date);
i.putExtra("orderNumber", orderNumber);
i.putExtra("upperLimit", upperLimit);
i.putExtra("debt", debt);
startActivity(i);
finish();
}
});
//
}
public class asy extends AsyncTask<String, String, ArrayList<Info>>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.setTitle("fetching");
pd.setMessage("waiting...");
pd.show();
}
@Override
protected ArrayList<Info> doInBackground(String... params) {
// TODO Auto-generated method stub
//activity is defined as a global variable in your AsyncTask
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(params[0]);
HttpResponse hr = hc.execute(hg);
HttpEntity he = hr.getEntity();
data = EntityUtils.toString(he);
Log.i("data", data);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<Info> sereenlist = new ArrayList<Info>();
sereenlist = getJSONData(data);
return sereenlist;
}
private ArrayList<Info> getJSONData(String data) {
// TODO Auto-generated method stub
String id = null;
String item=null;
String amount=null;
String unit=null;
String price=null;
String total_price=null;
ArrayList<Info> rs = null;
if ( isDataLoaded == false )
{
try
{
JSONObject obj = new JSONObject(data);
JSONArray finalObj = obj.optJSONArray("orders");
for (int i = 0; i < finalObj.length(); i++)
{
final String orderNumber = finalObj.optJSONObject(i).optString(
"order-number");
final String orderAmount = finalObj.optJSONObject(i).optString(
"order-amount");
final String date = finalObj.optJSONObject(i).optString(
"date");
final String client = finalObj.optJSONObject(i).optString(
"client");
final String upperLimit = finalObj.optJSONObject(i).optString(
"upper-limit");
final String debt = finalObj.optJSONObject(i).optString(
"debt");
JSONArray details = finalObj.getJSONObject(i).getJSONArray("details");
for(int j=0; j<details.length(); j++)
{
id = details.getJSONObject(j).optString("id");
item = details.getJSONObject(j).optString("item");
amount = details.getJSONObject(j).optString("amount");
unit = details.getJSONObject(j).optString("unit");
price = details.getJSONObject(j).optString("price");
total_price = details.getJSONObject(j).optString("total-price");
}
long id1=databaseHelper.insert(new Info(client,orderAmount,date,orderNumber,upperLimit,debt,img),name);
databaseHelper.insert_details(new Details_info(id,item,amount,unit,price,total_price,orderNumber),name2);
}
rs = databaseHelper.selectAll(name,img);
Log.i("size", finalObj.length()+"");
}//try end
catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return rs;
}
@Override//
protected void onPostExecute(ArrayList<Info> result)
{
// TODO Auto-generated method stub
//SetAdapterList(result);
isDataLoaded = true;
rs = databaseHelper.selectAll(name,img);
SetAdapterList(rs);
pd.dismiss();
}
}
@Override
public void onStop()
{
super.onStop();
if ( databaseHelper != null )
{
databaseHelper.close();
}
System.out.println ( "db closed" );
}
}
每次我点击一个项目,它会转到详细信息活动,显示来自获取意图的数据..如果我点击(批准)按钮,它应该从自定义适配器删除一行..但功能是'n'工作..这是一个详细信息代码:
public class Details extends Activity {
InfoServicesNew databaseHelper;
String name2=InfoServicesNew.DB_TABLE_NAME5;
@Override
protected void onCreate(Bundle savedInstanceState) {
approve.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Details.this,PenddinOrdersTest.class);
databaseHelper.delete(id,name2);
if(id < 0)
{
Toast.makeText(getApplicationContext(), "unsuccessfull delete", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
}
startActivity(intent);
}
});
details.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
ArrayList<Details_info> rs = null;
//rs=databaseHelper.selectAllDetails(name2);
//Log.i("rs",rs+"");
Intent intent=new Intent(Details.this,Order_info.class);
startActivity(intent);
}
catch(Exception e){
e.printStackTrace();
}
finally{
//databaseHelper.close();
}
//
}
});
}
}
最后这里是sqlite帮助器中的方法体..请帮忙吗?
public long delete(int id, String name2) {
// TODO Auto-generated method stub
return database.delete(name2, KEY_ID + " = " + id, null) ;
}