使用edittext自定义列表视图

时间:2015-03-18 03:47:19

标签: android android-listview

当我在1个listitems的编辑文本中输入值时,我使用带有2个edittexts和1个textview的自定义列表视图。在滚动时将edittext1的值复制到editext10,然后在edittext21上复制,依此类推,当我调用arrUT1 [n]时,它给出空指针异常

    public class Result extends ActionBarActivity implements AdapterView.OnItemClickListener {
    ListView listView;
    ResultAdapter resultAdapter;
    Button submt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        resultAdapter = new ResultAdapter(this);
        listView = (ListView)findViewById(R.id.listView4);
        listView.setAdapter(resultAdapter);

        submt=(Button)findViewById(R.id.submit4);
        submt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Toast.makeText(Result.this,""+resultAdapter.arrUT1[1] + "," + resultAdapter.arrUT2[1], Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(),"error "+e.toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}

class ResultAdapter extends BaseAdapter {

    String[] arraylist = new String[90];
    public String[] arrUT1,arrUT2;
    private Context context;

    ResultAdapter(Context context){
        this.context=context;
        for(int i=0;i<90;i++){
            arraylist[i]=(i+1)+" :";
        }
    }
    @Override
    public int getCount() {
        return arraylist.length;
    }

    @Override
    public Object getItem(int position) {
        return arraylist[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        //View row=null;
        if(convertView==null){
            holder=new ViewHolder();
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.result_list,parent,false);
            holder.editText1=(EditText)convertView.findViewById(R.id.ut1);
            holder.editText2=(EditText)convertView.findViewById(R.id.ut2);
            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }
        holder.ref=position;
        TextView titleTextView=(TextView)convertView.findViewById(R.id.rollview);
        titleTextView.setText(arraylist[position]);

        //editText1.setText(arrUT1[position]);
       // editText2.setText(arrUT2[position]);
       // final int pos=position;
        holder.editText1.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) {
                try {
                    arrUT1[holder.ref] = s.toString();
                }
                catch (Exception e){
                   // Toast.makeText(,"error "+e.toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.editText2.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 s1) {
                try{
                    arrUT2[holder.ref]=s1.toString();
                }catch (Exception e){}



            }
        });
        return  convertView;
    }

    private class ViewHolder {
        TextView textView1;
        EditText editText1,editText2;
        int ref;
    }

}

1 个答案:

答案 0 :(得分:0)

要避免 当我在1个列表项的编辑文本中输入值时,edittext1的值会在滚动时复制到editext10,然后在edittext21上复制,依此类推

请使用以下代码。     class ResultAdapter扩展了BaseAdapter {

        public String[] arrUT1 = new String[90] ;
        public String[]  arrUT2 =   new String[90];
        private Context context;

        ResultAdapter(Context context) {
            this.context = context;

        }

        @Override
        public int getCount() {
            return arrUT1.length;
        }

        @Override
        public Object getItem(int position) {
            return arrUT1[position];
        }

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

        @Override
        public View getView(int position, View convertView, final ViewGroup parent) {
            final ViewHolder holder;
            //View row=null;
            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item2, parent, false);
                holder.editText1 = (EditText) convertView.findViewById(R.id.et1);
                holder.editText2 = (EditText) convertView.findViewById(R.id.et2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.ref = position;
            TextView titleTextView = (TextView) convertView.findViewById(R.id.textView2);
            titleTextView.setText(""+position);
            holder.editText1.setText(arrUT1[position]);
            holder.editText2.setText(arrUT2[position]);


            holder.editText1.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 s1) {

                    try {
                        arrUT1[holder.ref] = s1.toString();
                    } catch (Exception e) {
                    }


                }
            });

            holder.editText2.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 s1) {
                    try {
                        arrUT2[holder.ref] = s1.toString();
                    } catch (Exception e) {
                    }


                }
            });

            return convertView;
        }

并在结果按钮上单击使用日志打印而不是吐司以便更好地理解。

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    for (int i = 0; i <resultAdapter.arrUT1.length ; i++) {
                        Log.i("TAG"  , " Array " +resultAdapter.arrUT1[i] );
                    }
                    for (int i = 0; i <resultAdapter.arrUT2.length ; i++) {
                        Log.i("TAG", " Array 2 " + resultAdapter.arrUT2[i]);
                    }


                }
            });