Search code examples
androidlistvieweventsonitemclicklistenerselected

Is there any way through which I can make a specific item clickable in a ListView?


I have a scenario, where I am showing options in a ListView. But, for a special case, I need only last item of the ListView to be clickable. Is there any way I can achieve this??

adapter code:

    public class OptionAdapter extends ArrayAdapter<OptionsPojo> {
    private int mColorResourceId;

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
   OptionsPojo optionsPojo = getItem(position);

    View listItemView =  convertView;
    if (listItemView == null){
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.option_card, parent,false);
    }

    View textImgContainer = listItemView.findViewById(R.id.ll_img_text);
    View textContainer = listItemView.findViewById(R.id.rl_text);

    ImageView icon = listItemView.findViewById(R.id.image_view);
    TextView imgOptnText = listItemView.findViewById(R.id.option_text_view);
    TextView optionText = listItemView.findViewById(R.id.tv_text);
    int color = getContext().getResources().getColor(mColorResourceId);

    if(optionsPojo.hasImage())
    {
        icon.setImageResource(optionsPojo.getmImageResourceId());
        textImgContainer.setBackgroundColor(color);
        icon.setVisibility(View.VISIBLE);

        //setting option with image
        if (optionsPojo.getOption().startsWith("<p>"))
        {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
                imgOptnText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
            else
                imgOptnText.setText(Html.fromHtml(optionsPojo.getOption()));
        }else
            imgOptnText.setText(optionsPojo.getOption());
    }else{
        textImgContainer.setVisibility(View.GONE);
        textContainer.setVisibility(View.VISIBLE);
        textContainer.setBackgroundColor(color);

        //setting option without image
        if (optionsPojo.getOption().startsWith("<p>"))
        {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
                optionText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
            else
                optionText.setText(Html.fromHtml(optionsPojo.getOption()));
        }else
            optionText.setText(optionsPojo.getOption());
    }
    return listItemView;
}

public OptionAdapter(@NonNull Context context, @NonNull ArrayList<OptionsPojo> options, int colorResourceId) {
    super(context,0, options);
    mColorResourceId = colorResourceId;
}

Activity code which is using listview:

      OptionAdapter Adapter = new OptionAdapter(this, optionList, R.color.background);
        listview.setAdapter(Adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer());
            }
        });
    }

Solution

  • Please check with this code

    listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                    if(i == (list.size()-1)){
                        showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer()); 
                    }
                }
            });