Frage

I have found another thread with the same question but that answer didn't work for me so I hope someone can help me with this.

I have multiple tablerows with each one EditText and one button in it. The Button is used for storing the value of the EditText in a variable.

This is my tablerow adapter.

    package com.example.vanhulzenapp;

    import java.util.List;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;
    
    public class ProductListAdapter extends BaseAdapter {
    
    private final List<TableRow> rows;
    /**
     * Adapter for TableRow-objects 
     */
    public ProductListAdapter(final Context context, final int itemResId, final List<TableRow> items){
        this.rows = items;
    }
    
    public int getCount(){
        return this.rows.size();
    }
    public Object getItem(int position){
        return this.rows.get(position);
    }
    public long getItemId(int position){
        return position;
    }
    
    
    /**
     * Set the content for a row here
     */
    
    
    public View getView(int position, View convertView, ViewGroup parent){
        final TableRow row = this.rows.get(position);
        View itemView = null;
        
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater)     parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.list_item,null);
        } else {
            itemView = convertView;
        }
        
        // Set the text of the row
        TextView productname = (TextView) itemView.findViewById(R.id.productName);
        productname.setText(row.getName());
        
        TextView productprice = (TextView) itemView.findViewById(R.id.productPrice);
        productprice.setText(row.getPrice());
        
        /*EditText productvalue = (EditText) itemView.findViewById(R.id.editValue);
        productvalue.setText(Integer.toString(row.getValue()));*/
        
        // Remember the row for each button so that we can refer to
        // it when the button is clicked
        ImageButton addItemButton = (ImageButton) itemView.findViewById(R.id.addItemButton);
        addItemButton.setTag(row);      
        
        
        
        return itemView;
    }
}

This is the TableRow class

    package com.example.vanhulzenapp;

    public class TableRow{
    private String name;
    private String price;
    private int value;
    
    
    public TableRow(String name, String price){
        this.name = name;
        this.price = price;
    }
    public void setName(String name){
        this.name = name;;
    }
    public String getName(){
        return name;
    }
    public void setValue(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
    public void setPrice(String price){
        this.price = price;
    }
    public String getPrice(){
        return price;
    }
}

and the main class where the click event occurs.. The button knows in which row it is by using the gettag() But I can't get the EditText to know in which row it is.

War es hilfreich?

Lösung

You can set tag on EditText as well.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top