Purusharth Verma
Purusharth Verma

Reputation: 78

How to handle a click event of a child view of a listview item view?

So..I want to create a list view whose views will be handled by a custom array adapter. Each view of this list view has a child text view. What I want to do is that I want to handle(launch an intent) the click event of this text view which is positioned within the view present within the list view. How do I do that? Would my suggested code run?

Suggested Code:

@Override
public view getView(int position, View convertView, View parent)
{
View v = convertView;
/*code…*/

TextView t = (TextView) v.findViewById(R.id.text_field);

t.setOnClickListener(new OnClickListener({
@Override
public void onClick(View v){
MediaPlayer mp = MediaPlayer.create(thisActivity.this, R.raw.sound);
mp.start();
}));

}

Upvotes: 1

Views: 1467

Answers (3)

okcomputer_kid
okcomputer_kid

Reputation: 511

To capture click event on a child element inside a listview item, you also need to set focusable="false"on child element and focusable="true"on Listview itself.

Other than that, the code looks correct. So try setting focusable and focusableInTouchMode params.

Upvotes: 0

Purusharth Verma
Purusharth Verma

Reputation: 78

If you want to handle the click event of a listview item, here is the code for it:

    /*Inside the onCreate method of your activity class which displays the list
    ......*/
        ListView l = (ListView)findViewById(R.id.list);
        CustomAdapter adapter = new CustomAdapter(this, 0, list, R.color.category_numbers);
        l.setAdapter(adapter);
        l.setOnItemClickListener(new AdapterView.OnItemClickListener({
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long 
        id) {
        /*your code*/
        }
    });

Upvotes: 1

Ranjith KP
Ranjith KP

Reputation: 810

Just refer the below example.

@Override
    public View getView(final int position, View view, final ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.store_list_item, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        holder.storeName.setText(mShoppingLists.get(position).getName());
        holder.editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentManager manager = ((Activity) mContext).getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                ShoppingListEditFragment shoppingListEditFragment =
                        ShoppingListEditFragment.newInstance(mShoppingLists.get(position).getId());
                transaction.replace(R.id.container, shoppingListEditFragment, "shoppinglistedit");
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        holder.handle.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((DynamicListView) parent).onHandleLongClickListener(new DragEvent());
                return true;
            }
        });
        return view;
    }

Upvotes: 1

Related Questions