列表视图中第一次加载16个项目。 我只需要选择2个项目(红色按钮)。 但是当我在显示错误的选择的11项之后滚动列表视图所有项目时。
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
v.setTag(holder);
}
else
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
return v;
}
答案 0 :(得分:0)
请另外写下其他部分。
if (selectedarray.get(position).equals("1")) {
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
} else {
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
答案 1 :(得分:0)
首先
if (selectedarray.get(position).equals("1")) {
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
} else {
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
其次,请勿将其保留在if (convertView == null)
或else
部分
在if和else之后保留它
见这里
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
v.setTag(holder);
}
else
{
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
}
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
return v;
}
答案 2 :(得分:0)
public View getView(final int position, View convertView, ViewGroup parent)
{
v = convertView;
holder = new PlanetHolder();
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
holder.btn=(Button)v.findViewById(R.id.btn1);
holder.displayname = (TextView)v.findViewById(R.id.txt);
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
holder.btn.setOnClickListener(new OnClickListener()
{
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
v.setTag(holder);
}
else
Log.d("else_position", "else_position= "+position);
holder = (PlanetHolder) v.getTag();
ItemList p = planetList.get(position);
holder.displayname.setText(p.getU_name());
if(selectedarray.get(position).equals("1"))
{
holder.btn.setBackgroundColor(R.color.red);
holder.btn.setClickable(false);
}
else
{
holder.btn.setBackgroundColor(R.color.white);
holder.btn.setClickable(true);
}
return v;
}