ListView中的setOnItemClickListener错误与ArrayAdapter

时间:2016-11-11 06:25:43

标签: java android listview android-arrayadapter onitemclicklistener

我正在开发Android应用,这需要 listview ArrayAdapter 。现在我想在这个列表视图上 onItemClickListener 。但IDE,Anacode(与eclipse相同)显示错误!

我在这样的问题帖子中尝试过每一个答案。但没有帮助!

请帮忙

//the code 
package edward.harsh.friends;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;   

public class ListViewSampleActivity extends Activity
{
    ListView mCountriesLV;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**
         * Get the control instances from the main.xml layout
         */
        mCountriesLV = (ListView) findViewById(R.id.countriesLV);
        mCountriesLV.setAdapter(new CountriesAdapter(this, R.layout.listview_country_row, mCountriesList));



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

   }
}

错误消息是

   The method    setOnItemClickListener(AdapterView.OnItemClickListener in the type
  AdapterView <ListAdapter> is not applicable for the arguments (new   OnItemClickListener(){})

OnItemClickListener cannot be resolved to a type
某人建议

我甚至试过这个

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

但它会引发另一个错误

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

2 个答案:

答案 0 :(得分:1)

尝试在 setOnItemClickListener 方法中添加 AdapterView.OnItemClickListener(),它不仅适用于 OnItemClickListener

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

        }
    });

答案 1 :(得分:0)

使用此代码

mCountriesLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {}

您的监听器应该是AdapterView.OnItemClickListener