ListView控件setOnItemClickListner不

ListView控件setOnItemClickListner不

本文介绍了ListView控件setOnItemClickListner不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用列表视图,因为长,但此时我面临一个奇怪的问题。这是我想提出setonitemclick听者就可以了,但它不工作我也搞不清楚这哪里是问题。我还有一个XML为列表视图,显示DATAS。

XML:

 < ListView的机器人:ID =@ + ID / LV
机器人:dividerHeight =5DP
机器人:choiceMode =singleChoice
机器人:可点击=真
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真正的>
 

code:

 包algonation.com.noteup;

进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.widget.DrawerLayout;
进口android.support.v7.app.ActionBarActivity;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
进口android.widget.Toast;


公共类MainActivity扩展ActionBarActivity
        实现NavigationDrawerFragment.NavigationDrawerCallbacks {

   私人NavigationDrawerFragment mNavigationDrawerFragment;

    私人的CharSequence mTitle;
    私人DBManager dbManager;

    私人的ListView ListView的;

    私人SimpleCursorAdapter适配器;

    最终的String []从=新的String [] {DatabaseHelper.TAG_TITLE,
            DatabaseHelper.TAG_TIME,DatabaseHelper._ID,
            DatabaseHelper.ADD_TAG
             };



    最终诠释[]到=新INT [] {R.id.title,R.id.time,R.id.textView4,R.id.fullnote};
    私人查看F;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        dbManager =新DBManager(本);
        dbManager.open();
        最终的动画animAlpha = AnimationUtils.loadAnimation(这一点,R.anim.alfa);
        最终按钮btnmain =(按钮)findViewById(R.id.button4);
        最终按钮btn_addnote =(按钮)findViewById(R.id.button2);
        最终按钮btn_TakePic =(按钮)findViewById(R.id.button5);
        btn_addnote.setVisibility(View.INVISIBLE);
        btn_TakePic.setVisibility(View.INVISIBLE);
        btnmain.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){

                v.startAnimation(animAlpha);
                意图I =新的意图(MainActivity.this,Camera.class);
                startActivity(ⅰ);
                btn_addnote.setVisibility(View.VISIBLE);
                btn_TakePic.setVisibility(View.VISIBLE);

                btnmain.setText(X);
            }
        });
        光标光标= dbManager.fetch();

        ListView的=(的ListView)findViewById(R.id.lv);

        适配器=新SimpleCursorAdapter(MainActivity.this,R.layout.activity_view_record,光标,从到,0);
        adapter.notifyDataSetChanged();
         listView.setAdapter(适配器);

       listView.setOnItemClickListener(新OnItemClickListener(){
           @覆盖
           公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
               Toast.makeText(MainActivity.thisfdfdfd,Toast.LENGTH_SHORT).show();
           }
       });

            mNavigationDrawerFragment =(NavigationDrawerFragment)
                。getSupportFragmentManager()findFragmentById(R.id.navigation_drawer);
        mTitle =的getTitle();

        //设置抽屉。
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout)findViewById(R.id.drawer_layout));
    }

    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置){
        //更新替换片段的主要内容
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,PlaceholderFragment.newInstance(位置+ 1))
                。承诺();
    }

    公共无效onSectionAttached(INT编号){
        开关(数){
            情况1:
                mTitle =的getString(R.string.title_section1);
                打破;
            案例2:
                mTitle =的getString(R.string.title_section2);
                打破;
            案例3:
                mTitle =的getString(R.string.title_section3);
                打破;
        }
    }



    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        如果(!mNavigationDrawerFragment.isDrawerOpen()){

            。getMenuInflater()膨胀(R.menu.main,菜单);

       }
            返回super.onCreateOptionsMenu(菜单);

    }
    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){

        开关(item.getItemId())
        {
             案例R.id.add:

                意图add_tag​​ =新的意图(MainActivity.this,AddNote.class);
                startActivity(add_tag​​);

                返回true;
            }

        返回super.onOptionsItemSelected(项目);
    }

    公共静态类PlaceholderFragment扩展片段{

        私有静态最后弦乐ARG_SECTION_NUMBER =section_number标;

         公共静态PlaceholderFragment的newInstance(INT sectionNumber){
            PlaceholderFragment片段=新PlaceholderFragment();
            捆绑的args =新包();
            args.putInt(ARG_SECTION_NUMBER,sectionNumber);
            fragment.setArguments(参数);
            返回片段;
        }

        公共PlaceholderFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                                 捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
            返回rootView;
        }

        @覆盖
        公共无效onAttach(活动活动){
            super.onAttach(活动);
            ((MainActivity)活动).onSectionAttached(
                    。getArguments()调用getInt(ARG_SECTION_NUMBER));
        }
    }
}
 

解决方案

以及最后我得到通过做R&放大器解决方案; d。该解决方案只是增加一个行的列表元素的布局根。安卓descendantFocusability =blocksDescendants这的工作pretty的罚款在我的情况。bytheway谢谢大家。

I am using listview since long but this time i am facing a weird problem. that is i want to put setonitemclick listner on it but its not working i could not figure it out where is the issue. i have another xml for listview that shows the datas.

xml:

 <ListView android:id="@+id/lv"
android:dividerHeight="5dp"
android:choiceMode="singleChoice"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

code:

package algonation.com.noteup;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

   private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;
    private DBManager dbManager;

    private ListView listView;

    private SimpleCursorAdapter adapter;

    final String[] from = new String[] {DatabaseHelper.TAG_TITLE,
            DatabaseHelper.TAG_TIME,DatabaseHelper._ID,
            DatabaseHelper.ADD_TAG
             };



    final int[] to = new int[] {R.id.title,  R.id.time,R.id.textView4,R.id.fullnote};
    private View f;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbManager = new DBManager(this);
        dbManager.open();
        final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.alfa);
        final Button btnmain =(Button)findViewById(R.id.button4);
        final Button btn_addnote = (Button)findViewById(R.id.button2);
        final Button btn_TakePic =(Button)findViewById(R.id.button5);
        btn_addnote.setVisibility(View.INVISIBLE);
        btn_TakePic.setVisibility(View.INVISIBLE);
        btnmain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                v.startAnimation(animAlpha);
                Intent  i = new Intent(MainActivity.this,Camera.class);
                startActivity(i);
                btn_addnote.setVisibility(View.VISIBLE);
                btn_TakePic.setVisibility(View.VISIBLE);

                btnmain.setText("X");
            }
        });
        Cursor cursor = dbManager.fetch();

        listView = (ListView) findViewById(R.id.lv);

        adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.activity_view_record, cursor, from, to, 0);
        adapter.notifyDataSetChanged();
         listView.setAdapter(adapter);

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

            mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {

            getMenuInflater().inflate(R.menu.main, menu);

       }
            return super.onCreateOptionsMenu(menu);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
             case R.id.add:

                Intent add_tag =  new Intent(MainActivity.this,AddNote.class);
                startActivity(add_tag);

                return true;
            }

        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

         public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }
}
解决方案

well finally i got solution by doing R&D. the solution is just adding a line to root of the list element layout.android:descendantFocusability="blocksDescendants"this's working pretty fine in my case.bytheway thanks everybody.

这篇关于ListView控件setOnItemClickListner不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:15