Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast not show in OnItemCLick

Tags:

android

package com.custom;

import java.util.ArrayList;
import java.util.List;



import android.app.Activity;
import android.content.Intent;


import android.os.Bundle;
import android.util.Log;
import android.view.View;


import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {
    ListView list;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.v("", "on create");
        ListView list = (ListView)findViewById(R.id.list);

        List<PhoneList> listOfPhonebook = new ArrayList<PhoneList>();
        listOfPhonebook.add(new PhoneList("Sutadi","9898989", "Jl.Pasar"));
        listOfPhonebook.add(new PhoneList("Iyand","1234455", "Jl.Mall"));
        listOfPhonebook.add(new PhoneList("Yanti","00000", "Jl.Sawah"));
        Log.v("", "Add phone list");

        PhoneBookAdapter adapter = new PhoneBookAdapter(this, listOfPhonebook);
        Log.v("", "Adapter create");

        list.setAdapter(adapter);
        Log.v("", "adapter implement");



    list.setOnItemClickListener(new OnItemClickListener() {

        @Override

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show();

    }
    });
    }
}

why the toast can not show ? anyone can help me ?

This is my PhoneBookAdapter

package com.custom;

import java.util.List;

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

public class PhoneBookAdapter extends BaseAdapter{
    private Context context;

    private List<PhoneList> listPhonebook;

    public PhoneBookAdapter(Context context, List<PhoneList> listPhonebook) {
        this.context = context;
        this.listPhonebook = listPhonebook;
    }

    public int getCount() {
        return listPhonebook.size();
    }

    public Object getItem(int position) {
        return listPhonebook.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        PhoneList entry = listPhonebook.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row, null);
        }
        TextView tvContact = (TextView) convertView.findViewById(R.id.textstatus);
        tvContact.setText(entry.getName());

        TextView tvPhone = (TextView) convertView.findViewById(R.id.textkonos);
        tvPhone.setText(entry.getPhone());

        TextView tvAlamat = (TextView) convertView.findViewById(R.id.textalamat);
        tvAlamat.setText(entry.getAlamat());

        return convertView;
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Custom"
    android:textSize="10pt"
    android:paddingBottom="7px"
    android:background="#d5d5d5"
    android:textColor="#000000"
    />
<ListView
    android:paddingLeft="2px"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list"
></ListView>

</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"

    android:padding="6dip">


    <CheckBox android:text="" 
    android:layout_width="wrap_content" 
    android:id="@+id/checkBox1" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"></CheckBox>





    <TextView
    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textnama"
            android:padding="7px" 
            android:layout_alignParentLeft="true"
            android:text="Konos : "/>

    <TextView
       android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textstatus"
            android:singleLine="true"
            android:padding="7px"

            android:layout_below="@+id/textnama"
            android:layout_alignParentLeft="true"
            android:text="haha"/>


     <TextView android:layout_width="wrap_content" 
     android:text="hohohoho" 
     android:layout_height="wrap_content" 
     android:id="@+id/textkonos" 
     android:paddingRight="3px" 
     android:padding="7px" 
     android:layout_above="@+id/textstatus" 
     android:layout_toRightOf="@+id/textnama"></TextView>

    <TextView android:layout_width="wrap_content" 
     android:text="-" 
     android:layout_height="wrap_content" 
     android:id="@+id/text" 
     android:paddingRight="3px" 
     android:padding="7px" 
     android:layout_below="@+id/textkonos"
     android:layout_toRightOf="@+id/textstatus"></TextView>


     <TextView android:layout_width="wrap_content" 
     android:text="" 
     android:layout_height="wrap_content" 
     android:id="@+id/textalamat" 
     android:paddingRight="3px" 
     android:padding="7px" 
     android:layout_alignParentBottom="true" 
     android:layout_alignLeft="@+id/textkonos"></TextView>


</RelativeLayout>
like image 764
Newbie Avatar asked Aug 18 '11 05:08

Newbie


3 Answers

Toast.makeText(getApplicationContext(), "You clicked ", Toast.LENGTH_LONG).show();
like image 167
ngesh Avatar answered Oct 25 '22 01:10

ngesh


first put the breakpoint at toast and ensure that you are getting the call there and if you get the call and it is not showing then try this:

Toast.makeText(MainActivity.this, "You clciked ", Toast.LENGTH_LONG).show();
like image 5
Vineet Shukla Avatar answered Oct 25 '22 00:10

Vineet Shukla


Your ListView clickListener is not called. The reason for this is the Checkbox is focusable will cancel out the listview's clickable ability.

SIMPLE solution: add attribute: android:focusable="true" to your Checkbox xml

like image 3
Tin Tran Avatar answered Oct 25 '22 01:10

Tin Tran