简体   繁体   English

如何从按钮上的列表视图自定义适配器获取所有textview数据

[英]How to get All textview data from List View Custom Adapter on Button Click

So, here is my code what I want to do is get all the data from listview custom adapter by clicking the button save. 因此,这是我的代码,我想要做的是通过单击保存按钮从listview自定义适配器获取所有数据。 What I have here is I can delete a single data from the listview or I can delete it all by clicking the delete button. 我所拥有的是我可以从列表视图中删除单个数据,也可以通过单击删除按钮将其全部删除。 My problem is, I cannot get all data from the listview by clicking the save button and store on a string. 我的问题是,我无法通过单击保存按钮并存储在字符串上来从列表视图中获取所有数据。 Here is the image. 这是图片。

ReviewYourOrder.java 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);

}

} }

I Have two button in my xml file save and cancel button. 我在xml文件中有两个按钮,即保存和取消按钮。

reviewyourorder.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" />

And here are my textviews which serve as my items inside my custom listview. 这是我的文本视图,用作自定义列表视图中的项目。

reviewyourorder_items.xml. 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" />

I hope this will helpful for you, 希望对您有帮助,

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从视图中获取 TextView 和按钮列表 - How to get the TextView and Button list from a view 在按钮上单击,使用自定义适配器将自定义行添加到列表视图 - on button click add custom row to list view using custom adapter 如何使用自定义适配器在Android列表视图中添加ImageView / TextView? - How to add ImageView/TextView in a Android List view with custom adapter? 如何从自定义列表视图适配器获取数据并传递给意图放额外的? - How to get data from custom list view adapter and pass to intent put extra? 如何使用自定义适配器从列表视图上单击的项目中获取文本 - How get text from item clicked on a List view with a Custom Adapter 列表视图中的页脚按钮,如何从自定义列表适配器获取价值 - Footer button in listview, how to get value from custom list adapter 如何在Android的自定义列表视图中从TextView获取值 - How to get the value from TextView in a custom list view in android 如何从同一视图内的ListView适配器onClick按钮获取textView值? - How to get a textView value from ListView adapter onClick button inside same view? 如何在单击按钮时选择自定义列表视图的所有复选框? - how to select all checkbox of custom list view on button click? 如何在不使用自定义适配器的情况下从ListView获取特定的TextView(View)? - How to get specific TextView (View ) from a ListView without use of custom Adapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM