Toast does not appear in OnItemCLick

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 can't the toast show? can anyone 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>
+5
source share
15 answers
Toast.makeText(getApplicationContext(), "You clicked ", Toast.LENGTH_LONG).show();
+7
source

first put a breakpoint in the toast and make sure you get a call there, and if you get a call and it doesn’t appear, try the following:

Toast.makeText(MainActivity.this, "You clciked ", Toast.LENGTH_LONG).show();
+5
source

(, ) , Handler.

, , .

Handler objHandler = new Handler();

Toast ( ).

    list.setOnItemClickListener(new OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
     objHandler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show();
            }



}
});
+2

ListView clickListener . , , .

:    :    android:focusable="true" xml

+2
+1

, onItemClickListener . , , . , .

+1

. , , : Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show(); : Toast.makeText(arg1.getContext(), "You clciked ", Toast.LENGTH_LONG).show(); Voila!

+1
Toast.makeText(getApplicationContext(), "You clciked ", Toast.LENGTH_LONG).show(); 

.

"getApplicationContext()" "getBaseContext()", "getApplicationContext()" . "getBaseContext()" , .

+1

getApplicationContext() getBaseContext.

+1

Context context = getApplicationContext();
            CharSequence text = "Your text here";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
0

,

Toast.makeText(getApplicationContext(), "You clciked ", Toast.LENGTH_LONG).show();
0
source

Toast.makeText(getActivity().getApplicationContext,"My Toast",Toast.LENGTH_LONG)

0
source

Your Listview must be interactive and interactive, otherwise your onItemClickListener cannot be called.

You can add custom and interactive attributes to your .xml file or set it programmatically in your Java code.

0
source
button = (Button) findViewById(R.id.buttonToast);

    button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

             Toast.makeText(getApplicationContext(), 
                           "Button is clicked", Toast.LENGTH_LONG).show();

          }
    });
0
source
button = (Button) findViewById(R.id.buttonToast);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get your custom_toast.xml ayout
            LayoutInflater inflater = getLayoutInflater();

            View layout = inflater.inflate(R.layout.custom_toast,
              (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            // set a dummy image
            ImageView image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            // set a message
            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText("Button is clicked!");

            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
    });


  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="horizontal"
android:padding="5dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#000" />

</LinearLayout>




[enter image description here][1]
0
source

All Articles