How do I implement onClickListener on a custom adapter?

I am involved in building a simple time table management application. I have a list of courses. Each item in the list is a textview + delete button. The onClick listener on my list item is not working as expected. When I click the delete button, it works fine. However, I want to open some other actions when the user clicks on the text view of the list item.

code:

ShowAll.java (main activity where I show the list of classes)

package com.example.android.mytimetable;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;


public class ShowAll extends ActionBarActivity {
    private ArrayAdapter<String> adapter ;
    ArrayList <ClassDetail> classesDetail ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_all);
        this.bindAdapter();

        ListView listView = (ListView) this.findViewById(R.id.class_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.v("Item", "clicked");
                Intent intent = new Intent(view.getContext(), ShowAllClicked.class);
                ClassDetail classDetail = classesDetail.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("CLASS_NAME", classDetail.class_name);
                bundle.putString("BUILDING", classDetail.building);
                bundle.putString("MONDAY_START", classDetail.monday_start);
                bundle.putString("MONDAY_END", classDetail.monday_end);
                bundle.putString("TUESDAY_START", classDetail.tuesday_start);
                bundle.putString("TUESDAY_END", classDetail.tuesday_end);
                bundle.putString("WEDNESDAY_START", classDetail.wednesday_start);
                bundle.putString("WEDNESDAY_END", classDetail.wednesday_end);
                bundle.putString("THURSDAY_START", classDetail.thursday_start);
                bundle.putString("THURSDAY_END", classDetail.thursday_end);
                bundle.putString("FRIDAY_START", classDetail.friday_start);
                bundle.putString("FRIDAY_END", classDetail.friday_end);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    void bindAdapter() {
        DBHelper db = new DBHelper(this);
        classesDetail = db.getClassesDetail();
        ArrayList <String> classes = new ArrayList<>();
        for(int i = 0 ; i < classesDetail.size() ; i++) {
            Log.v("Adding ", classesDetail.get(i).class_name);
            classes.add(classesDetail.get(i).class_name);
        }
        if(classes.size() == 0)
            ((TextView) this.findViewById(R.id.holiday)).setText(getString(R.string.noClass));
        CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(classes, this);
        ((ListView) this.findViewById(R.id.class_list)).setAdapter(customArrayAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_show_all, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return super.onOptionsItemSelected(item);
    }
}

      

activity_show_all.xml (ShowAll.java xml layout)

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.mytimetable.ShowAll"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/class_list"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/holiday"/>

</LinearLayout>

      

CustomArrayAdapter.java (adaptive adaptive array file)

package com.example.android.mytimetable;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Aman Goel on 02-08-2015.
 */
public class CustomArrayAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList <String> list = new ArrayList<String>();
    private Context context;

    public CustomArrayAdapter(ArrayList <String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item, null);
        }

        ((TextView) view.findViewById(R.id.list_item)).setText(list.get(position));

        Button deleteBtn = (Button) view.findViewById(R.id.delete);

        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DBHelper db = new DBHelper(context);
                db.deleteClass(list.get(position));
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        return view;
    }
}

      

list_item.xml (location of each list view)

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:id="@+id/list_item"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delete"
        android:id="@+id/delete"/>

</LinearLayout>

      

I tried to help here: Set onClickListener on Custom Adapter and here: Where should I put onClickListener on Custom ListView?

However, I still cannot get the adapter to work. Any help would be appreciated

+3


source to share


2 answers


set custom adapter getview function



TextView lst_tv=(TextView)view.findViewById(R.id.list_item);
lst_tv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});

      

+1


source


customArrayAdapter.setOnItemClickListener(...)



0


source







All Articles