ListView Toast消息

时间:2016-04-18 04:31:51

标签: android listview

我这里有这个代码。当我点击列表视图中的一行但没有显示任何内容时,我正在尝试显示一个Toast消息。这是我的代码。

   public class MainActivity extends AppCompatActivity {

        ListView list;
        String[] movieTitles;
        int[] images = {R.drawable.batman_vs_superman, R.drawable.captain_america, R.drawable.deadpool,
                R.drawable.jungle_book, R.drawable.xmen,R.drawable.zootopia, R.drawable.hail,R.drawable.allegent,
                R.drawable.jason, R.drawable.lane};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Resources res= getResources();
            movieTitles = res.getStringArray(R.array.titles);

            list = (ListView)findViewById(R.id.listView);

            MoviesAdapter adapter = new MoviesAdapter(this,movieTitles, images);

            list.setAdapter(adapter);

            list.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();

                }
            });

        }
    }

编辑我这里有两个xml文件是代码:

&#34; content_main.xml&#34;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.emmancipatemusemwa.task2.MainActivity"
    tools:showIn="@layout/activity_main">

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

这里我有单行设计的single_row.xml。

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

    <ImageView
        android:src="@drawable/captain_america"
        android:paddingTop="10dp"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <RatingBar
        android:layout_width="245dp"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="more info"
        android:id="@+id/textView2"
        android:layout_below="@+id/ratingBar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textStyle="italic" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

试试这个!!!!

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), "You selected "+ position, Toast.LENGTH_SHORT).show();

    }
});

答案 1 :(得分:0)

在主动性方面做这件事

// Import 

import android.widget.AdapterView;

.........
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();
            }
        });


    OR IN UR CUSTOM ADAPTER LISTVIEW ALTER/ADD THIS


 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;       
             rowView = inflater.inflate(R.layout.program_list, null);
.....................
...................
...................
rowView.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You selected "+result[position], Toast.LENGTH_LONG).show();
            }
        });   
        return rowView;
        }
  

注意:没有两个setOnClickListener,只使用了一个,   根据你的问题,我认为你使用两个setOnClickListener   功能如此Toast不工作​​。

U should use only one in MainActivity or ur custom class (MovieAdapter).