已关闭 - Toast消息未显示

时间:2016-07-07 11:06:32

标签: android android-toast

在此代码中,我想显示toast消息,但它不会显示在点击上。可能,我错过了一些东西。你能搞清楚问题吗?

MainActivity.java

package com.example.hacback17.listviewwithinflater;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);

        //Arrayadapter is used to convert the data source into a view.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list, R.id.textView, days);
        listView.setAdapter(adapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView temp = (TextView) view;
        Toast.makeText(MainActivity.this, temp.getText() + " " + position, Toast.LENGTH_SHORT).show();
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hacback17.listviewwithinflater.MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

simple_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/textView" />
</LinearLayout>

请你还为LayoutInflater编写一个代码片段,因为我还不能正确理解它。 提前谢谢!

添加遗漏部分后。该应用程序现在崩溃了。请看一下logcat。

Process: com.example.hacback17.listviewwithinflater, PID: 19479
                                                                                            java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                                                                                                at com.example.hacback17.listviewwithinflater.MainActivity.onItemClick(MainActivity.java:32)
                                                                                                at android.widget.AdapterView.performItemClick(AdapterView.java:299)
                                                                                                at android.widget.AbsListView.performItemClick(AbsListView.java:1115)
                                                                                                at android.widget.AbsListView$PerformClick.run(AbsListView.java:2928)
                                                                                                at android.widget.AbsListView$3.run(AbsListView.java:3691)
                                                                                                at android.os.Handler.handleCallback(Handler.java:733)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                at android.os.Looper.loop(Looper.java:136)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                                                at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:3)

试试这种方式

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
         listView.setOnItemClickListener(this);

        //Arrayadapter is used to convert the data source into a view.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list, R.id.textView, days);
        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView temp = (TextView) view;
        Toast.makeText(this, temp.getText() + " " + position, Toast.LENGTH_SHORT).show();
    }
}

答案 1 :(得分:1)

你错过了这个......

listView.setOnItemClickListener(this);

所以,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list, R.id.textView, days);
        listView.setAdapter(adapter)

listView.setOnItemClickListener(this);

并在那里通过id查看listview ..它找不到listview id ..

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 switch(parent.getId()) {
        case R.id.listview:

        TextView temp = (TextView) view;
        Toast.makeText(MainActivity.this, temp.getText() + " " + position,     Toast.LENGTH_SHORT).show();     
            break;

        }

    }

我希望,这会有所帮助..

答案 2 :(得分:1)

我认为onItemClick事件中的问题。所以试试这个

Toast.makeText(MainActivity.this,temp.getText+ " " +position,Toast.LENGH.SHORT).show();

答案 3 :(得分:1)

首先声明textview,然后从textview

获取文本
@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView temp = (TextView) view;
temp = (TextView)view.findViewById(R.id.listView);
        Toast.makeText(this, temp.getText() + " " + position, Toast.LENGTH_SHORT).show();
    }
}

答案 4 :(得分:0)

您的setOnItemClickListener回拨错过请添加

  listView.setOnItemClickListener(this);

希望这会对您有所帮助