【问题标题】:setOnItemClickListener error in ListView wth ArrayAdapter带有 ArrayAdapter 的 ListView 中的 setOnItemClickListener 错误
【发布时间】:2016-11-11 06:25:43
【问题描述】:

我正在开发 Android 应用程序,它需要 listviewArrayAdapter。现在我想要 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)

【问题讨论】:

  • 请在您的问题中添加错误的位置和错误...
  • 为什么会有 iframe 标签?
  • 可能你的 CountryAdapter.class 中有一些 onClickListeners。删除它们
  • @Dan 我编辑了问题
  • 我已经导入了android.widget.*;和 android.view*; @HsRaja

标签: java android listview android-arrayadapter onitemclicklistener


【解决方案1】:

尝试在setOnItemClickListener方法中加入AdapterView.OnItemClickListener(),它并不只适用于OnItemClickListener

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

        }
    });

【讨论】:

  • 请更新你有问题的列表视图,无论你写了什么
  • 列表视图为? countryAdapter.java 还是 XML 文件?
  • 用你写的新代码更新 ListViewSampleActivity 文件
【解决方案2】:

改用此代码

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

你的监听器应该是 AdapterView.OnItemClickListener

【讨论】:

  • 还有什么错误
  • 我只是在问题中添加了它.. 看看 ^
  • 你能发布你的导入吗
  • 只需删除您的导入,您将收到错误并通过选择错误词添加导入并在 Windows 中按 alt+enter 或在 mac 中按 option + enter 让 android studio 决定他们要导入的类。有许多这样的类同名但不同的包导致模棱两可的问题
  • 好吧,我在安卓手机上做,现在怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多