Peter Peter - 1 year ago 188
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++){

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;


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(;
tvC = (TextView) rowView.findViewById(;
tvCt.setText(""+s.get(position) + "/3");

return rowView;


regards , Peter.


in adapter check

rowView= inflater.inflate(R.layout.listviewitemdisabled, null, true);


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

and when click on

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

thanks for this easy solution

Answer Source

You can set enabled state in your adapter.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download