开发者

Toast not show 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 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_开发者_如何学Pythoncontent" 
     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>


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


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();


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


Inorder to control User Interface (like toast messages) in button click events , you need to use Handler's help.

Specicy the handler as below in the starting of class.

Handler objHandler = new Handler();

And inside button click event, use object of handler to execute Toast command (as below).

    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();
            }



}
});


Toast messages can be suppressed. If they were, you'd see it in logcat. You can enable toasts by turning on the notifications for the app, which is on the app settings screen.

https://stackoverflow.com/a/12401795/614612


First check whether your onItemClickListener is working or not. If your listener is not working, probably your toast will not show. Debug your code to see the main problem.


This is the best solution. I know, context can make developers pull their hair out, just change: Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show(); for this: Toast.makeText(arg1.getContext(), "You clciked ", Toast.LENGTH_LONG).show(); Voila!


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

This should work.

"getApplicationContext()" is preferred over "getBaseContext()" because "getApplicationContext()" is actually associated with the application and will remain the same throughout the lifecycle. "getBaseContext()" should not be used as it is associated with the activity and may be destroyed when the activity is destroyed.


Use getApplicationContext() instead of getBaseContext.


Try this

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


Your Listview should be clickable and focusable, otherwise your onItemClickListener could not be called.

You can add focusable and clickable attributes in your .xml file or set it programmatically in your java code.


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


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();

          }
    });


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]


You have to specify call the activity and then context

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜