【问题标题】:Get all values of listview after header button click单击标题按钮后获取列表视图的所有值
【发布时间】:2017-02-14 00:15:05
【问题描述】:

在我的活动中,我添加了一个标题按钮来保存列表视图的值,使用 EditText 并将它们发布到 php/mysql 网络应用程序。

如果我使用 setOnItemClickListener,我可以获取列表视图的值,但是当我在标题保存按钮上使用 setOnClickListener 时,我无法遍历列表视图。

我正在使用自定义数组适配器:-

公共类 CustomOrderAdaptor 扩展 ArrayAdapter{ int groupid;

ArrayList<OneOrder> records;

Context context;



public CustomOrderAdaptor(Context context, int vg, int id, ArrayList<OneOrder>records) {

    super(context, vg, id, records);

    this.context = context;

    groupid = vg;

    this.records = records;



}



public View getView(int position, View convertView, ViewGroup parent) {



    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(groupid, parent, false);

    TextView textName = (TextView) itemView.findViewById(R.id.product_name);
    textName.setText(records.get(position).getproduct_name());

    EditText new_quantity = (EditText) itemView.findViewById(R.id.new_quantity);
    new_quantity.setText(records.get(position).getnew_quantity());

    TextView textOrderitemid = (TextView) itemView.findViewById(R.id.order_item_id);
    textOrderitemid.setText(records.get(position).getorder_item_id());

    TextView textQuantity = (TextView) itemView.findViewById(R.id.quantity);
    textQuantity.setText(records.get(position).getquantity());



    return itemView;

}

}

数据模型:-

public class OneOrder {
private String quantity;
private String new_quantity;
private String product_name;
private String order_item_id;



public void setquantity(String quantity){this.quantity=quantity;}
public void setnew_quantity(String new_quantity){this.new_quantity=new_quantity;}
public void setproduct_name(String product_name){this.product_name=product_name;}
public void setorder_item_id(String order_item_id){this.order_item_id=order_item_id;}



public String getquantity(){return quantity;}
public String getnew_quantity(){return new_quantity;}
public String getproduct_name(){return product_name;}
public String getorder_item_id(){return order_item_id;}

}

我的活动是:-

protected void onCreate(Bundle savedInstanceState) {

    //TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_get_order);

    context = this;

    records = new ArrayList<OneOrder>();

    listOrder = (ListView) findViewById(R.id.order_item_list);

    LayoutInflater inflater = LayoutInflater.from(this);
    View nTop = inflater.inflate(R.layout.activity_get_order_footer, null);
    listOrder.addHeaderView(nTop);


    adapter = new CustomOrderAdaptor(context, R.layout.list_order, R.id.product_name,
            records);


    listOrder.setAdapter(adapter);

    Button mButton = (Button) nTop.findViewById(R.id.button_save);

    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            JSONObject order_items = new JSONObject();
            JSONObject sendObject = new JSONObject();
            for (int i=0;i<adapter.getCount();i++){
                JSONObject order_item = new JSONObject();
                OneOrder current_order = (OneOrder) listOrder.getAdapter().getItem(i);
                //OneOrder current_order = (OneOrder) getListView().getItemAtPosition(i);
                try {
                    order_item.put("quantity", current_order.getquantity().toString());
                    order_item.put("new_quantity", current_order.getnew_quantity().toString());
                    order_item.put("order_item_id", current_order.getorder_item_id().toString());
                    order_items.put(String.valueOf(i),order_item);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            HttpURLConnection conn = null;
            try {
                Intent i = getIntent(); // gets the previously created intent
                String order_id = i.getStringExtra("order_id");
                sendObject.put("items", order_items.toString());
                sendObject.put("order_id", order_id);
                try {
                    URL url = new URL("http://192.168.0.70/steam_dos/index.php?option=com_steam&section=linen&task=save_order_out");
                    String message = sendObject.toString();

                    conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout( 10000 /*milliseconds*/ );
                    conn.setConnectTimeout( 15000 /* milliseconds */ );
                    conn.setRequestMethod("POST");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setFixedLengthStreamingMode(message.getBytes().length);

                    //make some HTTP header nicety
                    conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                    conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                    //open
                    conn.connect();

                    //setup send
                    BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
                    os.write(message.getBytes());
                    //clean up
                    os.flush();

                    //do something with response
                    InputStream is = conn.getInputStream();
                }catch (MalformedURLException e) {
                    e.printStackTrace();

                } catch (IOException e) {

                    e.printStackTrace();

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    });






}

错误在这里:-

 order_item.put("quantity", current_order.getquantity().toString());

错误很简单:-

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String au.com.southportsteamlaundry.rfid.steamscanadditions.OneOrder.getquantity()”

视图如下所示:-

Layout view

我试图在单击保存按钮后保存列表视图的所有值,但我没有使用该代码获取列表视图项,您能否解释实现该目标的最佳方法?谢谢。

【问题讨论】:

  • 只保留一个对 ArrayList 的本地引用,该引用传递给拥有活动内部的自定义适配器构造函数。在 OnItemClickListener 内部,将选定的索引存储在一个布尔值 [] 中,该值被初始化为与 ArrayList 相同的大小。一旦选择了标题按钮,只需遍历布尔数组,具有真值的索引会抓取 ArrayList 中的相应索引并将它们存储在临时列表中并将其上传到您的数据库。
  • 感谢 Wade - 如果我理解正确,当我从 php/mysql Web 应用程序获取 json 对象时,我应该将数据保存在本地数组中,并在单击保存按钮后提取值?如何获取位于列表视图中的 EditText 的值,每行一个 EditText。
  • 显然是 java nube 过度思考的案例。我从 php 获取了一个 json 对象,我只需要在活动的顶部声明该 JsonArray ,然后就可以访问它并将其发回以进行处理。我将 HttpURLConnection 移动到异步任务中,因为它崩溃了。我仍然需要弄清楚如何访问每个列表视图行旁边的 EditTexts。
  • 至于获取每个 ListView 行的 EditText 值会很棘手,并且需要一些解决方法,因为这些行将被垃圾收集或回收,具体取决于您实现它的方式。您要做的是在数据模型中创建一个表示列表行的附加字段,它将文本保存在 EditText 内,并在文本更改时更新。

标签: java android listview android-arrayadapter


【解决方案1】:

通常不建议使用带适配器的编辑文本,原因是滚动时无法处理编辑文本保存。

有两种解决方案。

  1. 用滚动视图替换列表视图

2 创建一个变量并在edittext 上添加一个TextWatcher。每当编辑文本被修改时,文本观察器就会检测到更改并在您对其进行操作时覆盖该变量。

代码就像 Implementing Text Watcher for EditText

【讨论】:

  • 感谢TaoBIt,我会看看滚动视图,虽然我怀疑在这种情况下会不会滚动。每个订单不应该有太多的订单项目,并且私人应用程序将在平板电脑上运行。我仍然不知道您的回答我应该如何获取列表视图/滚动视图的所有值,或者如何获取该行的匹配 EditText。
  • 我不建议这样做,因为您必须一次在屏幕上加载订单项目的所有视图。如果您有大量订单项目,您不会希望加载所有视图,因为这会占用大量内存并且在初始加载期间会产生性能开销。 ListView 与 ScrollView 不同,它只绘制可以一次显示在屏幕上的视图,并且内部具有相当复杂的逻辑来处理回收视图,以便根据滚动方向在列表的顶部和底部再次使用,这大大节省了记忆。
  • 是的,@Wade Wilson。当您知道数据不多时,使用滚动视图会更好。总体而言,EditText 与 listview/recyclerview 不太兼容,因为当行滚动出屏幕时,数据会变得干净。如果您仍然必须使用它,我建议您在 EditText 中使用 Text Watcher。而且您还需要小心处理列表视图/滚动视图中 EditText 上的键盘焦点
  • @TaoBit 我同意,我相信最好的解决方案是从 ListView 中删除 EditText 字段并将其替换为 TextView 和一个编辑按钮,该按钮将启动一个带有 EditText 视图的对话框并执行从那里执行所需的操作。
【解决方案2】:

我的答案可能不是最好的,但这是我为获取列表视图的 EditText 值而实现的:

mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            JSONObject order_item = new JSONObject();
            order_items_edited = new JSONObject();
            for (int i = 0; i < listOrder.getCount(); i++) {
                EditText et = (EditText) listOrder.getChildAt(i).findViewById(R.id.new_quantity);
                if (et!=null) {

                    TextView oi = (TextView) listOrder.getChildAt(i).findViewById(R.id.order_item_id);
                    Log.i("dtag", "et is " + String.valueOf(et.getText()));
                    Log.i("dtag", "oi is " + String.valueOf(oi.getText()));
                    try {
                        order_item.put("new_quantity", String.valueOf(et.getText()));
                        order_item.put("order_item_id", String.valueOf(oi.getText()));
                        order_items_edited.put(String.valueOf(i),order_item);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

我遇到的主要问题是 et 的 null 对象,这是有效的,但我没有测试它。

现在它正在工作,我将考虑实施一个更清洁的解决方案。

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多