简体   繁体   中英

Using a custom layout with CheckedTextView for a ListView

To populate my ListView , I use an ArrayAdapter<String> which is declared like this : myListAdapter =new ArrayAdapter<String>(getActivity(),R.layout.myView,R.id.myTextView); . The xml file representing my view layout is :

<CheckedTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/DescriptionEtape"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/textCheckMark"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:checked="false"/>

To react to user's clicks, I have added : (ListView)FragmentView.findViewById(R.id.myListView)).setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View vue, int position, long id) { ((CheckedTextView)vue.findViewById(R.id.myTextView)).setChecked(!((CheckedTextView)vue.findViewById(R.id.myTextView)).isChecked()); } });
The onItemClick function is correctly executed when I touch an entry of the ListView and the corresponding entry is correctly checked. Unfortunately, another entry, which is not visible at the moment of the click, is checked as well!!!

What is wrong with my code?

According to Kintanpatel answer, with a CheckedTextView already embedded in a LinearLayout , I tried to use a custom ArrayAdapter<String> , with a getView function looking like this :

    public View getView(int position,View container,ViewGroup parent){
    View view;

    view= super.getView(position,container,parent);
    ((CheckedTextView)vue.findViewById(R.id.myTextView)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((CheckedTextView)v).setChecked(!((CheckedTextView)v).isChecked());
        }
    });

    return(view);
}

And I still have the same problem...

It is better to use BaseAdapter insted of ArrayAdapter. you only need to create Simple class and exteds with BaseAdapter,also create custom cell in layout and last apply this adapter to ListvView. You can perform all functionality with custom cell. below i create sample for You.

lay_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
</LinearLayout>

MyAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;

/**
 * Created by sai on 14-Jul-16.
 */
public class MyAdapter extends BaseAdapter {
    private Context mContext;

    public MyAdapter(Context mContext) {
        this.mContext = mContext;

    }

    @Override
    public int getCount() {
        return 10;//return how many cell you want to create
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.lay_cell, parent, false);
        final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkBox.setChecked(true);
            }
        });
        return convertView;
    }
}

and don't forget to apply this adpter to ListView,

listview.setAdapter(new MyAdapter(context));

In fact, after having looked at the xml file describing the simple_list_item_checked , I noticed that there was no LinearLayout . I suppressed it from my own xml file and everything is now fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM