在按钮单击android中获取列表视图edittext的值

时间:2016-07-22 06:16:03

标签: android listview android-edittext

我想得到listview项目,我按下按钮点击并在保存按钮我希望得到我在下面的arraylist中的listitem输入的任何内容是我的代码

public class AddNewPerson extends Activity {

private AtomPayListAdapter adapter;
Button mButtonSave;
private List<AtomPayment> items;
private List<AtomPayment> Finalitems;
AtomPayment mAtomPayment=new AtomPayment();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);
    mButtonSave=(Button)findViewById(R.id.EnterPays_saveAtomPayment);
    setupListViewAdapter();
    items=new ArrayList<>();
    Finalitems=new ArrayList<>();
    setupAddPaymentButton();
    mButtonSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        Log.d("Finalitems",String.valueOf(items.size()));
            for (int i=0;i<items.size();i++)
            {
                Log.d("Name",items.get(i).getName());
                Log.d("Value",String.valueOf(items.get(i).getValue()));
            }
        }
    });
}

public void removeAtomPayOnClickHandler(View v) {
    AtomPayment itemToRemove = (AtomPayment)v.getTag();
    adapter.remove(itemToRemove);
    Finalitems.remove(itemToRemove);
}

private void setupListViewAdapter() {
    adapter = new AtomPayListAdapter(AddNewPerson.this, R.layout.atom_pay_list_item, new ArrayList<AtomPayment>());
    ListView atomPaysListView = (ListView)findViewById(R.id.EnterPays_atomPaysList);
    atomPaysListView.setAdapter(adapter);
    adapter.insert(new AtomPayment("", 0), 0);
}

private void setupAddPaymentButton() {
    findViewById(R.id.EnterPays_addAtomPayment).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            adapter.insert(new AtomPayment("", 0), 0);
            Finalitems.add(new AtomPayment("",0));
        }
    });
}

public class AtomPayListAdapter extends ArrayAdapter<AtomPayment> {

    public    String LOG_TAG = AtomPayListAdapter.class.getSimpleName();

    private List<AtomPayment> items;
    private int layoutResourceId;
    private Context context;

    public AtomPayListAdapter(Context context, int layoutResourceId, List<AtomPayment> items) {
        super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.items = items;
        Finalitems=items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
       final AtomPaymentHolder holder ;

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new AtomPaymentHolder();
        holder.atomPayment = items.get(position);
        holder.removePaymentButton = (ImageButton)row.findViewById(R.id.atomPay_removePay);
        holder.removePaymentButton.setTag(holder.atomPayment);

        holder.name = (TextView)row.findViewById(R.id.atomPay_name);
        setNameTextChangeListener(holder);
        holder.value = (TextView)row.findViewById(R.id.atomPay_value);
        setValueTextListeners(holder);

        row.setTag(holder);

        setupItem(holder);


        return row;
    }

    private void setupItem(AtomPaymentHolder holder) {
        holder.name.setText(holder.atomPayment.getName());
        holder.value.setText(String.valueOf(holder.atomPayment.getValue()));
    }

    public   class AtomPaymentHolder {
        AtomPayment atomPayment;
        TextView name;
        TextView value;
        ImageButton removePaymentButton;
    }

    private void setNameTextChangeListener(final AtomPaymentHolder holder) {
        holder.name.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                holder.atomPayment.setName(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void afterTextChanged(Editable s) { }
        });
    }

    private void setValueTextListeners(final AtomPaymentHolder holder) {
        holder.value.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try{
                    holder.atomPayment.setValue(Double.parseDouble(s.toString()));

                }catch (NumberFormatException e) {
                    Log.e(LOG_TAG, "error reading double value: " + s.toString());
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void afterTextChanged(Editable s) { }
          });
      }
  }

}

当我点击保存按钮时,它在我的自定义arraylisy中给出了0项目列表,那么我该如何解决这个问题呢?你的所有建议都很明显

1 个答案:

答案 0 :(得分:3)

在适配器中创建一个方法,该方法返回正在使用的列表。 喜欢:

public ArrayList<AtomPayment> getList() {
    return items;
}

然后在onClick按钮上调用此方法。 喜欢:

List<AtomPayment> paymentList = adapter.getList();

并使用此列表进行进一步操作。

mButtonSave.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    List<AtomPayment> paymentList = adapter.getList();
    Log.d("Finalitems",String.valueOf(paymentList.size()));
        for (int i=0;i<items.size();i++)
        {
            Log.d("Name",paymentList.get(i).getName());
            Log.d("Value",String.valueOf(paymentList.get(i).getValue()));
        }
    }
});