将Custome ListView与TextView和CheckBox一起使用,并选择CheckBox [英] use Custome ListView with TextView And CheckBox With Single Selection of CheckBox

查看:54
本文介绍了将Custome ListView与TextView和CheckBox一起使用,并选择CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听到我将自定义ListViewTextViewCheckBox一起使用. 但是我想一次在复选框中选择 选中一个CheckBox,然后取消选中另一个 使用BaseAdapter 但是此代码无法正常工作.. 请给我建议..thnks

Hear i use Custom ListView with TextView and CheckBox. But i want that Single Selection in CheckBox At a time One CheckBox Selected then other one is Deselect using BaseAdapter but This code not Work Properly .. Please Give me Suggestion ..thnks

@Override
public View getView(final int position, View view, ViewGroup parent) {
    Integer selected_position = -1;
    holder = new ViewHolder();
    final Items itm = rowItem.get(position);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (view == null) {

        view = layoutInflater.inflate(R.layout.activity_custom_list,
                parent, false);

        holder.tvItemName = (TextView) view.findViewById(R.id.textView1);
        holder.check = (CheckBox) view.findViewById(R.id.checkBox1);
        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.tvItemName.setText(itm.getItems());

    if (position == selected_position)
        holder.check.setChecked(true);
    else
        holder.check.setChecked(false);

    holder.check.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (holder.check.isChecked()) {
                selected_position = position;
            } else {
                selected_position = -1;
            }
            notifyDataSetChanged();

        }
    });

    return view;
}}

推荐答案

将自定义列表视图与文本视图和复选框一起使用,只需选中复选框即可 如此多的尝试终于使我得到了解决方案,我希望它对大家都有用. 此代码可帮助您创建带有文本视图和复选框的自定义列表视图,然后选择一个复选框,如果您选择另一个复选框,则应自动取消选择第一个.....谢谢...

use Custom List-View with Text-View And Check-Box With Single Selection of Check Box So many try then finally i got the Solution I hope it is Useful Code to you All... This code Help you to create custom List-view with Text-view and Check-box then you select one check-box and if you select another one the first should automatically be Deselected.....Thank You...

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.listviewdemo2.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

activity_custom_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.listviewdemo2.CustomListActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="4dp"
        android:layout_weight="0.39"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="4dp"/>
</LinearLayout> </LinearLayout>

String.xml

<string-array name="name">
   <item>Laptop</item>
    <item>Mobile</item>
    <item>Desktop</item>
    <item>TV</item>
    <item>Pendrive</item>
    <item>Router</item>
    <item>Notebook</item>
    <item>Tablet</item>
    <item>I-pad</item>
    <item>Bluetooth</item>
    <item>HomeTheator</item>
</string-array>

MainActivity.java

String[] ItemName;
List<Items> rowItem;
ListView list;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rowItem = new ArrayList<Items>();
    ItemName = getResources().getStringArray(R.array.name);

    for(int i = 0 ; i < ItemName.length ; i++)
    {
        Items itm = new Items(ItemName[i]);
        rowItem.add(itm);
    }

    list = (ListView) findViewById(R.id.listView1);
    CustomListActivity adapter = new CustomListActivity(this, rowItem);
    list.setAdapter(adapter);
}

Items.java

公共课程项目{

private String items;
 private boolean selected;


public Items(String items) {

    this.items = items;

}

public String getItems() {

    return items;
}

public void setItemName(String name) {

    this.items = name;
}
public boolean getSelected() {
    return selected;
}

public boolean setSelected(Boolean selected) {
    return this.selected = selected;
}}

CustomListActivity.java

public class CustomListActivity extends BaseAdapter {

Context context;
List<Items> rowItem;
View listView;
boolean checkState[];

ViewHolder holder;

public CustomListActivity(Context context, List<Items> rowItem) {

    this.context = context;
    this.rowItem = rowItem;
    checkState = new boolean[rowItem.size()];

}

@Override
public int getCount() {

    return rowItem.size();
}

@Override
public Object getItem(int position) {

    return rowItem.get(position);

}

@Override
public long getItemId(int position) {

    return rowItem.indexOf(getItem(position));

}

public class ViewHolder {
    TextView tvItemName;
    CheckBox check;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {

    holder = new ViewHolder();
    final Items itm = rowItem.get(position);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (view == null) {

        listView = new View(context);
        listView = layoutInflater.inflate(R.layout.activity_custom_list,
                parent, false);

        holder.tvItemName = (TextView) listView
                .findViewById(R.id.textView1);
        holder.check = (CheckBox) listView.findViewById(R.id.checkBox1);
        listView.setTag(holder);

    } else {
        listView = (View) view;
        holder = (ViewHolder) listView.getTag();
    }

    holder.tvItemName.setText(itm.getItems());

    holder.check.setChecked(checkState[position]);

    holder.check.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            for(int i=0;i<checkState.length;i++)
            {
                if(i==position)
                {
                    checkState[i]=true;
                }
                else
                {
                    checkState[i]=false;
                }
            }
            notifyDataSetChanged();

        }
    });
    return listView;
}}

显示输出:-

这篇关于将Custome ListView与TextView和CheckBox一起使用,并选择CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆