吐司没有在OnItemCLick中显示

时间:2011-08-18 05:29:08

标签: 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();

    }
    });
    }
}

为什么烤面包不能显示?有人可以帮帮我吗?

这是我的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>

15 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:5)

首先将断点设置为toast并确保您在那里接听电话,如果您接到电话但未显示,请尝试以下操作:

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

答案 2 :(得分:3)

未调用ListView clickListener。原因是Checkbox是可聚焦的,将取消列表视图的可点击能力。

  
    

SIMPLE解决方案:     添加属性:     android:focusable="true"到您的Checkbox xml

  

答案 3 :(得分:2)

为了在按钮点击事件中控制用户界面(如Toast消息),您需要使用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();
            }



}
});

答案 4 :(得分:1)

可以抑制Toast消息。如果是的话,你会在logcat中看到它。您可以通过打开应用程序设置屏幕上的应用程序通知来启用祝酒。

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

答案 5 :(得分:1)

首先检查你的onItemClickListener是否正常工作。如果你的听众不工作,可能你的吐司不会显示。调试代码以查看主要问题。

答案 6 :(得分:1)

这是最好的解决方案。我知道,上下文可以让开发人员解决问题,只需更改:Toast.makeText(getBaseContext(), "You clciked ", Toast.LENGTH_LONG).show();Toast.makeText(arg1.getContext(), "You clciked ", Toast.LENGTH_LONG).show(); Voila!

答案 7 :(得分:1)

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

这应该有用。

&#34; getApplicationContext()&#34;优于&#34; getBaseContext()&#34;因为&#34; getApplicationContext()&#34;实际上与应用程序相关联,并且在整个生命周期中保持不变。 &#34; getBaseContext()&#34;不应该使用,因为它与活动相关联,并且可能在活动被销毁时被销毁。

答案 8 :(得分:1)

使用fn reverse_strings(strings:Vec<&str>) -> Vec<&str> { let actual: Vec<_> = strings.into_iter().rev().collect(); return actual; } 代替getApplicationContext()

答案 9 :(得分:0)

试试这个

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

答案 10 :(得分:0)

您必须指定调用活动,然后指定上下文

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

答案 11 :(得分:0)

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

答案 12 :(得分:0)

您的Listview应该是可点击的并且可以聚焦,否则无法调用onItemClickListener。

您可以在.xml文件中添加可聚焦和可点击的属性,或者在Java代码中以编程方式设置它。

答案 13 :(得分:0)

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

          }
    });

答案 14 :(得分:0)

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]