Android listview中的Textswitcher仅更新列表中的最后一项
我的文本切换器工作不正常。每次我单击列表中的任何图标(加号图标)时…最后一个文本切换器,即列表中最后一个元素的文本切换器,无论您单击哪个加号。列表中的其他每个元素都正确显示。 我从一个数据库中获取了数据。下面的代码是我的自定义适配器代码。dataid为id提供了该位置的元素Android listview中的Textswitcher仅更新列表中的最后一项,android,listview,text,Android,Listview,Text,我的文本切换器工作不正常。每次我单击列表中的任何图标(加号图标)时…最后一个文本切换器,即列表中最后一个元素的文本切换器,无论您单击哪个加号。列表中的其他每个元素都正确显示。 我从一个数据库中获取了数据。下面的代码是我的自定义适配器代码。dataid为id提供了该位置的元素 private class SubjectDataList extends ArrayAdapter<String> { private TextSwitcher mSwitcher;
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;
}
}
私有类SubjectDataList扩展了ArrayAdapter{
专用文本切换器mSwitcher;
列出姓名;
按钮btnadd;
Typeface newsfont=Typeface.createFromAsset(getContext().getAssets(),
“Adec.ttf”);
List dataid=helper.getAllSubjectsTSSID();
public SubjectDataList(上下文,int textViewResourceId,
列表名称){
super(上下文、textViewResourceId、名称);
//TODO自动生成的构造函数存根
名称=名称;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回helper.getSubjectCount();
}
@凌驾
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
视图行=转换视图;
if(行==null){
行=getActivity().GetLayoutFlater().充气(
R.layout.subjects列表,父项,false);
mSwitcher=(TextSwitcher)row.findviewbyd(R.id.subcount);
btnadd=(按钮)行。findViewById(R.id.addcountbtn);
mSwitcher.setFactory(新的ViewFactory(){
公共视图makeView(){
//TODO自动生成的方法存根
//创建新的textView并设置如下属性
//颜色、尺寸等
TextView myText=newtextView(getActivity());
myText.setGravity(Gravity.TOP
|重心(水平);
myText.setText(“0”);
myText.setTextSize(36);
myText.setTextColor(Color.GREEN);
设置字体(新闻字体);
返回myText;
}
});
//声明输入和输出动画并初始化它们
动画输入=AnimationUtils.loadAnimation(getActivity(),
R.anim.滑入(向上);
动画输出=AnimationUtils.loadAnimation(getActivity(),
R.anim.滑出/向上);
//设置textSwitcher的动画类型
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(输出);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
mSwitcher.setText(String.valueOf(helper
.getAllSubjectstScont(dataid.get(position));
}
});
TextView text=(TextView)row.findViewById(R.id.subjectname);
text.setTypeface(新闻字体);
text.setText(name.get(position));
}
返回行;
}
}
这是我获取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;
}
//获取主题id
公共列表GetAllSubjectsTSSID(){
列表=新的ArrayList();
SQLiteDatabase db=this.getWritableDatabase();
Cursor cur=db.rawQuery(“从”+subtable中选择”+colsubID+”
+其中“+colsubjectstudentid+”=“+getPos(),null);
//循环遍历所有行并添加到列表
if(cur.moveToFirst()){
做{
list.add(cur.getString(0));
}while(cur.moveToNext());
}
//闭合连接
cur.close();
db.close();
退货清单;
}
在getView
方法中移动以下声明
TextSwitcher mSwitcher;
Button btnadd;
原因:当您在getView方法之外声明它时,最后一行将超过
mSwitcher
的ViewFactory
实例,因为它的范围在该方法之外。这是在拯救生命……thnxx很多…-)