【问题标题】:How I can use the onItemClick in my android application?如何在我的 android 应用程序中使用 onItemClick?
【发布时间】:2013-01-23 12:35:25
【问题描述】:

您好,我的 android 应用程序中的 onItemClick 有问题。我试了一下,但我得到了一个错误。我希望在我的列表视图中从 sqlite 数据库中获取数据,并且可以单击此列表中的一个值以使用该值启动另一个活动。查看我的代码以获取更多信息:

我的活动:

public class ShowActivity extends ListActivity {

我的方法:

private void ladeDaten() {
        Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); 
        startManagingCursor(KlassenCursor); 

        android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, 
                KlassenCursor, 
                new String[] {"name"},
                new int[] {
                android.R.id.text1
                });

        setListAdapter(KlassenAdapter);

        ListView lv = (ListView)findViewById(android.R.id.list); 

        lv.setOnItemClickListener(new OnItemClickListener() {

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

                Cursor cursor = (Cursor)parent.getItemAtPosition(position);

                String item = cursor.getString(cursor.getColumnIndex("name"));

                Intent intent = new Intent(this,ShowActivity.class); //here is the error

                intent.putExtra("iteid", item); 

                startActivity(intent);
            }
        });

    }

我的布局: ...

    </ListView>

...

我的错误:

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<ShowActivity>) is undefined

我能做什么? :(

更新:

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

                Cursor cursor = (Cursor)parent.getItemAtPosition(position);

                String item = cursor.getString(cursor.getColumnIndex("name"));

                Intent intent = new Intent(ShowDayActitiy,ShowDayActivity.class); //here is the error

                intent.putExtra("iteid", item); 

                startActivity(intent);
            }
        });

我想开始一个治疗 :(

【问题讨论】:

  • 为什么哦为什么告诉我们你得到一个错误,然后不告诉我们错误是什么?你认为错误是随机的还是不重要的?
  • new Intent(ShowActitiy.this ,ShowDayActivity.class);替换new Intent(this,ShowActivity.class);
  • @njzk2 :这可能是答案。已经有 7 个答案,但你击败了所有答案,因为 Op 正在尝试打开 ShowDayActivity,但没有提及还没有。

标签: java android sqlite listview onitemclick


【解决方案1】:

发生这种情况是因为在您创建意图时,第一个参数是 context 。您正在从 onClick() 创建意图,因此其中的上下文不属于外部活动。 Intent 应该以当前活动的上下文作为第一个参数

因此使用ShowActivity.thisgetApplicationContext() 而不是使用这个

Intent intent = new Intent(getApplicationContext(), SecondActivity.class)

Intent intent = new Intent(FirstActivity.this, SecondActivity.class)

会成功的。

如有任何疑问,请随时提出

【讨论】:

    【解决方案2】:

    改变这个

    Intent intent = new Intent(this,ShowActivity.class); 
    

    Intent intent = new Intent(ShowActivity.this,ShowActivity.class); 
    

    【讨论】:

      【解决方案3】:

      使用

      Intent intent = new Intent(ShowActivity.this,ShowActivity.class);
      

      Intent intent = new Intent(view.getContext(),ShowActivity.class);
      

      而不是

      Intent intent = new Intent(this,ShowActivity.class);
      

      要启动 Activity,您需要传递 Activity 上下文而不是视图

      【讨论】:

        【解决方案4】:

        你的意图代码有问题

        Intent intent = new Intent(getapplicationContext(),ShowActivity.class); 
        intent.putExtra("iteid", item); 
        startActivity(intent);
        

        【讨论】:

          【解决方案5】:

          检查代码中 OnItemClickListener() 的导入。您使用了 adapter.onitemClickListener。

          删除该导入并导入 ListView.onItemClickListener

          你可以使用

          onItemClick(AdapterView adapterView, View view, int 位置,长值){}

          【讨论】:

            【解决方案6】:

            当您在 nItemClickListener 中使用“this”时设置 Intent intent = new Intent(ShowActivity.this,ShowActivity.class);,您没有获得 Activity 上下文。

            【讨论】:

              【解决方案7】:

              AdapterView 父级

              使用 parent.getitematpostion(postion).toString()。它会帮助你。

              【讨论】:

              • 不,因为它是一个游标,并且 cursor.toString() 没有多大意义。而it will help you 也没什么意义
              【解决方案8】:

              请更正代码

               @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);        
                      setContentView(R.layout.showroom);        
                      initView(); 
              
              
              
                      ListView list = getListView();
                      list.setOnItemClickListener(new OnItemClickListener() {
                           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              
              
              
              
                       if(dialog != null)  dialog.dismiss();
                          Dialog_Sh_Adapter task = new Dialog_Sh_Adapter(GStore.this, data);
                          setListAdapter(task); 
              
              
                              }
                          });
              

              【讨论】:

              • 欢迎来到stackOverflow,请阅读guide。通过编写完整详细的答案,您将帮助每个人理解解决方案。谢谢。
              猜你喜欢
              • 2012-01-26
              • 2012-09-15
              • 2015-04-03
              • 1970-01-01
              • 1970-01-01
              • 2012-03-31
              • 2011-01-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多