gpt4 book ai didi

java - ListView - 项目选择随着滚动而消失

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:40 26 4
gpt4 key购买 nike

我是新手。几天来我试图解决这个问题,但没有成功。有什么帮助吗?提前致谢。

下面是图片, I selected few items and then scrolled up Once scrolled down selection disappears

在我实现的适配器类下面,

/**
* Created by abcd on 27/1/17.
*/

public class ListItemAdapterTaxCheckBox extends BaseAdapter {

//flag = 1 name/id
//flag =2 //ID/Name
public class HolderCheck
{
TextView tv;
CheckBox ck;
}

private static LayoutInflater inflater=null;

Tax[] result;
Context context;
Tax[] selections = null;
String [] IDs = null;
boolean bAllSel = false;

Resources res = null;

public ListItemAdapterTaxCheckBox(Activity mainActivity, Tax[] prgmNameList, Tax[] sel, Resources rs, String [] ids) {

result=prgmNameList;
context=mainActivity;
selections = sel;


if(selections==null)
{
selections = new Tax[1];
selections[0] = new Tax();
}

IDs = ids;

res = rs;

inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return result.length + 1;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final HolderCheck holderCk = new HolderCheck();;
View rowView = convertView;
final int pos = position;

rowView = inflater.inflate(R.layout.custom_list_check_box, null);
holderCk.tv = (TextView) rowView.findViewById(R.id.code);
holderCk.ck = (CheckBox) rowView.findViewById(R.id.checkBox1);

if(position==0) {
holderCk.tv.setText(context.getString(R.string.all_text));
}
else {

holderCk.tv.setText("" + result[position-1].m_TaxID + " - " + result[position-1].m_TaxName);
}

holderCk.tv.setTextSize(16);
holderCk.tv.setTextColor(Color.BLACK);

rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (holderCk.ck.isChecked()) {
holderCk.tv.setBackgroundColor(Color.WHITE);
holderCk.ck.setChecked(false);

if(pos!=0)
{
if (IDs != null)
IDs[pos -1] = "0";
}
else
{
bAllSel = false;
}

} else {

holderCk.tv.setBackgroundColor(GetResourceColor.getColorWrapper(context, R.color.selHintColor));
holderCk.ck.setChecked(true);

if(pos!=0)
{
if (IDs != null)
IDs[pos -1] = "" + result[pos-1].m_TaxID;
}
else
{
bAllSel = true;
}
}
}
});

return rowView;
}

public String [] getIDs() {

if(bAllSel)
return null;
else {
ArrayList<String> arrayList = new ArrayList<String>();

for (int i = 0, j = 0; i < IDs.length; i++) {
if (IDs[i].trim() != "0") {

arrayList.add(j, IDs[i].trim());

j = j + 1;

if (arrayList.size() == MySQLHelper.MAXITEMSLISTDELETE)
break;
}
}

return arrayList.toArray(new String[arrayList.size()]);
}
}
}

在我填充 ListView 的方式下方

//class member

Tax[] valuesTaxID = null;


//inside a method

//default
Tax[] tx = new Tax[1];
tx[0] = new Tax();

if (valuesTaxID != null) {
listV.setAdapter(null);
listV.setAdapter(new ListItemAdapterTaxCheckBox(ctx, valuesTaxID, tx, rs, Ids));
listV.setVisibility(View.VISIBLE);

最佳答案

当您滚动时,ListView 将创建和销毁 View 。您的代码在单击 View 时更改复选框,但您将该信息存储在其他地方,您滚动,ListView 在您滚动时破坏 View ,并且您丢失了 View 的状态。

您想将 View 的“已检查”状态存储在某处。可能是一个带有状态的 ArrayList,可能是附加到 ViewHolders 的标签,...只需将其存储在某个地方即可。

关于java - ListView - 项目选择随着滚动而消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com