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

时间:2014-11-26 12:44:40

标签: android listview android-edittext

我的列表框包含列表框每行的EditBox。在按钮保存我必须检查是所有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

怎么了?

3 个答案:

答案 0 :(得分:1)

更改

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

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

并确保您的布局EditText

中包含ID为 txtQuantity activity_produced_quantities_rows {/ 1}

或尝试这种方式

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

答案 1 :(得分:1)

在适配器中使用一个布尔型ArrayList:

private ArrayList<Boolean> quantityCheckList;

使用适配器构造函数中的列表项初始化:

quantityCheckList = new ArrayList<Boolean>();
for(int i=0;i<cGlobal.listOfWork.size();i++){
     quantityCheckList.add(false);
}

当EditText值更改时更改相应的项布尔状态:

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

在适配器中定义一个自定义方法以检查任何空的EditText:

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

现在在activity中实现btnSave click listener并使用适配器isEmpty()检查空值:

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

答案 2 :(得分:0)

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

所以它会是这样的:

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