所以,这是我的代码,我想要做的是通过单击按钮保存从listview自定义适配器获取所有数据。我这里有的是我可以从列表视图中删除单个数据,或者我可以通过单击删除按钮将其全部删除。我的问题是,通过单击保存按钮并存储在字符串上,我无法从列表视图中获取所有数据。 Here is the image.
ReviewYourOrder.java
package com.example.orderingsystem;
import java.util.ArrayList;
.....
public class ReviewYourOrder extends Activity implements View.OnClickListener {
private static final String TAG = RegisterActivity.class.getSimpleName();
DatabaseAdapter ourDatabase;
Typeface customFont;
TextView review_you_order_txt,review_your_order_quantity,review_your_order_description,review_your_order_price;
Button save,cancel;
ListView myList;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reviewyourorder);
customFont = Typeface.createFromAsset(getAssets(), "fonts/EraserRegular.ttf");
review_you_order_txt = (TextView)findViewById(R.id.review_your_order_tv);
review_you_order_txt.setTypeface(customFont);
review_your_order_quantity = (TextView)findViewById(R.id.review_your_order_quantity_tv);
review_your_order_quantity.setTypeface(customFont);
review_your_order_description = (TextView)findViewById(R.id.review_your_order_description_tv);
review_your_order_description.setTypeface(customFont);
review_your_order_price = (TextView)findViewById(R.id.review_your_order_price_tv);
review_your_order_price.setTypeface(customFont);
save = (Button)findViewById(R.id.save_button);
save.setOnClickListener(this);
save.setTypeface(customFont);
cancel = (Button)findViewById(R.id.cancel_button);
cancel.setOnClickListener(this);
cancel.setTypeface(customFont);
openDB();
ourDatabase.open();
final Cursor c = ourDatabase.getAllDataReviewYourOrder();
String[] ReviewYourOrderNames = new String[] {DatabaseAdapter.KEY_REVIEWYOURORDER_TABLENUMBER,DatabaseAdapter.KEY_REVIEWYOURORDER_DESC,DatabaseAdapter.KEY_REVIEWYOURORDER_PRICE,DatabaseAdapter.KEY_REVIEWYOURORDER_QUANTITY};
int[] ReviewYourOrderID = new int[] {R.id.tablenumber,R.id.itemdesc,R.id.itemprice,R.id.itemquantity};
final SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.reviewyourorder_items, c, ReviewYourOrderNames, ReviewYourOrderID,0);
myList = (ListView)findViewById(R.id.review_your_order_listview);
myList.setAdapter(myCursorAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id_o) {
// TODO Auto-generated method stub
AlertDialog.Builder adb = new AlertDialog.Builder(ReviewYourOrder.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to remove this Order?");
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
ourDatabase.open();
ourDatabase.deleteReviewYourOrder(id_o);
myCursorAdapter.notifyDataSetChanged();
Intent intent = getIntent();
finish();
startActivity(intent);
}});
adb.show();
}
});
}
private void openDB() {
ourDatabase = new DatabaseAdapter(this);
ourDatabase.open();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.save_button:
String sample;
sample = "asd";
addOrder(sample.toString(),sample.toString(),sample.toString());
break;
case R.id.cancel_button:
AlertDialog.Builder dialog = new AlertDialog.Builder(ReviewYourOrder.this);
dialog.setTitle("CANCEL ORDER?");
dialog.setMessage("Are you sure you want to Cancel your Order?");
dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("OK", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
ourDatabase.open();
ourDatabase.deleteAllDataReviewYourOrder();
Toast.makeText(getBaseContext(), "Order Canceled",Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
}
private void addOrder(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 Added.", 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(getBaseContext(),
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", "add_order");
params.put("desc", desc);
params.put("price", price);
params.put("quantity", quantity);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}
我的xml文件中有两个按钮保存并取消按钮。
reviewyourorder.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/review_your_order_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/review_your_order"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<TextView
android:id="@+id/review_your_order_quantity_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/review_your_order_listview"
android:layout_below="@+id/review_your_order_tv"
android:text="@string/quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView
android:id="@+id/review_your_order_description_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/review_your_order_quantity_tv"
android:layout_alignBottom="@+id/review_your_order_quantity_tv"
android:layout_centerHorizontal="true"
android:text="@string/ryo_description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView
android:id="@+id/review_your_order_price_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/review_your_order_listview"
android:layout_alignRight="@+id/review_your_order_listview"
android:layout_marginRight="30dp"
android:text="@string/item_price"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/review_your_order_listview"
android:layout_width="543dp"
android:layout_height="695dp"
android:layout_below="@+id/review_your_order_quantity_tv"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true">
</ListView>
<Button
android:id="@+id/save_button"
android:layout_width="260dp"
android:layout_height="70dp"
android:layout_alignRight="@+id/review_your_order_listview"
android:layout_below="@+id/review_your_order_listview"
android:layout_marginTop="10dp"
android:text="@string/save"
android:textSize="40sp" />
<Button
android:id="@+id/cancel_button"
android:layout_width="260dp"
android:layout_height="70dp"
android:layout_alignLeft="@+id/review_your_order_listview"
android:layout_alignTop="@+id/save_button"
android:text="@string/cancel"
android:textSize="40sp" />
以下是我的自定义列表视图中的文章视图。
reviewyourorder_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/tablenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="@string/table_no"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/itemquantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tablenumber"
android:layout_marginLeft="41dp"
android:text="@string/item_quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemprice"
android:layout_alignBottom="@+id/itemprice"
android:layout_centerHorizontal="true"
android:text="@string/item_desc"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/itemprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemquantity"
android:layout_alignBottom="@+id/itemquantity"
android:layout_alignParentRight="true"
android:layout_marginRight="56dp"
android:text="@string/item_price"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
答案 0 :(得分:4)
我希望这会对你有所帮助,
int itemsCount = myList.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = myList.getChildAt(i);
String itemquantity = ((TextView) view.findViewById(R.id.itemquantity)).getText().toString();
String itemdesc = ((TextView) view.findViewById(R.id.itemdesc)).getText().toString();
String itemprice = ((TextView) view.findViewById(R.id.itemprice)).getText().toString();
addOrder(itemdesc,itemprice,itemquantity);
}