自定义列表视图每10个项目重复一次

时间:2014-04-18 07:54:24

标签: android listview adapter

我面临着针对listview的自定义适配器的奇怪问题 我的适配器每隔10条记录重复项目的ID。

我非常感谢任何帮助和/或想法

我的ListActivity

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_backuplist); // Get main display layout
            lv = (ListView) findViewById(android.R.id.list);
            lists = new ArrayList<BackupListView>();

            int j = 50;
            for (int i = 0; i < 15; i++)
            {
        String fileName = ((i % 2) == 0) ? "sample.csv" : "sample.vcf";
        String fileExt  = ((i % 2) == 0) ? "csv" : "vcf";
        lists.add(new BackupListView(fileName + "\ndate 2014/05/20", j, fileExt, "210"));

        j++;
    }

    int x = lists.size();
    BackupListView[] toArray = new BackupListView[x];
    toArray = lists.toArray(toArray);

    adapter = new BackupListAdapter(this, R.layout.custom_backup_listview_rows, toArray);
    lv.setAdapter(adapter);
    lv.setClickable(true);
    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
        {
            dspDialog();
        }
    });
}

这是我的自定义适配器

public class BackupListAdapter extends ArrayAdapter<BackupListView>
    {
    private Context context;
    private int resID;
    BackupListView[] data = null;
    private ArrayList<String> checkedItem = new ArrayList<String>();

    public BackupListAdapter(Context context, int resource, BackupListView[] objects)
    {
    super(context, resource, objects);
    this.context = context;
    this.resID = resource;
    this.data = objects;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    CustomHolder holder = null;

    if (convertView == null)
    {
        LayoutInflater inflate = ((Activity) context).getLayoutInflater();
        convertView = inflate.inflate(resID, null);

        holder = new CustomHolder();
        holder.cb = (CheckBox) convertView.findViewById(R.id.custom_checkbox);
        holder.im = (ImageView) convertView.findViewById(R.id.img_file_ext_icon);
        holder.tw = (TextView) convertView.findViewById(R.id.custom_textView);
        holder.bt = (ImageView) convertView.findViewById(R.id.custom_button);

        final BackupListView lists = data[position];
        holder.tw.setText(lists.getText() + "  " + lists.getFileSize() + "kb");
        holder.id = lists.getId();

        Log.i("TEST ", "List getId " + lists.getId());

        // Check box event
        holder.cb.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (((CompoundButton) v).isChecked())
                {
                    checkedItem.add("" + lists.getId());
                }
                else
                {
                    checkedItem.remove("" + lists.getId());
                }
            }
        });

        //  Set CSV file extension icon
        if(lists.getExt().equals("csv"))
            holder.im.setBackgroundResource(R.drawable.icon_csv);
        else
            holder.im.setBackgroundResource(R.drawable.icon_vcf);

        // Button event
        holder.bt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final View inflater;
                inflater = ((Activity) context).getLayoutInflater().inflate(
                        R.layout.dialog_filename_change, null);

                AlertDialog.Builder dialog = new AlertDialog.Builder(
                        context.getApplicationContext());
                dialog.setTitle("Enter Pass");
                dialog.setView(inflater);
                dialog.setInverseBackgroundForced(true);
                dialog.setNegativeButton("Cancel", null);
                dialog.setPositiveButton("OK", new OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Intent i = new Intent(context.getApplicationContext(),
                                PhoneBookDetail.class);
                        i.putExtra("id", lists.getId());
                        context.startActivity(i);
                    }
                });
                dialog.create();

                Log.i("TEST", " Clicked " + lists.getId());

                //dialog.show();
            }
        });

        convertView.setTag(holder);
    }
    else
    {
        holder = (CustomHolder) convertView.getTag();
    }

    return convertView;
}

public class CustomHolder
{
    CheckBox    cb;
    ImageView   im;
    TextView    tw;
    ImageView   bt;
    int         id;
}

}

结果是

04-18 16:31:12.840: I/TEST(21986): List getId 50
04-18 16:31:12.856: I/TEST(21986): List getId 51
04-18 16:31:12.863: I/TEST(21986): List getId 52
04-18 16:31:12.871: I/TEST(21986): List getId 53
04-18 16:31:12.879: I/TEST(21986): List getId 54
04-18 16:31:12.887: I/TEST(21986): List getId 55
04-18 16:31:12.895: I/TEST(21986): List getId 56
04-18 16:31:12.903: I/TEST(21986): List getId 57
04-18 16:31:12.910: I/TEST(21986): List getId 58
04-18 16:31:14.535: I/TEST(21986):  Clicked 55
04-18 16:31:15.840: I/TEST(21986):  Clicked 54
04-18 16:31:16.871: I/TEST(21986):  Clicked 53
04-18 16:31:17.238: I/TEST(21986):  Clicked 52
04-18 16:31:17.910: I/TEST(21986):  Clicked 53
04-18 16:31:18.309: I/TEST(21986):  Clicked 54
04-18 16:31:20.121: I/TEST(21986):  Clicked 56
04-18 16:31:20.645: I/TEST(21986):  Clicked 56
04-18 16:31:21.442: I/TEST(21986):  Clicked 57
04-18 16:31:23.067: I/TEST(21986):  Clicked 58
04-18 16:31:24.785: I/TEST(21986):  Clicked 53
04-18 16:31:26.481: I/TEST(21986):  Clicked 50
04-18 16:31:27.903: I/TEST(21986):  Clicked 58
04-18 16:31:28.965: I/TEST(21986): List getId 59
04-18 16:31:30.090: I/TEST(21986):  Clicked 59
04-18 16:31:33.520: I/TEST(21986):  Clicked 59
04-18 16:31:35.559: I/TEST(21986):  Clicked 50

2 个答案:

答案 0 :(得分:0)

初始化数组:

Map<Integer,boolean> checkBoxMap = new Map<Integer,boolean>();

在getView方法中,应首先实现以下内容。

    if (convertView == null)
        {
            LayoutInflater inflate = ((Activity) context).getLayoutInflater();
            convertView = inflate.inflate(resID, null);

            holder = new CustomHolder();
            holder.cb = (CheckBox) convertView.findViewById(R.id.custom_checkbox);
            holder.im = (ImageView) convertView.findViewById(R.id.img_file_ext_icon);
            holder.tw = (TextView) convertView.findViewById(R.id.custom_textView);
            holder.bt = (ImageView) convertView.findViewById(R.id.custom_button);  convertView.setTag(holder); 
    } else {
     holder = (CustomHolder) convertView.getTag();  
    }
// Check box event
        holder.cb.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                checkBoxMap.put(position,((CompoundButton) v).isChecked())
            }
        });
holder.cb.setChecked(checkBoxMap.get(position));

剩下的所有监听器和setText都应该在上面的代码之后。

我的意思是以下所有内容都应遵循以上代码。

final BackupListView lists = data[position];
        holder.tw.setText(lists.getText() + "  " + lists.getFileSize() + "kb");
        holder.id = lists.getId();

        Log.i("TEST ", "List getId " + lists.getId());

        // Check box event
        holder.cb.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (((CompoundButton) v).isChecked())
                {
                    checkedItem.add("" + lists.getId());
                }
                else
                {
                    checkedItem.remove("" + lists.getId());
                }
            }
        });

        //  Set CSV file extension icon
        if(lists.getExt().equals("csv"))
            holder.im.setBackgroundResource(R.drawable.icon_csv);
        else
            holder.im.setBackgroundResource(R.drawable.icon_vcf);

        // Button event
        holder.bt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final View inflater;
                inflater = ((Activity) context).getLayoutInflater().inflate(
                        R.layout.dialog_filename_change, null);

                AlertDialog.Builder dialog = new AlertDialog.Builder(
                        context.getApplicationContext());
                dialog.setTitle("Enter Pass");
                dialog.setView(inflater);
                dialog.setInverseBackgroundForced(true);
                dialog.setNegativeButton("Cancel", null);
                dialog.setPositiveButton("OK", new OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Intent i = new Intent(context.getApplicationContext(),
                                PhoneBookDetail.class);
                        i.putExtra("id", lists.getId());
                        context.startActivity(i);
                    }
                });
                dialog.create();

                Log.i("TEST", " Clicked " + lists.getId());

                //dialog.show();
            }
        });

答案 1 :(得分:0)

列出变量是最终的,并在点击指向特定位置时使用..当视图被回收时..列表保持不变,因此你得到重复