这是我从在线数据库显示数据的活动。我想要做的是 - 当点击列表视图中的项目时,它可以删除项目。但它不起作用,你能帮助我吗?。
这是我的代码:
ListOfOrders.java
package com.system.receivingoforder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.system.receivingoforder.app.AppConfig;
import com.system.receivingoforder.app.AppController;
public class ListOfOrders extends ListActivity{
private static final String TAG = RegisterActivity.class.getSimpleName();
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
Typeface customFont;
TextView list_of_orders_txt;
ListView listView1;
SimpleAdapter adapter;
String[] table = new String[9999];
String desc[] = new String[9999];
String price[] = new String[9999];
String quantity[] = new String[9999];
String num, info;
int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listoforders);
customFont = Typeface.createFromAsset(getAssets(), "fonts/EraserRegular.ttf");
list_of_orders_txt = (TextView)findViewById(R.id.list_of_orders_tv);
list_of_orders_txt.setTypeface(customFont);
adapter = new SimpleAdapter( this, list, R.layout.listoforders_items,
new String[] {"title","subtitle"},
new int[] {R.id.loo_tablenumber,R.id.loo_itemquantity} );
getOrderDetails();
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
deleteOrder(table[position], desc[position], price[position], quantity[position]);
}
public void getOrderDetails() {
// Tag used to cancel the request
String tag_string_req = "req_register";
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
JSONObject user = jObj.getJSONObject("user");
JSONArray tablenumarray = jObj.getJSONArray("tablearray");
JSONArray descarray = jObj.getJSONArray("descarray");
JSONArray pricearray = jObj.getJSONArray("pricearray");
JSONArray quantityarray = jObj.getJSONArray("quantityarray");
num = user.getString("prows");
x = Integer.parseInt(num);
HashMap<String,String> map = new HashMap<String,String>();
for(int i=0; i<x; i++) {
table[i] = tablenumarray.getString(i);
//treatment[i] = treatmentarray.getString(i);
desc[i] = descarray.getString(i);
price[i] = pricearray.getString(i);
quantity[i] = quantityarray.getString(i);
}
for(int i=0; i<x; i++) {
info = "Description: " + desc[i].toString() + " \n" + "Price: " + price[i].toString() + " \n" + "Quantity: " + quantity[i].toString();
map = new HashMap<String,String>();
map.put("title", "Table Number: " + table[i].toString());
map.put("subtitle", info);
list.add(map);
}
setListAdapter(adapter);
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "orderinfo");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
//delete Order
private void deleteOrder(final String tablenum, final String desc, final String price, final String quantity) {
// Tag used to cancel the request
String tag_string_req = "req_registerpatient";
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Toast.makeText(getApplicationContext(), "Order Deleted", Toast.LENGTH_LONG).show();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "deleteorder");
params.put("tablenum", tablenum);
params.put("desc", desc);
params.put("price", price);
params.put("quantity", quantity);
//params.remove("tablenum");
//params.remove("desc");
//params.remove("price");
//params.remove("quantity");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}
RegisterActivity.java
package com.system.receivingoforder;
import com.system.receivingoforder.app.AppConfig;
import com.system.receivingoforder.app.AppController;
import com.system.receivingoforder.helper.SQLiteHandler;
import com.system.receivingoforder.helper.SessionManager;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
public class RegisterActivity extends Activity {
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnRegister;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
}
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Login Screen
}
/**
* Function to store user in MySQL database will post params(tag, name,
* email, password) to register url
* */
private void registerUser(final String name, final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
// Launch login activity
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
listoforders.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenboard_background" >
<TextView
android:id="@+id/list_of_orders_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="@string/list_of_orders"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<ListView
android:id="@+id/android:list"
android:layout_width="543dp"
android:layout_height="800dp"
android:layout_below="@+id/list_of_orders_tv"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
listoforders_items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/loo_tablenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/table_no"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/loo_itemquantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/loo_tablenumber"
android:text="@string/item_quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
</RelativeLayout>
答案 0 :(得分:0)
您使用了arraylist,因此您可以使用arrayList.remove(position)
,之后只需添加adapter.notifyDataSetChanged();