Search code examples
androidlistviewlistviewitem

How to make the items in the listview respond to the click on that particular row on the listview


I am displaying a tab and which has two tags Category1 and Category2. The list gets populated based on the data in the tables for category1 and category2. This works fine but to make the items in the listview respond in a certain way to clicks made on the listview is what I am stuck at. Like for example:

I am trying to make the checkbox become visible and checked when the user long clicks on the particular row in the listview. I am trying to code it but I have no idea where to begin. There aren't questions asked on this topic specifically. Below is the code that I have tried.

FragmentClass

public class CategoryFragment extends Fragment{

    private ListView listView;
    private String currentTabTag;
    private CategoryAdapter1 categoryAdapter1;
    private CategoryAdapter2 categoryAdapter2;
    private MergeAdapter adapter = new MergeAdapter();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_category_fragment_list, container, false);

        // get references to widgets
        listView = (ListView) view.findViewById (R.id.ListView);

        // get the current tab
        TabHost tabHost = (TabHost) container.getParent().getParent();
        currentTabTag = tabHost.getCurrentTabTag();

        // refresh the category list view
        refreshCategoryList();

        listView.setOnItemLongClickListener(this);

        // return the view
        return view;
    }

    public void refreshCategoryList() {
        // get category list for current tab from database
        Context context = getActivity().getApplicationContext();
        CategoryDatabase categoryDatabase = new CategoryDatabase(context);

        if(currentTabTag.equals("Category 1")) {
            ArrayList<Category1> category1ArrayList = categoryDatabase.getAllCategory1(currentTabTag);
            // create adapter and set it in the ListView widget
            categoryAdapter1 = new Category1Adapter(context, category1ArrayList);
            adapter.add(Category1Adapter);
        }
        else if(currentTabTag.equals("Category 2"))
        {
            ArrayList<Category2> category2ArrayList = categoryDatabase.getAllCategory2(currentTabTag);
            // create adapter and set it in the ListView widget
            categoryAdapter2 = new categoryAdapter2(context, category2ArrayList);
            adapter.add(categoryAdapter2);
        }
        listView.setAdapter(adapter);

    }
    @Override
    public void onResume() {
        super.onResume();
        refreshTaskList();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        ItemsListLayout itemsListLayout = new ItemsListLayout(context);

                switch (view.getId()) {
                    case R.id.listView: {
                        itemsListLayout.setMyListItems();
                        Toast.makeText(context,"Why cant I be called?",Toast.LENGTH_SHORT).show();
                        //registerForContextMenu(view);
                    }
            /*default:
                Intent intent = new Intent(context, MyCategoryAddEdit.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("categoryId", category.getId());
                intent.putExtra("editMode", true);
                context.startActivity(intent);
                break;*/
               }
        return false;
    }
}

activity_category_fragment_list

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

    <ListView
        android:id="@+id/prescriptorsListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:longClickable="true"
        android:descendantFocusability="blocksDescendants">
    </ListView>


</LinearLayout>

ItemsListLayout

public class ItemsListLayout extends RelativeLayout implements View.OnCreateContextMenuListener {

    private CheckBox completedCheckBox;
    private TextView nameTextView;
    private TextView notesTextView;
    private ListView listView;
    private CategoryDatabase categoryDatabase;
    private Context context;

    public ItemsListLayout(Context context) {   // used by Android tools
        super(context);
    }

    public ItemsListLayout(Context context, Prescription p) {
        super(context);

        // set context and get db object
        this.context = context;
        categoryDatabase = new CategoryDatabase(context);

        // inflate the layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.activity_my_category_items_list_layout, this, true);

        // get references to widgets
        completedCheckBox = (CheckBox) findViewById(R.id.completedCheckBox);
        completedCheckBox.setVisibility(GONE);
        nameTextView = (TextView) findViewById(R.id.nameTextView);
        notesTextView = (TextView) findViewById(R.id.notesTextView);

        //listView = (ListView) findViewById(R.id.listView);
        // set listeners
        //listView.setOnItemLongClickListener(this);
        // set task data on widgets
        setCategory1(p);
    }

    public void setCategory1(setCategory1 p) {
        setCategory1 = p;
        nameTextView.setText(setCategory1.getCategory1Name());

        // Remove the notes if empty
        if (setCategory1.getMedicineName().equalsIgnoreCase("")) {
            notesTextView.setVisibility(GONE);
        }
        else {
            notesTextView.setText(setCategory1.getDescriptionName());
        }
    }

    /*@Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (view.getId()) {
            case R.id.prescriptorsListView:
                completedCheckBox.setVisibility(VISIBLE);
                completedCheckBox.setChecked(true);
                break;
            default:
                Intent intent = new Intent(context, MyCategoryAddEdit.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("categoryId", category.getId());
                intent.putExtra("editMode", true);
                context.startActivity(intent);
                break;
        }
        return false;
    }
*/
    public void setMyListItems()
    {
        completedCheckBox = (CheckBox) findViewById(R.id.completedCheckBox);
        completedCheckBox.setVisibility(VISIBLE);
        completedCheckBox.setChecked(true);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
    {
        super.onCreateContextMenu(contextMenu);
        //MenuInflater inflater = this.context.
    }
}

activity_my_category_items_list_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutTaskItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <CheckBox
        android:id="@+id/completedCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:button="@drawable/btn_check"
        android:textColor="#ffffff"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/completedCheckBox"
        android:layout_alignBottom="@+id/completedCheckBox"
        android:layout_toRightOf="@+id/completedCheckBox"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:textIsSelectable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/notesTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameTextView"
        android:layout_marginTop="-5dp"
        android:layout_toRightOf="@+id/completedCheckBox"
        android:paddingBottom="7dp"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:longClickable="true"
        android:focusable="false"
        android:clickable="false"
        android:textIsSelectable="false"
        android:focusableInTouchMode="false"/>

</RelativeLayout>

Any kind of help is appreciated. Especially code snippets if possible :).


Solution

  • I suggest you to read this blog, it has step by step procedure of what exactly you want to achieve. You can even clone the project from github.