English 中文(简体)
Android 选择错误的复选框吗?
原标题:Wrong Checkbox selection in Android?

在我的应用程序中, 我将选择要排序的项目 。 我的问题是如果我检查复选框来排序其它的复选框, 也已经检查过 。 意思是如果我选择了单项, 两个项目正在检查中 。

从 5 项开始, 它被正确检查, 并在顺序页面中显示。 从 6 项 中, 如果我选择它, 它会在顺序页面中显示其它项目 。

这是我的名单代码:

    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder viewHolder;
        if (view == null) {         
              view = inflater.inflate(R.layout.appetiserlistview, null);
            viewHolder = new ViewHolder();
            viewHolder.image=(ImageView)view.findViewById(R.id.appetiserimage);
            viewHolder.text = (TextView) view.findViewById(R.id.appetisertext);
            viewHolder.desc=(TextView)view.findViewById(R.id.appetiserdesc);
            viewHolder.price=(TextView)view.findViewById(R.id.appetiserprice);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.bcheck);


            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }






        viewHolder.image.setImageBitmap(bmps[position]);

        viewHolder.price.setText(prices[position]);
        viewHolder.desc.setText(descs[position]);
        viewHolder.checkbox.setTag(itemnames[position]);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(itemnames[position]);
        viewHolder.checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (((CheckBox) v).isChecked())
                {
                    arr.add(itemnames[position]);
                    itemprice.add(prices[position]);
                       image.add(bmps[position]);


                }
问题回答

您必须保存您所有复选框的状态, 并相应恢复这些状态 。 这是我已贴过的一个例子 。 如果您遇到任何问题, 请告诉我 。 我将尽力帮助您 。

检查此链接

< a href=> https://stackoverflow.com/ questions/10190083/ how-to-taminment- a- button- thatt-gets-all-checkboxs-state-and-adds-value-f-c/101369#101969_101369> 如何执行一个按钮, 该按钮将所有复选框状态都接收到状态, 并将检查过的项目添加到数组列表中?

Its a basic property of ListView not only checkboxes but also Edittext and other input controls will have the same value as the one have what actually happen in list view

正在显示的项目仅被创建。

When you scroll them new items are created so as per previous display state the one of check box get checked for current display. Solution 1: Create a list in which store the states of all checkboxes(or list items) and at calling of bindview() method check the status of check box and according to the list check and uncheck them also create checkbox listener so that oncheckchange event change the state of checkbox(or listitem) in list so this way you can get the solution

<强力 > 解决方案2:

如果您只需要一个复选框,您就可以使用列表视图属性来选择多个项目

<强>编辑1:

设置此属性

android:choiceMode="multipleChoice"

用于您的 2

Follow the Link and also See this Property

根据列表视图的行为, 它不会保存项目状态, 因为它们在滚动时间上插入了滚动时间, 所以您应该使用一系列布林值来保持复选框的状态,

public class FriendListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private boolean isCheck[] = new boolean[Data.fbFriendList.size()];
 // Data.fbFriendList is arraylist contains data
    public FriendListAdapter() {

        mInflater = LayoutInflater.from(mContext);
        Arrays.fill(isCheck, Boolean.FALSE);
    }

    @Override
    public int getCount() {
        return Data.fbFriendList.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View hView = convertView;
        if (convertView == null) {
            hView = mInflater.inflate(R.layout.friend_item, null);
            ViewHolder holder = new ViewHolder();
            holder.profile_pic = (ImageView) hView
                    .findViewById(R.id.profile_pic);
            holder.name = (TextView) hView.findViewById(R.id.name);
            holder.checkBox = (CheckBox) hView.findViewById(R.id.checkBox);
            hView.setTag(holder);
        }

        final ViewHolder holder = (ViewHolder) hView.getTag();

        if (isCheck[position]) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }

        try {
            holder.profile_pic.setImageBitmap(Utility.model.getImage(
                    Data.fbFriendList.get(position).getFbFriendFBid(),
                    Data.fbFriendList.get(position)
                            .getFbFriendThumbImgUrl()));
            holder.name.setText(Data.fbFriendList.get(position)
                    .getFbFriendName());

        } catch (Exception e) {
            e.printStackTrace();
            holder.name.setText("");
        }

        holder.checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.checkBox.isChecked())
                    isCheck[position] = true;
                else
                    isCheck[position] = false;
            }
        });

        return hView;
    }

}

class ViewHolder {
    ImageView profile_pic;
    TextView name;
    CheckBox checkBox;
}

这里选中是一个用于适应器类中的布尔数组, 用于持续状态的复选框和 Data. fbFrienddList 是一个数组列表 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签