我在自定义对话框中有一个 listview
。在该列表中,每一行都有一个文本字段和一个 Edittext
。我正在使用 editTextWatcher
来处理精确位置上的所有 edittext
值。到目前为止工作正常。我在 listView
下的那个对话框上有一个保存按钮。单击保存后,我想将所有 editext
数据发布到服务器。但是我只能发布那些在前面的 edittext
值。
对话框布局:
private void showDialog(){
dialog1 = new Dialog(this);
final Dialog tradDialog = new Dialog(this, android.R.style.Theme_Light_NoTitleBar);
//tradDialog.setContentView(R.layout.trad_dialog_layout);
View view = getLayoutInflater().inflate(R.layout.trad_dialog_layout_individual, null);
//tradDialog.setContentView(R.layout.trad_dialog_layout);
tradDialog.setCanceledOnTouchOutside(false);
// holder.mWatcher = new MutableWatcher();
lv = (ListView) view.findViewById(R.id.productsListView);
RelativeLayout saveBtnLayout = (RelativeLayout) view.findViewById(R.id.saveBtnLayout);
// Change MyActivity.this and myListOfItems to your own values
clad = new CustomListAdapterDialog(SolutionActivity.this, individual_productChoosedAr);
lv.setAdapter(clad);
clad.notifyDataSetChanged();
//save button for posting all edittext values to server
saveBtnLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < lv.getChildCount(); i++) {
v = lv.getChildAt(i);
etPrice = (EditText) v.findViewById(R.id.etPrice);
ProductPrice = etPrice.getText().toString();
if(ProductPrice.equals("")){
ProductPrice = "NULL";
}
productPriceAr.add(ProductPrice);
}
Toast toast = Toast.makeText(getApplicationContext(),"Please wait...",Toast.LENGTH_LONG);
toast.show();
SendIndividualDatatoServer sendIndividualData = new SendIndividualDatatoServer();
sendIndividualData.execute();
}
});
//lv.setOnItemClickListener(........);
dialog1.setContentView(view);
dialog1.show();
}
自定义适配器:
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position ;
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_layout_individual, null);
holder = new ViewHolder();
holder.product = (TextView) convertView.findViewById(R.id.tvProductName);
holder.price = (EditText) convertView.findViewById((R.id.etPrice));
holder.mWatcher = new MutableWatcher();
holder.price.addTextChangedListener(holder.mWatcher);
etPrice = (EditText) convertView.findViewById(R.id.etPrice);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mWatcher.setActive(false);
holder.price.setText(myList.get(position), TextView.BufferType.EDITABLE);
holder.mWatcher.setPosition(position);
holder.mWatcher.setActive(true);
holder.product.setText(listData.get(position).P_Name);
productIds = listData.get(position).Category_Id;
return convertView;
}
TextWatcher 类:
@Override
public void afterTextChanged(final Editable s) {
// SolutionActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
if (mActive) {
myList.put(mPosition, s.toString());
System.out.print(s.toString());
}
最佳答案
你传递给 CustomAdapter 的是什么? 您应该使用具有将包含 editText 文本的属性的自定义对象。 因此,您将向适配器传递类型为自定义对象的 ArrayList myList。 每当用户键入内容时,您都会获得 editText 在 ListView 中的位置,然后在该位置获得自定义对象并将 editText 值写入对象的属性。
像这样:
MyCustomClass object = myList.get(position);
object.setValue(edtText.getText());
像这样,您将拥有 arrayList 中的所有数据。
单击保存按钮时,您可以遍历 arrayList 并检查单个对象属性,如果它有数据,则将其添加到您的 productPriceList。
希望这对您有所帮助!
关于java - android无法在自定义对话框中的 ListView 中使用获取所有编辑文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36931226/