ListView-Make Button onClick事件为同一行的EditText添加值

时间:2018-12-12 12:43:39

标签: android android-studio

我有一个ListView,在每行中用一个EditText和一个Button填充应用程序。

我已经在按钮上添加了onClick事件,该事件将+1添加到EditText的值。除了每个按钮仅添加到第一个EditText之外,它都可以正常工作。

我如何捕获按下的按钮,以便将+1添加到相应的EditText(在同一Button行上)?

这是我到目前为止所拥有的:

row_layout_2.xml

   <Button
        android:id="@+id/button_plus"
        android:layout_width="40dp"
        android:layout_height="50dp"
        android:text="+"
        android:textSize="15sp"
        android:onClick="addone"
         />

MainActivity.xml

public void addone(View v){

    int numtest = 1;

    TextView t = (TextView) findViewById(R.id.edit_text_1);

    int numtemp = Integer.parseInt(t.getText().toString());

    int sum_numbers = numtest + numtemp;

    String final_sum = String.valueOf(sum_numbers);

    t.setText(final_sum);
}

CustomAdapter.java

package com.example.serveira.fluxoparagem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

//The following adapter makes it possible for me to put both strings
// into different columns.

public class CustomAdapter extends ArrayAdapter {

    private Context context;
    private String[] products;
    private String[] prices;

    public CustomAdapter(Context context, int resource, String[] products, String[] prices) {
        super(context, resource);
        this.context = context;
        this.products = products;
        this.prices = prices;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_layout_2, parent, false);
        TextView column1 = view.findViewById(R.id.textView1);
        TextView column2 = view.findViewById(R.id.textView2);
        column1.setText(products[position]);
        column2.setText(prices[position]);
        return view;
    }

}

2 个答案:

答案 0 :(得分:1)

将onClick方法放入适配器的getView方法中,如下所示

public View getView(final int position, View convertView, ViewGroup parent) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_layout_2, parent, false);
    final TextView column1 = view.findViewById(R.id.textView1);
    final TextView column2 = view.findViewById(R.id.textView2);
    Button plus1 = view.findViewById(R.id.button_plus);

    column1.setText(products[position]);
    column2.setText(prices[position]);

    plus1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                products[position]++;
                column1.setText(products[position]);
            }
        });
    return view;
}

答案 1 :(得分:0)

嗨,您可以在适配器类中做到这一点。

static changeLayerVisibility(layer) {
    let mapLayers = [... AxisStore.getStore().getState().map.mapGLlayers];
    const ids = mapLayers.map(l => l.id);
    const index = ids.indexOf(layer.id);
    const newVisibility = (layer.layout.visibility === 'visible') ? 'none' : 'visible';

    let nextVisibleLayer = mapLayers.find((layerObj, idx) => idx > index && layerObj.layout.visibility === 'visible');

    let updateLayout = Object.assign({}, mapLayers[index], {
        layout: Object.assign({}, mapLayers[index].layout, { visibility: newVisibility }),
        paint: Object.assign({}, mapLayers[index].paint),
        before: newVisibility === 'visible' && nextVisibleLayer ? nextVisibleLayer.id: undefined
    });

    mapLayers.splice(index, 1, updateLayout);

    return (dispatch) => {
        dispatch({
            type: MapTypeConstants.SET_MAP_LAYERS,
            payload: mapLayers
        });
    };
}