2016-03-23 88 views
0

在我的列表視圖中,我有Edittext和一些textviews。當我試圖迭代listview以得到edittext值時,我遇到了一些問題。我只想在listview中的edittext不爲空時執行操作。否則我想繼續迭代。當迭代listview時,我只需要edittexts中的非空值。我的listview這個名字是「list」。 我的代碼是編輯列表視圖中的文本是否爲空

if (list != null) { 
      for (int i = 0; i < list.getChildCount(); i++) { 
       ed2 = (EditText) list.getChildAt(i).findViewById(R.id.pcs); 
       if (ed2.getText().toString().trim().length() != 0) { 
        TextView im_codetext = (TextView) vie.findViewById(R.id.code); 
        imcode = im_codetext.getText().toString(); 
        try { 
         DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext()); 
         newDB = dbHelper.getWritableDatabase(); 
         Cursor cr = newDB.rawQuery("select distinct im_srp_pc, im_subgroup_code from itemmaster where im_code =" + imcode, null); 

         while (cr.moveToNext()) { 
          srppc = cr.getString(cr.getColumnIndex("im_srp_pc")); 
          String subcode = cr.getString(cr.getColumnIndex("im_subgroup_code")); 
          databas.put("subgp", subcode); 
         } 
         srps = Float.valueOf(srpval); 
         qtys = Integer.valueOf(ed2.getText().toString()); 

        } catch (NumberFormatException e) { 
         srps = 0; 
        } 
        float amnts = srps * qtys; 
        sumnet = sumnet + amnts; 
        else{ 
         Log.v(" empty", " empty"); 
        } 

       } 
      } 
     } 

我的適配器類是提前

public class MyListAdapter extends BaseAdapter{ 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      if(datas != null && datas.size() != 0){ 
       return datas.size();  
      } 
      return 0; 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return datas.get(position); 
     } 

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

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

      //ViewHolder holder = null; 
      final ViewHolder holder; 
      if (convertView == null) { 

       holder = new ViewHolder(); 
       LayoutInflater inflater = Orders.this.getLayoutInflater(); 
       convertView = inflater.inflate(R.layout.order_simple_row, null); 
       holder.textView1 = (TextView) convertView.findViewById(R.id.name); 
       holder.textView2 = (TextView) convertView.findViewById(R.id.srp); 
       holder.textview3 = (TextView) convertView.findViewById(R.id.code); 
            holder.editText2 = (EditText) convertView.findViewById(R.id.pcs); 

       convertView.setTag(holder); 

      } else { 

       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.ref = position; 
      String [] names = new String[550]; 
      String [] codes = new String[550]; 
      String [] prize = new String[550]; 

       DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext()); 
      newDB = dbHelper.getWritableDatabase(); 
      Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp " 
        + " from itemmaster", null); 

       c.moveToFirst(); 

       while (c.moveToNext()){ 

        Log.v("item detailss", c.toString()); 
       String cod = c.getString(c.getColumnIndex("im_code")); 
      codes[c.getPosition()] =cod; 
       String desc1 = c.getString(c.getColumnIndex("im_desc")); 
       names[c.getPosition()] = desc1; 

       String price = c.getString(c.getColumnIndex("im_srp")); 
       prize[c.getPosition()] = price; 
           } 

       holder.textView1.setText(names[position+1]); 
       holder.textview3.setText(codes[position+1]); 
        holder.textView2.setText(prize[position+1]); 



      holder.editText2.setText(arrTemp[position]); 

      holder.editText2.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
         int arg3) { 
        // TODO Auto-generated method stub 
        // arrTemp[holder.ref] = arg0.toString(); 
       } 

       @Override 
       public void afterTextChanged(Editable arg0) { 
        // TODO Auto-generated method stub 
        arrTemp[holder.ref] = arg0.toString(); 

       } 
      }); 

      return convertView; 


    } 
     private class ViewHolder { 

      public Object list; 
      TextView textView1,textView2,textview3; 
     EditText editText1,editText2; 
      int ref; 
      } 
    } 

請help.thanks。

+0

請發表您的適配器類... – Ancee

+0

@Ancee我只是張貼了我的適配器類。請看看 – ellie

+0

你想要什麼?檢查所有的edittext是否爲空? –

回答

0

我有一個解決方案。

在您的自定義適配器類中聲明一個HashMap。

public class CustomAdapter extends BaseAdapter{ 

    public HashMap<Integer,String> textMap = new HashMap<Integer,String>(); 
    .... 

getView()方法從HasMap中獲取文本並在EditText中設置。

@Override 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    final Holder holder=new Holder(); 
    View rowView; 
    rowView = inflater.inflate(R.layout.list_item, null); 

    holder.editText = (EditText)rowView.findViewById(R.id.editText); 

    holder.editText.setText(textMap.get(position)); 
    .... 

並在TextChangeListner中按位置在HashMap中添加文本。

holder.editText.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
              int arg3) { 
       // TODO Auto-generated method stub 
       // arrTemp[holder.ref] = arg0.toString(); 
      } 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // TODO Auto-generated method stub 

       textMap.put(position,holder.editText.getText().toString()); 

      } 
     }); 

現在,在您的活動中,您可以獲取適配器的HashMap並按位置獲取其值。如果在特定位置沒有文本,則表示edttext爲空。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button)findViewById(R.id.button); 
     listView = (ListView)findViewById(R.id.listView); 
     final CustomAdapter customAdapter = new CustomAdapter(this,names); 
     listView.setAdapter(customAdapter); 


     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 



       for(int i=0;i<names.length;i++){ 


        Toast.makeText(MainActivity.this, customAdapter.textMap.get(i), Toast.LENGTH_SHORT).show(); 

       } 

      } 
     }); 
相關問題