3

この質問がここで何度か聞かれることは知っています。すべての回答を調べて、コードに実装しようとしましたが、結果は失敗しました。そのため、コードを実行するために新しい質問を投稿しました。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)
        {

        }
    }
}
4

3 に答える 3

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

これは、すべてのアクティブな要素に当てはまります。

于 2013-06-17T09:16:37.083 に答える
-1
  1. null でないかどうかを確認しm_VisitorListViewます (デバッグ モード)。
  2. インポート onItemClick を確認してください。AdapterView.OnItemClickListener
  3. それでもこの問題に直面している場合は、匿名で実装してみてください。

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

于 2013-06-17T08:23:48.073 に答える