简体   繁体   English

在列表视图中禁用项目

[英]disable item in listview android java

Im trying disable some items in listview , but cant to do it. 我正在尝试禁用listview中的某些项目,但无法做到这一点。 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 但是如果像这样写,我总是在字符串“ listview.getChildAt()”上得到nullpointerexception

if(listview.getChildAt(i)!=null){ //do code here } if(listview.getChildAt(i)!= null){//在此处进行编码}

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. 比我看到字符串“ getChildAt(i).setEnabled(false)”没有任何入口,我对getChildAt一点也不了解,但是我在想它可以按位置获取项目的方式。 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 谢谢你这个简单的解决方案

您可以在适配器中设置启用状态。

rowView.setEnabled(false)

Use Adapter approach. 使用适配器方法。 Create an adapter and a viewHolder and in OnBind method just get that item of list and disable it. 创建一个适配器和一个viewHolder,然后在OnBind方法中获取列表项并禁用它。 send value to the adapter using method and notify the adapter about change. 使用方法将值发送给适配器,并通知适配器有关更改的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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