带有editText和Check Box和TextView

时间:2015-10-11 05:25:35

标签: android listview android-adapter custom-adapter listview-adapter

我被困在listview的适配器中:  我在适配器中有一个textView(名称),复选框,editText(名称数量)。  默认editText将被禁用但当我选中任何复选框时,只有我能输入数量。  在getView中,我坚持使用代码。在listview的OnItemClick中,我需要存储Model对象并存储在DataBase上。

package com.example.lokesh.agrigate.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lokesh.agrigate.R;
import com.example.lokesh.agrigate.models.Crop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Amit on 2015-10-10.
 */
public class CropListViewAdapter extends BaseAdapter {
    Context mContext;
    List<Integer> positions;
    List<Crop> mCrops;
    public CropListViewAdapter(Context mContext, List<Crop> mCrops) {
        this.mContext = mContext;
        this.mCrops = mCrops;
        mSelectedList=new HashMap<>();
        positions=new ArrayList<>();
    }

    Map<Crop,Integer> mSelectedList=null;



    public void addCrop(List<Crop> crops){
        this.mCrops=crops;
    }

    @Override
    public int getCount() {
        return mCrops.size();
    }

    @Override
    public Crop getItem(int position) {
        return mCrops.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Crop crop=getItem(position);
        Holder mHolder=null;
        if(convertView==null){
            LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=  inflater.inflate(R.layout.crop_list_item,null);
            mHolder=new Holder();
            convertView.setTag(mHolder);

            mHolder.mName=(TextView)convertView.findViewById(R.id.crpname);
            mHolder.mHelpText=(TextView)convertView.findViewById(R.id.quantityHelpText);
            mHolder.mCheckox=(CheckBox)convertView.findViewById(R.id.checkCrop);
            mHolder.mQuantityEditText=(EditText)convertView.findViewById(R.id.quantity);

            mHolder.mQuantityEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            mHolder.mCheckox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (int) buttonView.getTag();
                    if (isChecked) {


                    } else {

                    }
                    buttonView.setChecked(isChecked);
                }
            });


        }else{
            mHolder=(Holder)convertView.getTag();
        }

        mHolder.mCheckox.setTag(position);
        mHolder.mName.setText(crop.toString());

        return convertView;
    }

    public void setData(List<Crop> crops) {
        this.mCrops=crops;
    }


    class Holder{
        TextView mName;
        TextView mHelpText;
        CheckBox mCheckox;
        EditText mQuantityEditText;


    }
}

活动OnItemClick:

Map<Crop,Integer> data=new HashMap<>();
AdapterView.OnItemClickListener onCropItemClickListener=new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CheckBox checkBox=(CheckBox)view.findViewById(R.id.checkCrop);
        Crop crop=(Crop)parent.getItemAtPosition(position);
        if(checkBox.isChecked()){

            EditText ed=(EditText)view.findViewById(R.id.quantity);
            if(!TextUtils.isEmpty(ed.getText().toString()))
              data.put(crop,Integer.parseInt(ed.getText().toString()));
        }else{
           data.remove(crop);
        }
    }
};

请帮我纠正适配器和OnItemClick:

2 个答案:

答案 0 :(得分:0)

启用setOnCheckedChangeListener块中的编辑文本。

myvec<- c("chr2:213403244:213403244:G:T:snp","chr7:55240586:55240586:T:G:snp" ,"chr7:55241607:55241607:C:G:snp")

result
chr2:213403244   
chr7:55240586
chr7:55241607

答案 1 :(得分:0)

在对不同网站进行大量搜索并低估问题并更正我的代码后,我创建了新模型并使用它而不是裁剪,检查我的代码:

    mCropList=mDbAdapter.getAllCrops();
    mEmployees=mDbAdapter.getAllEmployee();
    mTransactionModels=new ArrayList<>();
    for(Crop crop: mCropList){
        TransactionModel transactionModel=new TransactionModel();
        transactionModel.setIsSelected(false);  // this is to keep track of checkbox selection
        transactionModel.setQuantity(0);  // get EditText value of quantity .
        transactionModel.setDate(date);
        transactionModel.setName(crop.getName());
        transactionModel.setId(crop.getId());
        mTransactionModels.add(transactionModel);
    }



mCropListViewAdapter=new CropListViewAdapter(mContext,transactionModels);
            mCropListView.setAdapter(mCropListViewAdapter);   // Setting adapter

现在,下面是修改后的适配器代码:

package com.example.lokesh.agrigate.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lokesh.agrigate.R;
import com.example.lokesh.agrigate.models.Crop;
import com.example.lokesh.agrigate.models.TransactionModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Amit on 2015-10-10.
 */
public class CropListViewAdapter extends BaseAdapter {
    Context mContext;
    List<Integer> positions;
    List<TransactionModel> mTransactionModels;
    public CropListViewAdapter(Context mContext, List<TransactionModel> mTransactionModels) {
        this.mContext = mContext;
        this.mTransactionModels = mTransactionModels;
        mSelectedList=new HashMap<>();
        positions=new ArrayList<>();
    }

    Map<Crop,Integer> mSelectedList=null;



    public void addCrop(List<TransactionModel> mTransactionModels){
        this.mTransactionModels=mTransactionModels;
    }

    @Override
    public int getCount() {
        return mTransactionModels.size();
    }

    @Override
    public TransactionModel getItem(int position) {
        return mTransactionModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       final TransactionModel transactionModel =getItem(position);
        Holder mHolder=null;
        if(convertView==null){
            LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=  inflater.inflate(R.layout.crop_list_item,null);
            mHolder=new Holder();
            convertView.setTag(mHolder);

            mHolder.mName=(TextView)convertView.findViewById(R.id.crpname);
            mHolder.mHelpText=(TextView)convertView.findViewById(R.id.quantityHelpText);
            mHolder.mCheckox=(CheckBox)convertView.findViewById(R.id.checkCrop);
            mHolder.mQuantityEditText=(EditText)convertView.findViewById(R.id.quantity);
            mHolder.watcher=new MutableWatcher();
            mHolder.mQuantityEditText.addTextChangedListener(mHolder.watcher);


            mHolder.mCheckox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (int) buttonView.getTag();
                    if (isChecked) {


                        mTransactionModels.get(getPosition).setIsSelected(buttonView.isChecked());
                    } else {
                        mTransactionModels.get(getPosition).setIsSelected(buttonView.isChecked());
                    }


                }
            });


        }else{
            mHolder=(Holder)convertView.getTag();
        }
        mHolder.mQuantityEditText.setTag(position);


        mHolder.mCheckox.setTag(position);
        mHolder.mName.setText(transactionModel.getName().toString());
        mHolder.watcher.setActive(false);
        mHolder.mQuantityEditText.setText(mTransactionModels.get(position).getQuantity()+"");
        mHolder.watcher.setPosition(position);
        mHolder.watcher.setActive(true);
        mHolder.mCheckox.setChecked(mTransactionModels.get(position).isSelected());

        return convertView;
    }

    public void setData(List<TransactionModel> mTransactionModels) {
        this.mTransactionModels=mTransactionModels;
    }

    public List<TransactionModel> getmTransactionModels(){
        return mTransactionModels;
    }


    class Holder{
        TextView mName;
        TextView mHelpText;
        CheckBox mCheckox;
        EditText mQuantityEditText;
        MutableWatcher watcher;


    }

    class MutableWatcher implements TextWatcher {

        private int mPosition;
        private boolean mActive;

        void setPosition(int position) {
            mPosition = position;
        }

        void setActive(boolean active) {
            mActive = active;
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

        @Override
        public void afterTextChanged(Editable s) {
            if (mActive) {
                if(s.length()>0) {
                    mTransactionModels.get(mPosition).setQuantity(Integer.parseInt(s.toString()));
                }else {

                    Toast.makeText(mContext, "Not a valid Number", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

现在我将检查修改为:

,而不是阻止Edittext用于未经检查的项目
for(TransactionModel model: models ){
                if(model.isSelected()){
                    if(model.getQuantity()<=0){
                        Toast.makeText(mContext,"Please enter a valid quantity",Toast.LENGTH_SHORT).show();
                        return;
                    }else{
                        model.setEmployee(mCurrentEmployee);

                        mValidModels.add(model);
                    }

                }else{
                    count++;
                }
            }