【发布时间】: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§ion=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()”
视图如下所示:-
我试图在单击保存按钮后保存列表视图的所有值,但我没有使用该代码获取列表视图项,您能否解释实现该目标的最佳方法?谢谢。
【问题讨论】:
-
只保留一个对 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