Peter
Peter

Reputation: 2500

disable item in listview android java

Im trying disable some items in listview , but cant to do it. I have Array of booleans

private boolean[] array; //10 items all false, and some of them true

in code im trying

for(int i=0;i<array.length();i++){
 if(!array[i]){
   listview.getChildAt(i).setEnabled(false);
 }
}

but im always got nullpointerexception on string "listview.getChildAt()" if write like

if(listview.getChildAt(i)!=null){ //do code here }

than i see what no entrance to string "getChildAt(i).setEnabled(false)" im little not understand about getChildAt but i was thinking its way where i can get items by position. Any one can help me how to do it? adapter for list view

public class LevelAdapter extends ArrayAdapter<String> {

      public LevelAdapter(Activity context, ArrayList<String> le, ArrayList<Integer> co, boolean[] bools) {
        super(context, R.layout.listviewitem, le);
        this.context = context;
        this.l = le;
        this.s = co;
        this.boolStates = bools;


    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.listviewitem, null, true);

        tvL = (TextView) rowView.findViewById(R.id.l);
        tvC = (TextView) rowView.findViewById(R.id.s);
        tvL.setText(""+l.get(position));
        tvCt.setText(""+s.get(position) + "/3");



        return rowView;
    }

}

regards , Peter.

SOLUTION

in adapter check

if(lvl[position]==false){
            rowView= inflater.inflate(R.layout.listviewitemdisabled, null, true);

            rowView.setEnabled(false);

        }else
        {
            rowView= inflater.inflate(R.layout.listviewitem, null, true);
        }

and when click on

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (view.isEnabled()) {
                     // do our code here

thanks for this easy solution

Upvotes: 2

Views: 2245

Answers (2)

Jakoss
Jakoss

Reputation: 5275

You can set enabled state in your adapter.

rowView.setEnabled(false)

Upvotes: 2

Uma Shankar Pathak
Uma Shankar Pathak

Reputation: 54

Use Adapter approach. Create an adapter and a viewHolder and in OnBind method just get that item of list and disable it. send value to the adapter using method and notify the adapter about change.

Upvotes: 0

Related Questions