-1

The onListItemClick is not working. I am trying to store the values selected in the listview and pass it on to the mainactivity.

How can I get the selected Items positions and pass it on to the Main Activity?

Please suggest and thank you for the help.

Here is my code:

public static class ListViewFragment extends Fragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";
    //FragmentActivity listener;
    private ListView myListView;
    private EditText editsearch;
    private CheckBox selectAll;
    private ArrayList<String> SelectedFilter1;
    private ArrayList<String> CheckedFitlers;

    ArrayAdapter<String> adapter_list;

    public ListViewFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);  

        SelectedFilter1=getArguments().getStringArrayList("ArrayList");

        adapter_list = new ArrayAdapter<String>(this.getActivity(),
                android.R.layout.simple_list_item_multiple_choice,SelectedFilter1);

        adapter_list.notifyDataSetChanged();

        myListView.setAdapter(adapter_list);

        myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        myListView.setTextFilterEnabled(true);

        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                adapter_list.getFilter().filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
        });

        selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

                Log.i("CheckBOX","Is Checked:"+isChecked);
                if(isChecked)
                {
                    for ( int i=0; i< myListView.getCount(); i++ ) 
                        myListView.setItemChecked(i, true);
                }else
                {
                    for ( int i=0; i< myListView.getCount(); i++ ) 
                        myListView.setItemChecked(i, false);
                }
            }
        }
     );
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        //showDetails(position);
        Log.i("Item Clicked in the Listview","Item Clicked in the listview: "+position);
        //Toast.makeText(getActivity(), "Item Clicked in the listview: "+position, 3000).show();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {        

        View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

        myListView=(ListView) rootView.findViewById(R.id.listView_brand);

        editsearch = (EditText) rootView.findViewById(R.id.search);

        selectAll=(CheckBox) rootView.findViewById(R.id.checkBox1);

        //myListView.setFocusable(false);

       // myListView.setClickable(false);

        selectAll.setFocusable(false);

       // rootView.setFocusable(false)

        return rootView;
    }
}

The XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="test.dsi.decathnavigation.Brand_list" >

<ListView
    android:id="@+id/listView_brand"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/search"
    android:fastScrollAlwaysVisible="false"
    android:fastScrollEnabled="false"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollingCache="true"
    android:smoothScrollbar="false" >

</ListView>

<EditText
    android:id="@+id/search"
    android:layout_width="450dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView_brand"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="Type to search" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/search"
    android:text="Select All" />

</RelativeLayout>
PramodK
  • 171
  • 1
  • 13

3 Answers3

0

onListItemClick not working in fragment

Because you are not implementing OnItemClickListener in ListViewFragment Fragment and overriding onListItemClick of OnItemClickListener.

Do it as:

myListView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3)
    {
        Log.i("Item Clicked in the Listview","Item Clicked in the listview: "+position);
    }
});
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

You will need to add a OnItemClickListener as this for ListView:

myListView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    //your code over here
    }
});

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Can you please help me suggest how to pass the checked items to main activity? – PramodK Sep 16 '14 at 08:36
  • @PramodK [get clicked listview item's text](http://stackoverflow.com/a/9208977/1777090), [and pass to other activity using intent](http://stackoverflow.com/a/5265952/1777090) – MysticMagicϡ Sep 16 '14 at 08:42
0

i will try to help using my example:

In Activity which contains ListView declare your ListView:

   public class RankingActivity extends Activity {
   ListView bestPlayersList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        mContext=this;
        bestPlayersList = (ListView) findViewById(R.id.bestPlayersListView);
     }

bestPlayersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final CheckedTextView item = (CheckedTextView) view;

                    if (item.isChecked())
                    {
                       //..do some action..for example get item text: item.getText()
                     }
                }
               });
}

It works for me;)