2017-07-12 5 views
-1

ich eine Liste der Elemente haben, wie folgt:EditText nicht den Text halten eingegeben, dh er löscht

private void getProducts() { 
 
     List<Product> listProducts = Product.getListProduct(true); 
 
     if (listProducts != null && listProducts.size() > 0) { 
 
      ProductAdapter m_adapterProduct = new ProductAdapter(this, listProducts); 
 
      mlvwProducts.setAdapter(m_adapterProduct); 
 
     } 
 
    }

Die ProductAdapter Klasse ist wie folgt:

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

Alles funktioniert sehr gut, ich kann die Informationen der Produkte abrufen und sie in der Liste anzeigen, aber wenn ich in EditText (m_product_amount) einen Wert eingeben und auf ein anderes Objekt der Aktivität klicken, wird die eingegebene Nummer gelöscht und bleibt bei 0 Das war die Standardnummer im Layout.

Was mache ich falsch?

Irgendwelche Ideen oder Vorschläge sind willkommen

Dank

============= Frage Aktuelles ===============

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

Ich schlage vor, Sie Ihr Layout erstellen (s). Ich schlage vor, dass Sie einen RecyclerView verwenden. –

+0

Ok, ich werde die Frage mit dem Layout aktualisieren. Danke –

+0

Ich sehe nicht, wo Sie abholen, was Sie in das 'EditText'-Feld eingegeben haben ... – DigitalNinja

Antwort

0

Ich vermute, ListView zeigt nicht mehrere Elemente an, es ist ein einzelnes Element, in dem temporäre Informationen (einige Millisekunden) für die darin enthaltenen Objekte angezeigt werden und anschließend das nächste Element angezeigt wird, das das gleiche, jedoch andere Werte aufweist.

Aus diesem Grund werden die Informationen gelöscht, wenn auf einen anderen Teil verwiesen wird, und der, der in der öffentlichen Ansicht getView zugewiesen wurde.

Dann schaffe ich ein Array vom Typ String für den Wert von EditText gemäß der Anzahl der Artikel, wie folgt:

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

 
    } 
 

 
    ... 
 
}

Dann ich die editText mit den Werten initialisiert enthalten in das Array wie folgt:

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

Und schließlich die Werte jedes Element des Arrays zu aktualisieren ich folgendes tun:

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

Verwandte Themen