不会触发/调用ListView setOnItemClickListener

时间:2013-06-17 08:11:10

标签: java android android-listview

我知道这个问题在这里被多次询问过,我会仔细阅读所有答案并尝试在我的代码中实现它,但结果却失败了。这就是为什么我发布了一个新问题来运行我的代码。每当我想触发listView.setOnItemClickListener(this)时,问题就很简单。它没有被解雇。我尝试了stackoverflow中给出的每个建议,但无法解决问题。

我使用的代码是

visitor_list_fragment.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/node_name_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textIsSelectable="false"
        android:textSize="20sp" />

    <ListView
        android:id="@+id/visitor_list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:cacheColorHint="#00000000"
        android:dividerHeight="5dp"
        android:listSelector="#00000000"
        android:scrollingCache="true"
        android:smoothScrollbar="true" >
    </ListView>

</LinearLayout>

visitor_list_item.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/photo_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_marginLeft="5dp"
        android:contentDescription="@string/image_name"
        android:focusable="false" />

    <TextView
        android:id="@+id/profile_info_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_marginLeft="5dp"        
        android:focusable="false"
        android:textColor="@android:color/black"
        android:textIsSelectable="false"
        android:textSize="20sp" />

</LinearLayout>

我调用onItemClickListener

的代码
public class VisitorListFragment extends Fragment implements OnItemClickListener
{   
    private TextView                m_NodeName;

    private ListView                m_VisitorListView;

    private VisitorListAdapter      m_VisitorNodeListAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view   = inflater.inflate(R.layout.visitor_list_fragment, null);

        m_NodeName = (TextView)view.findViewById(R.id.node_name_textView);

        m_VisitorNodeListAdapter = new VisitorListAdapter(getActivity().getApplicationContext());        
        m_VisitorListView = (ListView)view.findViewById(R.id.visitor_list_view);

        // Displaying header & footer in the list-view
        TextView header = (TextView) getActivity().getLayoutInflater().inflate(R.layout.list_headfoot, null); 
        header.setBackgroundResource(R.drawable.header_footer_img);
        m_VisitorListView.addHeaderView(header, null, false);

        TextView footer = (TextView) getActivity().getLayoutInflater().inflate(R.layout.list_headfoot, null);     
        footer.setBackgroundResource(R.drawable.header_footer_img);
        m_VisitorListView.addFooterView(footer, null, false);

        m_VisitorListView.setAdapter(m_VisitorNodeListAdapter);     
        m_VisitorListView.setOnItemClickListener(this);

        return view;        
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {       
         String nodeName = m_VisitorNodeListAdapter.getNodeName(position);
         System.out.println(nodeName);
    } 
}

AdapterClass

public class VisitorListAdapter extends BaseAdapter
{
    private HashMap<String, String>     m_ProfileImagePath;

    private HashMap<String, String>     m_ProfileInfo;

    private ArrayList<String>           m_NodeName;

    private LayoutInflater              m_Inflater=null;

    public VisitorListAdapter(Context context) 
    {
        super();
        m_ProfileImagePath = new HashMap<String, String>();
        m_ProfileInfo = new HashMap<String, String>();
        m_NodeName = new ArrayList<String>();
        m_Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getItem(int arg0) 
    {
        return null;
    }

    public int getCount() 
    {
        return m_NodeName.size();
    }

    public long getItemId(int position) 
    {
        return position;
    }

    public boolean isEnabled(int position) 
    {
        return false;
    }

    public String getNodeName(int position) 
    {
        return m_NodeName.get(position);
    }

    public class ViewHolder
    {
        TextView    profileInfoTextView;

        ImageView   profileImageView;
    }

    public void addProfileInfo(String nodeName, String profileInfo) 
    {       
        boolean found = false;
        for(int i = 0; i<m_NodeName.size(); i++)
        {
            if(nodeName.equals(m_NodeName.get(i)))
            {
                found = true;
            }
        }       

        if(found == false)
        {
            m_NodeName.add(nodeName);           
            m_ProfileInfo.put(nodeName, profileInfo);
            ChordActvityManager.getSingletonObject().setNodeNameList(m_NodeName);
        }       
    }   

    public void addProfileImagePath(String nodeName, String profileInfoImagePath) 
    {       
        m_ProfileImagePath.put(nodeName, Utilities.CHORD_FILE_PATH + "/" + profileInfoImagePath);
        notifyDataSetChanged();
    }

    public void removeNode(String nodeName) 
    {       
        for(int i = 0; i<m_NodeName.size(); i++)
        {
            if(nodeName.equals(m_NodeName.get(i)))
            {
                m_NodeName.remove(i);
                m_ProfileInfo.remove(nodeName);
                m_ProfileImagePath.remove(nodeName);
            }
        }       
        notifyDataSetChanged();
        ChordActvityManager.getSingletonObject().setNodeNameList(m_NodeName);
    }

    public void clearAll() 
    {
        m_ProfileImagePath.clear();
        m_NodeName.clear();
        m_ProfileInfo.clear();
        notifyDataSetChanged();

        ChordActvityManager.getSingletonObject().setNodeNameList(m_NodeName);
    }   

    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;

        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = m_Inflater.inflate(R.layout.visitor_list_item, null);                             

            holder.profileImageView = (ImageView)convertView.findViewById(R.id.photo_view);
            holder.profileInfoTextView = (TextView)convertView.findViewById(R.id.profile_info_textview);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        // Put the code in an async task
        new ImageLoaderTask(position, holder).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

        return convertView;
    }   

    class ImageLoaderTask extends AsyncTask<URL, Integer, Long>
    {
        private int         position;

        private ViewHolder  holder;

        ImageLoaderTask(int position, ViewHolder holder)
        {
            this.position =  position;
            this.holder = holder;
        }

        @Override
        protected void onPreExecute()
        {   
            String loadPath = m_ProfileImagePath.get(m_NodeName.get(position));
            Bitmap bitmap = BitmapFactory.decodeFile(loadPath); 

            if(bitmap != null)
            {               
                holder.profileImageView.setImageBitmap(bitmap);
            }

            holder.profileInfoTextView.setText(m_ProfileInfo.get(m_NodeName.get(position)));
        }

        @Override
        protected Long doInBackground(URL... urls)
        {                       
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... progress)
        {

        }

        @Override
        protected void onPostExecute(Long result)
        {

        }
    }
}

3 个答案:

答案 0 :(得分:6)

  public boolean isEnabled(int position) 
    {
        return false;
    }

对于所有活跃元素都应如此!

答案 1 :(得分:0)

尝试删除

android:focusable="false"

 android:textIsSelectable="false" 

来自listView_item.xml档案......

答案 2 :(得分:-1)

  1. 检查m_VisitorListView是否为空(调试模式)。
  2. 检查导入onItemClick,应为AdapterView.OnItemClickListener
  3. 如果您仍然面临此问题,请尝试匿名实施:

    m_VisitorListView .setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } } );