我想在我的应用中使用自定义ListView .. 我创建了一个扩展BaseAdapter的Myadapter。 每个项目都有一个文本视图和切换按钮.. 问题是程序,当用户点击项目1的切换按钮时, 切换按钮项目4和项目6.自动切换... 我不知道有什么问题请帮帮我... 抱歉。我的英语不好
java代码是:
public class PayamActivity extends Activity {
Myadapter adapter;
ListView list;
String [] payam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
payam=getResources().getStringArray(R.array.payam);
list=(ListView) findViewById(R.id.listView1);
}
public class Myadapter extends BaseAdapter
{
LayoutInflater myInflater;
public Myadapter(Context context) {
myInflater=LayoutInflater.from(context);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return payam.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder Holder;
;
if(convertView==null)
{
Holder=new ViewHolder();
convertView=myInflater.inflate(R.layout.item_custom_layout,null );
Holder.message=(TextView)convertView.findViewById(R.id.text_massege);
Holder.heart=(ToggleButton)convertView.findViewById(R.id.toggleButton_heart);
convertView.setTag(Holder);
}
else
Holder=(ViewHolder) convertView.getTag();
Holder.message.setText(payam[position]);
return convertView;
}
}
public class ViewHolder extends Activity{
TextView message;
ToggleButton heart;
}
}
答案 0 :(得分:0)
每当您点击toogle按钮时,请将该行位置保留在适合您的arraylist或数组中...
并在getview()
添加这样的代码,
if(array contains the id)
Holder.heart.setchecked(true);
else
Holder.heart.setchecked(false);
请查看此链接
http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html