列表视图中的文本切换器仅更新列表中的最后一项

时间:2013-11-06 12:11:20

标签: android listview text

我的文本切换器无法正常工作。每次我点击列表中的任何图标(加上图标)...最后一个文本切换器,即列表中最后一个元素的文本切换器,无论哪个加号,你都可以点击。列表中的其他元素正确显示。 我从数据库中获取了数据,下面的代码是我的自定义适配器代码.dataid给出了该位置元素的id。

private class SubjectDataList extends ArrayAdapter<String> {
        private TextSwitcher mSwitcher;
        List<String> names;
        Button btnadd;
        Typeface newsfont = Typeface.createFromAsset(getContext().getAssets(),
                "Adec.ttf");
        List<String> dataid = helper.getAllSubjectstsid();

        public SubjectDataList(Context context, int textViewResourceId,
                List<String> name) {
            super(context, textViewResourceId, name);
            // TODO Auto-generated constructor stub
            names = name;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return helper.getSubjectCount();
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {

            View row = convertView;

            if (row == null) {
                row = getActivity().getLayoutInflater().inflate(
                        R.layout.subjectslist, parent, false);
                mSwitcher = (TextSwitcher)      row.findViewById(R.id.subjcount);
                btnadd = (Button) row.findViewById(R.id.addcountbtn);

                mSwitcher.setFactory(new ViewFactory() {

                    public View makeView() {
                        // TODO Auto-generated method stub
                        // create new textView and set the properties like
                        // color, size etc
                        TextView myText = new TextView(getActivity());
                        myText.setGravity(Gravity.TOP
                                | Gravity.CENTER_HORIZONTAL);
                        myText.setText("0");
                        myText.setTextSize(36);
                        myText.setTextColor(Color.GREEN);
                        myText.setTypeface(newsfont);
                        return myText;
                    }
                });
                // Declare the in and out animations and initialize them
                Animation in = AnimationUtils.loadAnimation(getActivity(),
                        R.anim.slide_in_up);
                Animation out = AnimationUtils.loadAnimation(getActivity(),
                        R.anim.slide_out_up);

                // set the animation type of textSwitcher
                mSwitcher.setInAnimation(in);
                mSwitcher.setOutAnimation(out);
                btnadd.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        mSwitcher.setText(String.valueOf(helper
                                .getAllSubjectstscount(dataid.get(position))));
                    }
                });

                TextView text = (TextView) row.findViewById(R.id.subjectname);
                text.setTypeface(newsfont);
                text.setText(names.get(position));

            }
            return row;
        }

    }

这是我获取id的功能:

// getting subjects id
public List<String> getAllSubjectstsid() {
    List<String> list = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cur = db.rawQuery("Select " + colsubID + " from " + subjTable
            + " WHERE " + colsubjstudentid + "=" + getPos(), null);
    // looping through all rows and adding to list
    if (cur.moveToFirst()) {
        do {
            list.add(cur.getString(0));
        } while (cur.moveToNext());
    }
    // closing connection
    cur.close();
    db.close();

    return list;
}

1 个答案:

答案 0 :(得分:0)

getView方法中移动以下声明。

TextSwitcher mSwitcher;
Button btnadd;

原因:当您在 getView 方法之外声明它时,最后一行将超过ViewFactory的{​​{1}}实例,因为它的范围超出了方法。