0

I have to receive clicked list number. When I use getcheckeditemposition() method of ListView, return number is always 1. I don't know why. I have to solve this problem.

This is my code:

ChoiceDialogFragment.java

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import aa.SendFragment;

public class ChoiceEmotionDialogFragment extends DialogFragment {

    private ListView listView;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> data = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);

        data.add("a");
        data.add("b");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);

        final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
        cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("checkedNum", listView.getCheckedItemCount()+"");
            }
        });

        listView = (ListView) view.findViewById(R.id.listview);
        adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
        listView.setAdapter(adapter);

        return view;
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="15dp"
        android:text="choice"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_title">

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:choiceMode="singleChoice" />

        <TableRow>

            <Button
                android:id="@+id/dialog_button_cancel"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:text="cancel" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#90909090" />

            <Button
                android:id="@+id/dialog_button_confirm"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:background="@null"
                android:text="confirm" />

        </TableRow>
    </TableLayout>
</RelativeLayout>
Selena Lee
  • 33
  • 7
  • Where does your code use `getCheckedItemPosition()`? I don't see it. You did use `getCheckedItemCount()`. Maybe that's the problem? That would explain why the answer was always 1 (after you selected an item). – LarsH Aug 06 '19 at 02:06
  • @LarsH Hi, This is a situation that I experienced two years ago. So, I do not remember the details. getCheckedItemCount() called when I click confirm button. Maybe I solved the situation using the interface. – Selena Lee Aug 06 '19 at 11:01

2 Answers2

0

You can use setOnItemClickListener to get selected item first, and store it to a global variable;

int selectedItemPosition;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);

    final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
    cancel.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            dismiss();
        }
    });

    final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
    confirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Access the selected item here
            Log.d("checkedNum", selectedItemPosition+"");
        }
    });

    listView = (ListView) view.findViewById(R.id.listview);
    adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Store the position
            selectedItemPosition = position;      
        }
    });

    return view;
}
0

you have to set the item as checked on clicking the item

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             listView.setItemChecked(position, true);
    }
});

Existing questions

Community
  • 1
  • 1
Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38