0

我的文本切换器无法正常工作。每次我单击列表上的任何图标(加号图标)...最后一个文本切换器,即列表中最后一个元素的文本切换器,无论您点击哪个加号。所有其他元素列表显示正确。我从数据库中获取了数据,下面的代码是我的自定义适配器代码。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;
}
4

1 回答 1

0

在方法中移动以下声明getView

TextSwitcher mSwitcher;
Button btnadd;

原因:当你在getView方法外声明它时,最后一行会超过 ViewFactoryinstance for,mSwitcher因为它的作用域在方法外。

于 2013-11-06T12:15:48.713 回答