Peter Peter - 5 months ago 63
Java Question

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

Answer

You can set enabled state in your adapter.

rowView.setEnabled(false)