2017-07-12 5 views
-1

は私が項目のリストを持って削除します。すなわち、入力されたのEditTextが、それは次のように

public class ProductAdapter extends ArrayAdapter<Product> { 
 

 
    private Context context; 
 
    private List<Product> mdata; 
 

 

 
    public ProductAdapter(Context context, List<Product> mdata) { 
 
     super(context, R.layout.act_item_product_adapter, mdata); 
 
     this.context = context; 
 
     this.mdata = mdata; 
 
    } 
 

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

 
     View item = LayoutInflater.from(context).inflate(R.layout.act_item_product_adapter,null); 
 
     TextView m_product_name = (TextView) item.findViewById(R.id.adapter_product_nameProduct); 
 
     TextView m_product_cost = (TextView) item.findViewById(R.id.adapter_product_cost); 
 
     TextView m_product_stock = (TextView) item.findViewById(R.id.adapter_product_stock); 
 
     EditText m_product_amount = (EditText) item.findViewById(R.id.adapter_product_amount); 
 
     ImageView m_product_photo = (ImageView) item.findViewById(R.id.adapter_product_photo); 
 

 

 

 
     m_product_name.setText(datos.get(position).getNombreProducto()); 
 
     m_product_cost.setText(" Cost: " + String.format("%.2f",mdata.get(position).getCostSold())); 
 
     m_product_stock.setText(" Stock: " + Integer.toString(mdata.get(position).getStock())); 
 
     //m_product_amout.setText("0"); 
 

 
     if (datos.get(position).getimagenProduct() != null){ 
 
      byte[] decodedString = Base64.decode(mdata.get(position).getimagenProduct(), Base64.DEFAULT); 
 
      m_product_photo.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length)); 
 
     } 
 

 

 
     return item; 
 
    } 
 
}

すべてがうまくいき、商品の情報を取得してリストに表示できますが、EditText(m_product_amount)に値を入力してアクティビティの別のオブジェクトをクリックすると、入力された数字は削除され、0のままですレイアウト内のデフォルトの番号です。

私は間違っていますか?

任意のアイデアや提案は

おかげで歓迎されている

=============質問更新===============

act_item_product_adapter.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"> 
 
    <ImageView 
 
     android:id="@+id/adapter_product_photo" 
 
     android:layout_width="80dp" 
 
     android:layout_height="80dp" 
 
     android:scaleType="centerInside" 
 
     android:background="@android:color/white" 
 
     android:cropToPadding="true"/> 
 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="horizontal" 
 
     android:layout_toRightOf="@id/adapter_product_photo" 
 
     android:id="@+id/linearLayout2"> 
 
     <TextView 
 
      android:id="@+id/adapter_product_nameProducto" 
 
      android:layout_width = "match_parent" 
 
      android:layout_height = "37dp" 
 
      android:gravity="center" 
 
      android:textSize="20dp" 
 
      android:text="Name of product"/> 
 
    </LinearLayout> 
 
    <LinearLayout 
 
     android:layout_width= "170dp" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="vertical" 
 
     android:id="@+id/linearLayout3" 
 
     android:layout_below="@+id/linearLayout2" 
 
     android:layout_toRightOf="@+id/adapter_product_photo" 
 
     android:layout_alignBottom="@+id/adapter_product_photo"> 
 
     <TextView 
 
      android:id="@+id/adapter_product_cost" 
 
      android:layout_width = "170dp" 
 
      android:layout_height = "wrap_content" 
 
      android:text="cost"/> 
 
     <TextView 
 
      android:id="@+id/adapter_product_stock" 
 
      android:layout_width="170dp" 
 
      android:layout_height="wrap_content" 
 
      android:text="stock"/> 
 
    </LinearLayout> 
 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="80dp" 
 
     android:layout_below="@+id/linearLayout2" 
 
     android:layout_toEndOf="@+id/linearLayout3" 
 
     android:layout_toRightOf="@+id/linearLayout3" 
 
     android:orientation="horizontal" 
 
     android:layout_alignBottom="@+id/linearLayout3"> 
 
     <TextView 
 
      android:layout_width = "wrap_content" 
 
      android:layout_height = "match_parent" 
 
      android:gravity="center" 
 
      android:textSize="20dp" 
 
      android:text="Amount"/> 
 
     <EditText 
 
      android:layout_width = "match_parent" 
 
      android:layout_height = "match_parent" 
 
      android:inputType="number" 
 
      android:id="@+id/adapter_product_amount" 
 
      android:gravity="center" 
 
      android:text="0"/> 
 
    </LinearLayout> 
 
</RelativeLayout>

+0

私はあなたのレイアウト(複数可)を投稿示唆しています。 RecyclerViewを使うことをお勧めします。 –

+0

さて、レイアウトで質問を更新します。ありがとう –

+0

'EditText'フィールドに入力したものをどこで取得しているのかわかりません... – DigitalNinja

答えて

0

ListViewに複数のアイテムが表示されないことが考えられます。アイテムには一時的な情報(数ミリ秒)が表示され、次のアイテムは他の値で表示されます。

これは、別の部分を指すときに情報が削除され、public Viewで割り当てられたものがgetViewになるためです。

以下のように、私はアイテムの数に応じのEditTextの値の文字列型の配列を作成する:

public class ProductAdapter extends ArrayAdapter<Producto> { 
 

 
    private Context context; 
 
    private List<Product> mdata; 
 

 
    private EditText m_product_amount; 
 
    private String[] m_amounts; 
 
    
 
    public ProductAdapter(Context context, List<Product> mdata) { 
 
     super(context, R.layout.act_item_product_adapter, mdata); 
 
     this.context = context; 
 
     this.mdata = mdata; 
 
     m_amounts = new String[mdata.size()]; 
 

 
    } 
 

 
    ... 
 
}

そして私が含まれている値とEDITTEXTを初期化します配列は、次のように:

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

 
     View item = LayoutInflater.from(context).inflate(R.layout.act_item_product_adapter,null); 
 
     
 
     .... 
 

 

 
     if (null != m_amounts[position]) m_product_amount.setText(m_amounts[position]); 
 

 

 
     .... 
 
     
 
     return item; 
 
}

そして最後に、私は次の操作を行い、配列の各要素の値を更新する:

@Override 
 
    public View getView(final int position, View convertView, ViewGroup parent) { 
 
    
 
    .... 
 
    
 
    
 
    
 
    
 
    m_product_amount.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
 

 
      @Override 
 
      public void onFocusChange(View v, boolean hasFocus) { 
 
       switch (v.getId()) { 
 
        case R.id.adapter_product_amount:{ 
 
         if (false == hasFocus && m_product_amount.getText().length() > 0) { 
 
          m_amounts[position] = m_product_amount.getText().toString(); 
 
         } 
 
        } 
 
       } 
 
      } 
 
     }); 
 
    
 
    ... 
 
    
 
    return item; 
 
    
 
}

関連する問題