ListView中的按钮每个项目布局

时间:2014-01-10 14:21:28

标签: java android listview

我有一个listview,我从自定义对象的ArrayList填充该列表视图。 这是自定义适配器代码。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final int a=position;


        FileHolder holder=null;
        View row = convertView;
        if(row==null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(textViewResourceId, parent, false);
            holder=new FileHolder();

            holder.file=files.get(position);
            holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
            holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);

            holder.deleteButton.setTag(holder.file);
            holder.downloadButton.setTag(holder.file);


            holder.fileName= (TextView) row.findViewById(R.id.fileName);



            holder.fileName.setText(holder.file.getFileName());

        }
        else
        {
            holder=(FileHolder) row.getTag();
        }
        fPOJO=holder.file;
        final AsyncCall call=new AsyncCall();

        holder.downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                MODE=0;
                //call.execute(MODE);

            }
        });

        holder.deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+"  "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
                MODE=1;
                //call.execute(MODE);

            }
        });


        return row;
    }

    public static class FileHolder{
    FilesPOJO file;
    TextView fileName;
    ImageButton downloadButton;
    ImageButton deleteButton;
}

有两个按钮用于文件删除,另一个用于文件下载。我已经为这两个按钮实现了单击侦听器。问题是,当我单击任何按钮时,Toast消息中的fileName与我在屏幕显示上看到的文件名不同。例如:我有5个名字

的文件
delete.png
upload.png
share.png
referesh.png
copy.png

现在当我点击对应于referesh.png文件的删除按钮时,toast弹出窗口并且文件名与referesh.png不同。

如何解决此问题。

编辑:

这是我设置此适配器的方式

             FilesAdapter adapter=new FilesAdapter(rootView.getContext(), R.layout.file_list_view_item_row, Application.files);
            View header = (View) View.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
            fileListHeading.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
            filesListView.addHeaderView(header);
            filesListView.setAdapter(adapter);

2 个答案:

答案 0 :(得分:1)

我认为问题出在fPOJO变量时调用onClick它可能有不同的值,因为在每个getView之后你都会改变它的值。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final int a=position;


    final FileHolder holder;
    View row = convertView;
    if(row==null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(textViewResourceId, parent, false);
        holder=new FileHolder();

        holder.file=files.get(position);
        holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
        holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);

        holder.deleteButton.setTag(holder.file);
        holder.downloadButton.setTag(holder.file);


        holder.fileName= (TextView) row.findViewById(R.id.fileName);



        holder.fileName.setText(holder.file.getFileName());

    }
    else
    {
        holder=(FileHolder) row.getTag();
    }

    final AsyncCall call=new AsyncCall();

    holder.downloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            MODE=0;
            //call.execute(MODE);

        }
    });

    holder.deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+"  "+holder.fileName, Toast.LENGTH_LONG).show();
            MODE=1;
            //call.execute(MODE);

        }
    });


    return row;
}

只需将holder设为最终版,然后从中获取文件名。

答案 1 :(得分:0)

您将列表项存储在holder中,这是您的问题,ListView适配器根据需要创建尽可能多的convertViews以填充屏幕,因此如果您有10个以上元素的列表,则只会将少数元素添加到行中。

首先,不要将您的列表元素存储在holder中,您提供了名为getItem(positoin)的适配器方法来获取它。

在持有者视图商店中,只对布局视图(TextView,Buttons等)的引用和(if(row == null)对它们和setTag调用findViewById。

然后在if语句之外设置文本,监听器,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final int a=position;


    FileHolder holder=null;
    View row = convertView;
    if(row==null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(textViewResourceId, parent, false);
        holder=new FileHolder();

        holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
        holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);

        holder.fileName= (TextView) row.findViewById(R.id.fileName);

        row.setTag(holder);

    }
    else
    {
        holder=(FileHolder) row.getTag();
    }

    fPOJO= getItem(position);

    final AsyncCall call=new AsyncCall();

    holder.downloadButton.setOnClickListener(null);
    holder.downloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            MODE=0;
            //call.execute(MODE);

        }
    });

    holder.deleteButton.setOnClickListener(null);
    holder.deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+"  "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
            MODE=1;
            //call.execute(MODE);

        }
    });


    return row;
}

public static class FileHolder{
    TextView fileName;
    ImageButton downloadButton;
    ImageButton deleteButton;
}

您也可以进行一些代码优化,例如,不要在getView()方法中初始化LayoutInflater,您可以在适配器Constructor

中执行此操作