Android敬酒Listview元素?

时间:2012-05-04 10:28:48

标签: android listview

我使用以下代码在Android中创建listView:

 ListView listView = (ListView) List.this.findViewById(R.id.mylist);
         listView.setTextFilterEnabled(true);

         String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" ,"asdsad","asdsdsa","asdasdsa"};

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item,values);

            // Assign adapter to ListView

            listView.setAdapter(adapter);
            listView.setVisibility(0);

现在我想从我的ListView Toast一个选定的项目,所以我使用以下代码:

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

                    String item = ((TextView)view).getText().toString();

                    Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

                }
            });


但不幸的是我收到了以下一段代码的错误

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

错误是

新的AdapterView.OnItemClickListener(){}类型必须实现继承的抽象方法      AdapterView.OnItemClickListener.onItemClick(AdapterView,View,int,long)


那么我该怎么做才能纠正这个错误并打印我的listview元素? 的被修改

整个代码

package com.notification;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.view.*;

public class List1 extends Activity 
{

Context context=this;

@Override
        public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        //ListView
        try{

         ListView listView = (ListView) List1.this.findViewById(R.id.mylist);
         listView.setTextFilterEnabled(true);

         String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" ,"asdsad","asdsdsa","asdasdsa"};

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item,values);

            // Assign adapter to ListView

            listView.setAdapter(adapter);
            listView.setVisibility(0);

            //required
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view, int position,long id)
                //public void onItemClick(ListView<?> listview, View view, int position,long id)
                {

                 Toast.makeText(List1.this, "hi",Toast.LENGTH_LONG).show();
                }
            });


            //required
            }
       catch(Exception e)
       {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();}
       }
}

没有什么可以烘烤了。为什么?
编辑2
list_item.xml

<?xml version="1.0" encoding="utf-8"?>



<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tv1"
    android:padding="10dp"
    android:background="@drawable/textview1"
    style="@style/textview_text"
    android:textSize="16dp">


</Button>

listview.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="fill_parent" >


  <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

4 个答案:

答案 0 :(得分:2)

将列表项的样式更改为R.layout.simple_liast_item_1而不是自定义项。

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,values);

答案 1 :(得分:1)

您已将 java编译器版本设置为1.5 ,因此其给出错误。

你可以做两件事。

1)删除 onItemClick

之前写的@Override

2)right-click project - properties - java compiler - check-Enable project setting - change to 1.6

还需要进行其他更改。

在显示YouActivityName.this

时使用getBaseContext()代替Toast

项目可以为null,因此请更改... String item = values[position]

答案 2 :(得分:0)

    ListView listView = (ListView) findViewById(R.id.mylist);
    listView.setTextFilterEnabled(true);

    final String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
       "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
       "Linux", "OS/2" ,"asdsad","asdsdsa","asdasdsa"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

       // Assign adapter to ListView

       listView.setAdapter(adapter);
       listView.setVisibility(0);

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

               String item = values[position];

               Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

           }
       });

答案 3 :(得分:0)

试试这个,

Toast.makeText(getApplicationContext(),
                ((TextView) view).getText(), Toast.LENGTH_SHORT).show();