2016-06-27 51 views
1

我有列表元素中有3個編輯文本的列表視圖。編輯文本輸入字符串的狀態不會固定在元素的位置上。listView中的EditText不能保存輸入,android?

我已經創建了一個數組來存儲該值,但它將複製所有編輯文本中的值。

這是我的適配器類

public class OrderAdapter extends BaseAdapter { 
private ArrayList<POJOOrder> assetArrayList; 
private String[] agreedVolume, agreedSalesParty, agreedSalesMgr; 

public OrderAdapter(ArrayList<POJOOrder> assetArrayList) { 
    this.assetArrayList = assetArrayList; 
    agreedVolume = new String[assetArrayList.size()]; 
    agreedSalesParty = new String[assetArrayList.size()]; 
    agreedSalesMgr = new String[assetArrayList.size()]; 
} 

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

@Override 
public Object getItem(int position) { 
    return assetArrayList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View vi = convertView; 
    final ViewHolder holder; 
    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) 
       parent.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     vi = mInflater.inflate(R.layout.element_order, null); 
     holder = new ViewHolder(); 
     holder.tvItem = (TextView) vi.findViewById(R.id.textViewItem); 
     holder.tvPlanned = (TextView) vi.findViewById(R.id.textViewPlanned); 
     holder.tvPlannedParty = (TextView) vi.findViewById(R.id.textViewPalnnedParty); 
     holder.tvPlannedMgr = (TextView) vi.findViewById(R.id.textViewPlannedMgr); 
     holder.etAgreed = (EditText) vi.findViewById(R.id.editTextAgreed); 
     holder.etAgreedParty = (EditText) vi.findViewById(R.id.editTextAgreedParty); 
     holder.etAgreeMgr = (EditText) vi.findViewById(R.id.editTextAgreedMgr); 
     //   holder.etAgreed.setTag(position); 
     //   holder.etAgreedParty.setTag(position); 
    //   holder.etAgreeMgr.setTag(position); 
     vi.setTag(holder); 
    } else 
     holder = (ViewHolder) vi.getTag(); 

    holder.number = position; 
    holder.tvItem.setText(assetArrayList.get(position).getBrandWith_SKU()); 
    holder.tvPlanned.setText(assetArrayList.get(position).getPlannedVolume() + " CS"); 
    holder.tvPlannedParty.setText(assetArrayList.get(position).getPlannedSalesPromoParty() + ""); 
    holder.tvPlannedMgr.setText(assetArrayList.get(position).getPlannedSalesPromoMgr() + ""); 

    if (agreedVolume[holder.number] != null) { 
     holder.etAgreed.setText("" + agreedVolume[position]); 
    } 

    if (agreedSalesParty[position] != null) { 
     holder.etAgreedParty.setText("" + agreedSalesParty[position]); 
    } 

    if (agreedSalesMgr[position] != null) { 
     holder.etAgreeMgr.setText("" + agreedSalesMgr[position]); 
    } 


    holder.etAgreed.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      Log.e("position", holder.number + ""); 

      agreedVolume[holder.number] = s.toString(); 
     } 
    }); 
    holder.etAgreedParty.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      agreedSalesParty[holder.number] = s.toString(); 
     } 
    }); 
    holder.etAgreeMgr.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      agreedSalesMgr[holder.number] = s.toString(); 
     } 
    }); 

    return vi; 
} 

private static class ViewHolder { 
    private TextView tvItem, tvPlanned, tvPlannedParty, tvPlannedMgr; 
    private EditText etAgreed, etAgreedParty, etAgreeMgr; 
    private int number; 
} 

} 

POJOOrder僅僅是一個POJO類的getter和setter

public class POJOOrder { 
private int BrandId; 
private String BrandWith_SKU; 
private int SKU_Id; 
private int PlannedVolume; 
private int PlannedSalesPromoParty; 
private int PlannedSalesPromoMgr; 

public POJOOrder(int brandId, String brandWith_SKU, int SKU_Id, int plannedVolume, int plannedSalesPromoParty, int plannedSalesPromoMgr) { 
    BrandId = brandId; 
    BrandWith_SKU = brandWith_SKU; 
    this.SKU_Id = SKU_Id; 
    PlannedVolume = plannedVolume; 
    PlannedSalesPromoParty = plannedSalesPromoParty; 
    PlannedSalesPromoMgr = plannedSalesPromoMgr; 
} 

public int getBrandId() { 
    return BrandId; 
} 

public String getBrandWith_SKU() { 
    return BrandWith_SKU; 
} 

public int getSKU_Id() { 
    return SKU_Id; 
} 

public int getPlannedVolume() { 
    return PlannedVolume; 
} 

public int getPlannedSalesPromoParty() { 
    return PlannedSalesPromoParty; 
} 

public int getPlannedSalesPromoMgr() { 
    return PlannedSalesPromoMgr; 
} 

}

這是列表元素XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@android:color/white" 
android:orientation="vertical"> 

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

    <TextView 
     android:id="@+id/textViewItem" 
     android:layout_width="120dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:text="Item Code" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/textViewPlanned" 
     android:layout_width="90dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:text="0 CS " 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/textViewPalnnedParty" 
     android:layout_width="90dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:text="0" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/textViewPlannedMgr" 
     android:layout_width="90dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:text="0" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <EditText 
     android:id="@+id/editTextAgreed" 
     android:layout_width="80dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:hint="000" 
     android:inputType="number" 
     android:maxLength="3" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 


    <EditText 
     android:id="@+id/editTextAgreedParty" 
     android:layout_width="80dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:hint="000" 
     android:inputType="number" 
     android:maxLength="3" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <EditText 
     android:id="@+id/editTextAgreedMgr" 
     android:layout_width="80dp" 
     android:layout_height="60dp" 
     android:gravity="center" 
     android:hint="000" 
     android:inputType="number" 
     android:maxLength="3" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

</LinearLayout> 

+0

你還可以顯示你的'POJOOrder.java'和'element_order.xml'嗎? – x0r

+0

@ x0r更新了問題 – WISHY

+0

你可以發佈你的應用截圖嗎?我無法理解它在哪裏複製。 –

回答

0

所以我想你忘了清理農地,如果你沒有保存價值的那些領域,基本嘗試,而不是這樣的:

if (agreedVolume[holder.number] != null) { 
    holder.etAgreed.setText("" + agreedVolume[position]); 
} 

if (agreedSalesParty[position] != null) { 
    holder.etAgreedParty.setText("" + agreedSalesParty[position]); 
} 

if (agreedSalesMgr[position] != null) { 
    holder.etAgreeMgr.setText("" + agreedSalesMgr[position]); 
} 

是這樣的:

holder.etAgreed.setText(agreedVolume[holder.number] != null ? "" + agreedVolume[position] : ""); 
holder.etAgreedParty.setText(agreedSalesParty[position] != null ? "" + agreedSalesParty[position] : ""); 
holder.etAgreeMgr.setText(agreedSalesMgr[position] != null ? "" + agreedSalesMgr[position] : ""); 
0

試試這個一個,它可能工作:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
View vi = null; 
    convertView=null; 
    if (convertView == null) { 
     vi = LayoutInflater.from(this.context).inflate(
       R.layout.row_item_edit, null); 
     holder = new ViewHolder(); 
     ......... 
     ........... 
     .............. 
    } 

return vi; 
} 
0

還需要在設置文本中有其他部分

if (agreedVolume[holder.number] != null) { 
     holder.etAgreed.setText("" + agreedVolume[position]); 
    }else{ 
     holder.etAgreed.setText(""); 
    } 

    if (agreedSalesParty[position] != null) { 
     holder.etAgreedParty.setText("" + agreedSalesParty[position]); 
    }else{ 
     holder.etAgreedParty.setText(""); 
    } 

    if (agreedSalesMgr[position] != null) { 
     holder.etAgreeMgr.setText("" + agreedSalesMgr[position]); 
    }else{ 
     holder.etAgreeMgr.setText(""); 
    }