The Listview item from Android?

I am creating a listView in Android using the following code :

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 adapter = new ArrayAdapter(context,R.layout.list_item,values);

            // Assign adapter to ListView

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

Now I want to Toast a selected Item from my ListView so I use the following code :

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

                }
            });

But Unfortunately I am getting error for the following piece of code

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

The error is

The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)

So What Should I do to correct this error and also print my listview element? Edited

Entire Code

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 adapter = new ArrayAdapter(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();}
       }
}

Nothing is getting toasted. why?

Edit 2

list_item.xml


listview.xml



  
    

You have set your java compiler version to 1.5
so its giving error.

Two things you can do.

1) remove @Override
written before onItemClick

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

Also there is other changes required.

Use YouActivityName.this
instead getBaseContext()
while showing Toast

Item can be null so change… String item = values[position]

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » The Listview item from Android?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录