2017-01-03 55 views
2

當我嘗試點擊EditText裏面ListView項目。它獲得焦點和失去焦點,而調試,我發現,如果我點擊第21項,位置值改變像21,19,1,2等。不知道如果列表重新呈現自己或其他事情正在發生。我已經搜索了很多,並已嘗試配置ListViewbeforeDescendantsafterDescendants)和ActivityadjustPan)。專注於列表視圖內錯誤編輯文本

我已經使用了我的自定義適配器。共享XML文件ListView和列表項以及適配器的java代碼。

XML列表:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:padding="10dp"> 

    <!-- submit progress --> 
    <ProgressBar 
     android:id="@+id/list_progress" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:visibility="gone"/> 

    <LinearLayout 
     android:id="@+id/list_form" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="20dp"> 
     <TextView 
      android:id="@+id/nodata_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No data Found" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_gravity="center_horizontal" 
      android:textSize="20sp" 
      android:visibility="gone"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/info_ll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center_horizontal"> 
     <Button 
      android:id="@+id/send_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Create Order" 
      android:textSize="14sp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:layout_weight="1" 
      android:textColor="@android:color/white" 
      android:background="@drawable/bg_button"/> 


    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/ly_item_list_form" 
     android:layout_below="@+id/info_ll" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
     <ListView 
      android:id="@+id/item_list_view" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:descendantFocusability="afterDescendants"/> 
    </LinearLayout> 

</RelativeLayout> 

list_item_selector

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="2dp" 
    android:layout_marginRight="2dp" 
    android:foreground="?android:attr/selectableItemBackground" 
    card_view:cardCornerRadius="5dp" 
    card_view:cardElevation="2dp" 
    card_view:cardPreventCornerOverlap="true" 
    card_view:cardUseCompatPadding="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/item_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_margin="10dp" 
      android:text="ItemName" 
      android:textColor="@color/colorPrimaryDark" 
      android:textSize="15sp" 
      android:textStyle="bold"/> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginEnd="10dp" 
      android:layout_marginStart="10dp" 
      android:background="#FF0B38CC"></RelativeLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/uom_desc" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:layout_marginStart="10dp" 
        android:text="uom" 
        android:textColor="@color/colorPrimary" 
        android:textSize="13sp" 
        /> 

      </LinearLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.1" 
       > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:orientation="horizontal"> 

        <ImageButton 
         android:id="@+id/button_minus" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_weight="1" 
         android:background="@null" 
         android:src="@drawable/ic_remove_black_24dp"/> 

        <EditText 
         android:id="@+id/number_of_item" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0.7" 
         android:gravity="center" 
         android:text="0" 
         android:digits="" 
         android:maxLength="4" 
         android:inputType="number" 
         android:textColor="@color/colorPrimary"/> 

        <ImageButton 
         android:id="@+id/button_plus" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_weight="1" 
         android:background="@null" 
         android:src="@drawable/ic_add_black_24dp"/> 
       </LinearLayout> 
      </RelativeLayout> 
     </LinearLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

我的自定義適配器類

public class OrderViewListAdapter extends ArrayAdapter<OrderItemList> { 

public OrderViewListAdapter(Context context, int resource) { 
    super(context, resource); 
} 

private class ViewHolder { 
    TextView sku_desc; 
    TextView uom_desc; 
    EditText order_qty; 
    ImageButton plus; 
    ImageButton minus; 
    AutoCompleteTextView uom; 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    OrderItemList vehicleTransit = mDataSet.get(position); 
    //System.out.println("kamal123" +mDataSet.get(position).getOrderID()); 
    System.out.println("kamal123" +position); 
    ViewHolder viewHolder; 

    if (convertView == null) { 
     viewHolder = new ViewHolder(); 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(R.layout.list_item_selector, parent, false); 
     viewHolder.sku_desc = (TextView) convertView.findViewById(R.id 
       .item_name); 
     viewHolder.uom_desc = (TextView) convertView.findViewById(R.id 
       .uom_desc); 
     viewHolder.order_qty = (EditText) convertView.findViewById(R.id.number_of_item); 
     viewHolder.plus= (ImageButton)convertView.findViewById(R.id.button_plus) ; 
     viewHolder.minus= (ImageButton)convertView.findViewById(R.id.button_minus) ; 
     convertView.setTag(viewHolder); 
     viewHolder.plus.setTag(viewHolder); 
     viewHolder.minus.setTag(viewHolder); 
     viewHolder.order_qty.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    if(vehicleTransit!= null) { 
     System.out.println("kamal123" + vehicleTransit.getExpectedDeliveryDate() 
       .toString()); 


     if (vehicleTransit.getExpectedDeliveryDate() != null) { 
      viewHolder.uom_desc.setText(vehicleTransit.getUOMDesc() 
        .toString()); 
      System.out.println("kamal123" +vehicleTransit.getExpectedDeliveryDate() 
        .toString()); 

     }if (vehicleTransit.getSkuDesc() != null) { 
      viewHolder.sku_desc.setText(vehicleTransit.getSkuDesc().toString()); 
     } 
      viewHolder.order_qty.setText(vehicleTransit.getOrderQty()+""); 
      viewHolder.order_qty.setId(position); 
    } 



    viewHolder.plus.setOnClickListener(new View.OnClickListener() { 


     int count=0; 

     @Override 
     public void onClick(View v) { 
      View p = (View) v.getParent(); 
      ViewHolder holder1 = (ViewHolder) v.getTag(); 
      count = Integer.valueOf(holder1.order_qty.getText().toString()); 
      count++; 
      holder1.order_qty.setText(String.valueOf(count)); 
      mDataSet.get(position).setOrderQty(count); 
      //mAdapter.notifyDataSetChanged(); 
     } 

    }); 



    viewHolder.minus.setOnClickListener(new View.OnClickListener() { 


     int count=0; 

     @Override 
     public void onClick(View v) { 
      View p = (View) v.getParent(); 
      ViewHolder holder1 = (ViewHolder) v.getTag(); 
      count = Integer.valueOf(holder1.order_qty.getText().toString()); 
      count--; 
      if(count>=0) 
      {holder1.order_qty.setText(String.valueOf(count)); 
      mDataSet.get(position).setOrderQty(count); 
      //mAdapter.notifyDataSetChanged(); 
      } 

     } 

    }); 





    //we need to update adapter once we finish with editing 
    viewHolder.order_qty.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

     int count=0; 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus){ 
       View p = (View) v.getParent(); 
       ViewHolder holder1 = (ViewHolder) v.getTag(); 
       final int position = holder1.order_qty.getId(); 
       count = Integer.valueOf(holder1.order_qty.getText().toString()); 
       if(count>0) 
       {holder1.order_qty.setText(String.valueOf(count)); 
        mDataSet.get(position).setOrderQty(count); 
        //mAdapter.notifyDataSetChanged(); 
       } 





      } 
     } 
    }); 


    /*viewHolder.order_qty.addTextChangedListener(new TextWatcher() { 

     int count=0; 

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

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

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      count = Integer.valueOf(s.toString()); 
      mDataSet.get(position).setOrderQty(count); 

     } 
    });*/ 


    return convertView; 
} 

@Override 
public int getCount() { 
    return mDataSet.size(); 
} 
} 

預先感謝的XML。

回答

0

最後,我想它了。該問題試圖解決在getview中get tex更改的值。這是重新呈現列表項目,我得到了錯誤的焦點和不同奇怪的行爲。我創建了一個textwatcher類,並在初始化對象時添加了它,以便它不會影響列表中每個項目的每次。以下是MyCustom適配器的代碼

public class OrderViewListAdapter extends ArrayAdapter<OrderItemList> { 

    public OrderViewListAdapter(Context context, int resource) { 
     super(context, resource); 
    } 

    private class ViewHolder { 
     TextView sku_desc; 
     TextView uom_desc; 
     EditText order_qty; 
     ImageButton plus; 
     ImageButton minus; 
     AutoCompleteTextView uom; 
     MyCustomEditTextListener myCustomEditTextListener; 

     public ViewHolder(View convertView, MyCustomEditTextListener myCustomEditTextListener) { 
      this.myCustomEditTextListener = myCustomEditTextListener; 
      this.order_qty = (EditText) convertView.findViewById(R.id.number_of_item); 
      this.order_qty.addTextChangedListener(myCustomEditTextListener); 

     } 
    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     OrderItemList vehicleTransit = mDataSet.get(position); 
     //System.out.println("kamal123" +mDataSet.get(position).getOrderID()); 
     System.out.println("kamal123" +position); 
     ViewHolder viewHolder; 

     if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.list_item_selector, parent, false); 
      viewHolder = new ViewHolder(convertView, new MyCustomEditTextListener()); 
      viewHolder.sku_desc = (TextView) convertView.findViewById(R.id 
        .item_name); 
      viewHolder.uom_desc = (TextView) convertView.findViewById(R.id 
        .uom_desc); 
      viewHolder.order_qty = (EditText) convertView.findViewById(R.id.number_of_item); 
      viewHolder.plus= (ImageButton)convertView.findViewById(R.id.button_plus) ; 
      viewHolder.minus= (ImageButton)convertView.findViewById(R.id.button_minus) ; 
      convertView.setTag(viewHolder); 
      viewHolder.plus.setTag(viewHolder); 
      viewHolder.minus.setTag(viewHolder); 
      viewHolder.order_qty.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     if(vehicleTransit!= null) { 
      System.out.println("kamal123" + vehicleTransit.getExpectedDeliveryDate() 
        .toString()); 


      if (vehicleTransit.getExpectedDeliveryDate() != null) { 
       viewHolder.uom_desc.setText(vehicleTransit.getUOMDesc() 
         .toString()); 
       System.out.println("kamal123" +vehicleTransit.getExpectedDeliveryDate() 
         .toString()); 

      }if (vehicleTransit.getSkuDesc() != null) { 
       viewHolder.sku_desc.setText(vehicleTransit.getSkuDesc().toString()); 
      } 

      viewHolder.myCustomEditTextListener.updatePosition(position); 
      viewHolder.order_qty.setText(vehicleTransit.getOrderQty()+""); 
       viewHolder.order_qty.setId(position); 

     } 



     viewHolder.plus.setOnClickListener(new View.OnClickListener() { 


      int count=0; 

      @Override 
      public void onClick(View v) { 
       View p = (View) v.getParent(); 
       ViewHolder holder1 = (ViewHolder) v.getTag(); 
       count = Integer.valueOf(holder1.order_qty.getText().toString()); 
       count++; 
       holder1.order_qty.setText(String.valueOf(count)); 
       mDataSet.get(position).setOrderQty(count); 
       //mAdapter.notifyDataSetChanged(); 
      } 

     }); 



     viewHolder.minus.setOnClickListener(new View.OnClickListener() { 


      int count=0; 

      @Override 
      public void onClick(View v) { 
       View p = (View) v.getParent(); 
       ViewHolder holder1 = (ViewHolder) v.getTag(); 
       count = Integer.valueOf(holder1.order_qty.getText().toString()); 
       count--; 
       if(count>=0) 
       {holder1.order_qty.setText(String.valueOf(count)); 
       mDataSet.get(position).setOrderQty(count); 
       //mAdapter.notifyDataSetChanged(); 
       } 

      } 

     }); 



     return convertView; 
    } 



    private class MyCustomEditTextListener implements TextWatcher { 
     private int position; 

     public void updatePosition(int position) { 
      this.position = position; 
     } 

     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      // no op 
     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      if (!charSequence.toString().equals("")) { 
       mDataSet.get(position).setOrderQty(Integer.valueOf(charSequence.toString())); 
      } else { 
       mDataSet.get(position).setOrderQty(0); 
      } 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      // no op 
     } 
    } 



    @Override 
    public int getCount() { 
     return mDataSet.size(); 
    } 
} 
0

插入此行之前onCreatesetContentView()

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST); 

它一定會幫助你

+0

我將您的建議代碼應用於我的應用程序,性能有所提高,但有時仍然不允許編輯框內的文本在輸入值時發生更改。 –

+0

早些時候,我的代碼允許我在30-40個列表項中的一個或兩個編輯文本中輸入值,現在它允許我在大多數編輯文本中輸入值,但是當我上下滾動並嘗試在某些值中輸入值時隨機編輯文本,一次或兩次的值不反映。 –