Android 检查ListView内EditText对象中的条目

Android 检查ListView内EditText对象中的条目,android,listview,android-edittext,Android,Listview,Android Edittext,我有一个列表框,它在列表框的每一行中都包含一个编辑框。在“保存”按钮上,我必须检查所有EditText对象是否都填充了数据 以下是ListView适配器的构造方式: public class LvAdapter extends BaseAdapter{ LayoutInflater mInflater; private Context context; public LvAdapter(Context c){ mInflater = (LayoutI

我有一个列表框,它在列表框的每一行中都包含一个编辑框。在“保存”按钮上,我必须检查所有EditText对象是否都填充了数据

以下是ListView适配器的构造方式:

public class LvAdapter extends BaseAdapter{

    LayoutInflater mInflater;
    private Context context;

    public LvAdapter(Context c){
         mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         context = c;
    }       

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //return list.size();
        return cGlobal.listOfWork.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final ViewHolder holder;
        convertView=null;
    if (convertView == null) {
        holder = new ViewHolder();


        convertView = mInflater.inflate(R.layout.activity_produced_quantities_rows, null); // this is your cell

        holder.orderID = (TextView) convertView.findViewById(R.id.pqOrderID);
        holder.orderID.setTag(position);
        holder.orderID.setText(cGlobal.listOfWork.get(position).get("OrderID"));

        holder.productCode = (TextView) convertView.findViewById(R.id.pqProductCode);
        holder.productCode.setTag(position);
        holder.productCode.setText(cGlobal.listOfWork.get(position).get("ProductCode"));


        holder.productionID = (TextView) convertView.findViewById(R.id.pqProductionID);
        holder.productionID.setTag(position);
        holder.productionID.setText(cGlobal.listOfWork.get(position).get("ProductionID"));

        holder.operationID = (TextView)convertView.findViewById(R.id.pqOperationID);
        holder.operationID.setTag(position);
        holder.operationID.setText(cGlobal.listOfWork.get(position).get("OperationID"));

        holder.quantity = (EditText) convertView.findViewById(R.id.txtQuantity);
        holder.quantity.setTag(position);

        convertView.setTag(holder);

        holder.quantity.setFocusable(true);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        final int tag_position=(Integer) holder.quantity.getTag();
        holder.quantity.setId(tag_position);


        if (upisano.get(holder.productionID.getText().toString()) != null)
        {
            holder.quantity.setText(upisano.get(holder.productionID.getText().toString()));
        }


         holder.quantity.addTextChangedListener(new TextWatcher() {

               @Override
               public void onTextChanged(CharSequence s, int start, int before,
                       int count) {                          

                     }                   


               @Override
               public void beforeTextChanged(CharSequence s, int start, int count,
                       int after) {
                   // TODO Auto-generated method stub
               }

               @Override
               public void afterTextChanged(Editable s) {                      

               }

           });  

         btnSave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText tvEdit = (EditText)findViewById(R.id.txtQuantity);

                if (tvEdit.getText().toString().isEmpty())
                {
                    Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT).show();
                } else
                {
                    Toast.makeText(getApplicationContext(), "Not Empty", Toast.LENGTH_SHORT).show();
                }

            }
        });

    return convertView;
    }       

}

public class ViewHolder {
    EditText quantity;
    TextView orderID;
    TextView productCode;
    TextView productionID;  
    TextView operationID;
}
应用程序联机崩溃:

if (tvEdit.getText().toString().isEmpty())
错误类型:

java.lang.NullPointerExpection

怎么了?

改变

   EditText tvEdit = (EditText)findViewById(R.id.txtQuantity);

并确保在布局
activity\u producted\u quantities\u rows

还是这样试试
在适配器中获取一个布尔数组列表:

private ArrayList<Boolean> quantityCheckList;
在适配器中定义一个自定义方法以检查是否存在任何空EditText:

public boolean isEmpty(){
   boolean isAnyEmpty=true;
   for (int i=0;i<quantityCheckList.size();i++){
        if(!quantityCheckList.get(i)){
           isAnyEmpty=false;
           break;
        }
   }
   return isAnyEmpty;
}

您可以在匿名内部类(OnClickListener)中使用holder.quantity,因为holder是最终变量。如果我是正确的,匿名内部类将在其构造函数中的最后一个变量放在引擎盖下,该变量将成为该对象的实例变量

所以它是这样的:

if (holder.quantity.getText().toString().isEmpty()) {
    Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT).show();
} else ...

在哪里初始化btnSave?您说每行的每个EditText中都必须有一个值。但是,即使您的代码碰巧是正确的,也不会检查当前不可见的行。我会在适配器中有一个字符串列表,在其中镜像我的EditText值。保存时,我会检查列表,而不是列表中当前加载的EditText我相信您的(R.id.txtQuantity)EditText不属于listView行。如果是的话,你可以这样定义它。。EditText tEdit=(EditText)convertView.findViewById(R.id.txtQuantity);它属于我,我已经检查过了。按钮在onCreate()中初始化。您的解决方案在if语句中中断。@Josef,那么您为什么在适配器中实现click listener而不是activity。因为这是我知道的唯一一个访问getView的方法:(@Josef,好的,请稍等,我编辑了我的ans,达到了您的要求。@Josef,没有兄弟,我认为btnSave在ListView中,但现在给出的解决方案是根据您的要求正确实现的。
quantityCheckList = new ArrayList<Boolean>();
for(int i=0;i<cGlobal.listOfWork.size();i++){
     quantityCheckList.add(false);
}
holder.quantity.addTextChangedListener(new TextWatcher() {
     @Override
     public void onTextChanged(CharSequence s, int start, int before,int count) {
     }
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count,int after) {
     }
     @Override
     public void afterTextChanged(Editable s) {
        if(s.toString().trim().length()>0){
           quantityCheckList.add(position,true);
        }else{
           quantityCheckList.add(position,false);
        }
     }
});
public boolean isEmpty(){
   boolean isAnyEmpty=true;
   for (int i=0;i<quantityCheckList.size();i++){
        if(!quantityCheckList.get(i)){
           isAnyEmpty=false;
           break;
        }
   }
   return isAnyEmpty;
}
btnSave.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      if (adapter.isEmpty())
      {
          Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT).show();
      } else
      {
          Toast.makeText(getApplicationContext(), "Not Empty", Toast.LENGTH_SHORT).show();
      }
    }
}); 
if (holder.quantity.getText().toString().isEmpty()) {
    Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT).show();
} else ...