如何禁用 ListView 中的先前项目

How to disable previous items in a ListView

我正在尝试开发一个 activity,其中有一个由 CustomAdapter 组成的自定义 listView

列表由 TextViewEditText 组成。 EditTextclicked时,它会自动获取系统时间。

我想要的是当一个特定的EditText被填充时,我希望序列中的所有先前(以上)列表项被禁用。

到目前为止,我已经尝试使用 isEnabled()areAllItemsEnabled() 函数使用位置返回相应的布尔值,但是没有用。

请帮助我实现上述目标。

谢谢。

这是我的自定义适配器Class

public class SelectStnListByRoute extends BaseAdapter implements View.OnClickListener {
    Context context;
    ArrayList<StnNames> stnList;
    LayoutInflater layoutInflater = null;
    ViewHolder viewHolder;
    private int mLastClicked;

    public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
        super();
        this.context = context;
        this.stnList = stnList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return stnList.size();
    }

    @Override
    public Object getItem(int position) {
        return stnList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return stnList.indexOf(getItem(position));
    }

    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if(position==position){
            return false;
        }
        return false;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        StnNames stnDetails = stnList.get(position);
        viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
            viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
            viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
            viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
            convertView.setTag(viewHolder);
            viewHolder.txtStnArr.setTag(stnDetails);
            viewHolder.txtStnDep.setTag(stnDetails);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.txtStnArr.setTag(stnDetails);
            viewHolder.txtStnDep.setTag(stnDetails);
        }
        viewHolder.txtStnNAme.setText(stnDetails.getStnCode());

          viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: " , String.valueOf(position)); //Here I am getting the position of the row item clicked, where should I put the Onclick false for disabling all of the above fields using the position?
        }
    });
        viewHolder.txtStnDep.setOnClickListener(this);
        viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
        if (stnDetails.getArrivalTime() != null) {
            viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
        } else {
            viewHolder.txtStnArr.setText("");
        }


        if (stnDetails.getDeptTime() != null) {
            viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
        } else {
            viewHolder.txtStnDep.setText("");
        } 
        return convertView;
    }

    class ViewHolder {
        TextView txtStnNAme, txtStnArr, txtStnDep;
        int ref;
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch (id) {
            case txtArrivalTime:
                TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);
                textViewArrVal.setClickable(false);
                StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
                if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
                    listItemsArrr.setArrivalTime(getCurrentTime());
                    if (listItemsArrr.getArrivalTime() != null) {
                        int position = textViewArrVal.getSelectionStart();
                        textViewArrVal.setText(listItemsArrr.getArrivalTime());

                    } else {
                        textViewArrVal.setText("");
                    }
                }
                break;
            case txtDepTime:
                TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
                StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
                if (listItemsDepp.getDeptTime() != getCurrentTime()) {
                    listItemsDepp.setDeptTime(getCurrentTime());
                    if (listItemsDepp.getDeptTime() != null) {
                        textViewDepVal.setText(listItemsDepp.getDeptTime());
                    } else {
                        textViewDepVal.setText("");
                    }
                }
                break;
            default:
                break;
        }

    }
    public String getCurrentTime(){

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
        String arrDate = mdformat.format(calendar.getTime());
        return arrDate;
    }

}

获取被点击行的位置,然后为小于点击位置的位置设置onclick false

如下:

viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: " , String.valueOf(position)); 
            for (int i = 0; i < position; i++) {
                  viewHolder.txtStnArr.setEnable(false);
             }
             notifyDataSetChanged();
        }
    });

你可以按照下面的方法来做-:

您需要存储点击按钮的位置。所以在你的 class

中初始化一个变量
int mButtonSelected = -1;

编辑 1.

然后更改您的 isEnabled 方法

 @Override
public boolean isEnabled(int position) {
    if(position<mButtonSelected){
        return false;
    }
    return true;
}

如果单击任何其他按钮,它将起作用。但你必须在 onClick

中这样做
mButtonSelected = position;
notifyDataSetChanged();

管用不管用

编辑

请参阅以下代码更改-:

public class SelectStnListByRoute extends BaseAdapter {
Context context;
ArrayList<StnNames> stnList;
LayoutInflater layoutInflater = null;
ViewHolder viewHolder;
private int mLastClicked;
private SQLiteDB sqLiteDB;
int mArrivalSelected = -1;
int mDepartSelected = -1;

public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
    super();
    this.context = context;
    this.stnList = stnList;
    layoutInflater = LayoutInflater.from(context);
    sqLiteDB = new SQLiteDB(context);
}

@Override
public int getCount() {
    return stnList.size();
}

@Override
public Object getItem(int position) {
    return stnList.get(position);
}

@Override
public long getItemId(int position) {
    return stnList.indexOf(getItem(position));
}

public int getViewTypeCount() {
    return 1;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    if (position <= mArrivalSelected) {
        return false;
    }
    return true;
}

public boolean isEnabledd(int position) {
    if (position <= mDepartSelected) {
        return false;
    }
    return true;
}

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    int type = getItemViewType(position);
    StnNames stnDetails = stnList.get(position);
    viewHolder = new ViewHolder();
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
        viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
        viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
        viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
        convertView.setTag(viewHolder);
        viewHolder.txtStnArr.setTag(stnDetails);
        viewHolder.txtStnDep.setTag(stnDetails);


    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.txtStnArr.setTag(stnDetails);
        viewHolder.txtStnDep.setTag(stnDetails);

    }


    viewHolder.txtStnNAme.setText(stnDetails.getStnCode());
    if (!isEnabled(position)) {
        if (position <= mArrivalSelected) {
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnArr.setEnabled(false);
            if (position < mArrivalSelected) {
                viewHolder.txtStnDep.setEnabled(false);
                viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
            }
        }
    } else {
        viewHolder.txtStnArr.setEnabled(true);
        viewHolder.txtStnDep.setEnabled(true);
        viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
        viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
    }
    viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: ", String.valueOf(position));
            mArrivalSelected = position;
            arrivalClick(view);
            notifyDataSetChanged();
        }
    });
    if (!isEnabledd(position)) {
        if (position <= mDepartSelected) {
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
            viewHolder.txtStnArr.setEnabled(false);
            viewHolder.txtStnDep.setEnabled(false);
        } else {
            viewHolder.txtStnArr.setEnabled(true);
            viewHolder.txtStnDep.setEnabled(true);
            viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
            viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
        }
    }
    viewHolder.txtStnDep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("Position: ", String.valueOf(position));
            mDepartSelected = position;
            departureClick(view);
            notifyDataSetChanged();
        }
    });
    viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
    if (stnDetails.getArrivalTime() != null) {
        viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
    } else {
        viewHolder.txtStnArr.setText("");
    }


    if (stnDetails.getDeptTime() != null) {
        viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
    } else {
        viewHolder.txtStnDep.setText("");
    }

    return convertView;
}

class ViewHolder {
    TextView txtStnNAme, txtStnArr, txtStnDep;
    StnNames pos;
    int ref;
}

public void arrivalClick(View view) {
    TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);

    StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
    if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
        listItemsArrr.setArrivalTime(getCurrentTime());
        int stnId = listItemsArrr.getStnId();
        String arrClick = "arrival";
        String upSideKm = listItemsArrr.getStnUpsideKm();
        String downsideKm = listItemsArrr.getStnDownSideKm();
        String arrTime = getCurrentTime();
           /*     sqLiteDB.open();
               *//* long abc = sqLiteDB.insertJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick);*//*
               *//* long abcd = sqLiteDB.updateJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick,downsideKm,upSideKm);
                Log.e("arrclick",String.valueOf(abcd));*//*
                sqLiteDB.close();*/
        if (listItemsArrr.getArrivalTime() != null) {
            int position = textViewArrVal.getSelectionStart();
            textViewArrVal.setText(listItemsArrr.getArrivalTime());

        } else {
            textViewArrVal.setText("");
        }
    }
}

public void departureClick(View view) {
    TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
    StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
    if (listItemsDepp.getDeptTime() != getCurrentTime()) {
        listItemsDepp.setDeptTime(getCurrentTime());
        String depTime = getCurrentTime();
        int stnId = listItemsDepp.getStnId();
        String depClick = "departure";
        String upSideKm = listItemsDepp.getStnUpsideKm();
        String downsideKm = listItemsDepp.getStnDownSideKm();
        sqLiteDB.open();
                /*long abc = sqLiteDB.insertJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick);*/
                /*long abcd = sqLiteDB.updateJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick,downsideKm,upSideKm);
                Log.e("depclick",String.valueOf(abcd));*/
        sqLiteDB.close();
        if (listItemsDepp.getDeptTime() != null) {
            textViewDepVal.setText(listItemsDepp.getDeptTime());
        } else {
            textViewDepVal.setText("");
        }
    }
}



public String getCurrentTime() {

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
    String arrDate = mdformat.format(calendar.getTime());
    return arrDate;
}

}