vendredi 3 juin 2016

Checkboxes not getting clicked

I am working on self-learning android and presently trying my hands on creating a list view with checkboxes. However when I run the program and I do see the list view with checkboxes, but I don't see checks in the checkbox when I select any of the options. Am I missing something here?

MainActivity:

package com.example.practisetutorials.myapplication;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.Menu;

public class MainActivity extends Activity {

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        ListView listView = (ListView) findViewById(R.id.listView);

        String[] fruits = {"Banana", "Mango", "Apple", "Guava", "Orange", "Water Melon", "Grapes", "Cantaloupe", "Strawberry", "Blackberry", "Blueberry", "Papaya", "Pineapple"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.fruit_item, R.id.checkedTextView, fruits);
        listView.setAdapter(adapter);
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    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="com.example.practisetutorials.myapplication.MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>
</RelativeLayout>

fruit_item.xml

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/checkedTextView"
        android:gravity="center"
        android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
        android:checkMark="@color/accent_material_light"/>
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire