ListView.SetOnItemClickListener不起作用

时间:2018-04-27 10:20:08

标签: android listview listener

我在单击textView时尝试创建ListView。我需要为这个新ListView的项添加一个监听器。

这是ListView:

我的问题是方法setOnItemClickListener()似乎不起作用,因此我无法打开包含该项目子项的下一个ListView。

在附件中我们可以看到两件事:

  • 布局:它显示了Android的所有内容的布局,我的 问题出在底部的ListView中。

    <include
        layout="@layout/app_bar_content_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" >
        <LinearLayout
    
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="84dp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:background="@color/black">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/white"/>
    
        <ListView
            android:visibility="gone"
            android:id="@+id/navigationList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>
    
        </LinearLayout>
    </android.support.design.widget.NavigationView>
    </android.support.v4.widget.DrawerLayout>
    
  • Class:这只是来自真实课程的片段,但已经足够了。 我在这里尝试做的是当用户点击调用的按钮时 方法&#34; buttonClicked&#34;他的身份是&#34;标题&#34;,我做了几件事,但是 重要的一个是在Layout文件中调用ListView设置他的适配器和make 类&#34; setOnItemClickListener&#34;,它允许ListView的项目 被称为:

    public void buttonClicked(View view) {
    NavigationView gg = (NavigationView) findViewById(R.id.nav_view);
    int id = view.getId();
    switch (id){
        case R.id.title:
            TextView title = (TextView) findViewById(R.id.title);
            String titleText=title.getText().toString();
            LinearLayout needsGone=(LinearLayout) findViewById(R.id.needsGone);
            ListView navigationListView=(ListView)findViewById(R.id.navigationList);
            if (titleText.equalsIgnoreCase(getResources().getString(R.string.Content))){
                //TextView title = (TextView) findViewById(R.id.title);
                title.setText(R.string.main_menu);
                needsGone.setVisibility(View.GONE);
    
                navigationListView.setVisibility(View.VISIBLE);
                adapter = new NavigationAdapter(this, new ArrayList<NavigationClass>());
    
                navigationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        System.out.println("This is where it does not do anything);
    
                    }
                });
                navigationListView.setAdapter(adapter);
    
                new GetTaskDone().execute(request1Url);
            }else if(titleText.equalsIgnoreCase(getResources().getString(R.string.main_menu))){
                title.setText(R.string.Content);
                needsGone.setVisibility(View.VISIBLE);
                navigationListView.setVisibility(View.GONE);
            }
            break;
    ...
    }
    }
    

AsyncTask我在适配器中收取信息:

        protected class GetTaskDone extends AsyncTask<String, Void, 
    List<NavigationClass>> {
            @Override
            protected List<NavigationClass> doInBackground(String... urls) {
                if (urls.length < 1 || urls[0] == null) {
                    return null;
                }
                List<NavigationClass> result = 
     appUtils.fetchDataApp_Navigation(urls[0]);
                return result;
                //navigationList=appUtils.fetchDataApp_Navigation(urls[0]);
                //return appUtils.fetchDataApp_Navigation(urls[0]);
            }

            @Override
            protected void onPostExecute(List<NavigationClass> data) {
                adapter.clear();

                if (data != null && !data.isEmpty()) {
                    adapter.addAll(data);
                }
                //super.onPostExecute(result);
            }

        }
        }

0 个答案:

没有答案