android中适配器类中的单击事件

发布于 2024-12-24 16:24:50 字数 2138 浏览 1 评论 0原文

我有一个类数组适配器,其中有 3 个字段和一个复选框 我想在每个 textView 上显示单击事件,下面给出了我的代码如何在此类中触发单击事件。我很困惑

public class ItemReportAdapter extends ArrayAdapter<SalesForce>{
    private LayoutInflater li;

    public ItemReportAdapter(Context context,List<SalesForce> items){
        super(context,0,items);
        li=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        final SalesForce item=getItem(position);

        View v=convertView;
        if(v==null){
            v=li.inflate(R.layout.sfreportview,null);
        }

        final TextView idView=(TextView)v.findViewById(R.id.itemID);
        if(idView!=null){
            idView.setText(""+item.getUserID());
        }

        idView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                popup();                
            }

            private void popup() {

                //what to write in here so as i can be shifted to other  drill down 

            }
        });

        final TextView captionView=(TextView)v.findViewById(R.id.itemCaption);
        if(captionView!=null){
            captionView.setText(item.getName());
        }

        final TextView typeView=(TextView)v.findViewById(R.id.itemType);
        if(typeView!=null){
            typeView.setText(item.getUserType());
        }

        final CheckBox chkbox=(CheckBox)v.findViewById(R.id.checkboxes);

        chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
                        popup();
                }

                private void popup() {
                    // TODO Auto-generated method stub

                }
            });
        return v;
    }
    public long getItemId(int position){
        return getItem(position).getUserID();
    }
    public boolean hasStableIds(){
        return true;
    }

我想在 idView 上有点击事件我该怎么做? 任何帮助将不胜感激!1:)

I have a class Array Adapter in which i have 3 field and one check box
i wanna shw click events on each textView , my code is given Below how to fire click event in this class . i am confused

public class ItemReportAdapter extends ArrayAdapter<SalesForce>{
    private LayoutInflater li;

    public ItemReportAdapter(Context context,List<SalesForce> items){
        super(context,0,items);
        li=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        final SalesForce item=getItem(position);

        View v=convertView;
        if(v==null){
            v=li.inflate(R.layout.sfreportview,null);
        }

        final TextView idView=(TextView)v.findViewById(R.id.itemID);
        if(idView!=null){
            idView.setText(""+item.getUserID());
        }

        idView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                popup();                
            }

            private void popup() {

                //what to write in here so as i can be shifted to other  drill down 

            }
        });

        final TextView captionView=(TextView)v.findViewById(R.id.itemCaption);
        if(captionView!=null){
            captionView.setText(item.getName());
        }

        final TextView typeView=(TextView)v.findViewById(R.id.itemType);
        if(typeView!=null){
            typeView.setText(item.getUserType());
        }

        final CheckBox chkbox=(CheckBox)v.findViewById(R.id.checkboxes);

        chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
                        popup();
                }

                private void popup() {
                    // TODO Auto-generated method stub

                }
            });
        return v;
    }
    public long getItemId(int position){
        return getItem(position).getUserID();
    }
    public boolean hasStableIds(){
        return true;
    }

i wanna have click event on idView how can i do that ?
Any help would be appreciated Thanx!1 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝梦月影 2024-12-31 16:24:50

我假设这个适配器用于 ListView 内的视图。我看到你还有一个复选框。 Android 确实不喜欢在 ListView 中拥有可以单击或聚焦的项目。你可以这样做,但你真的不应该这样做。如果您有一个复选框,那么您确实想使用 ListView 的“选择模式”。请参阅此示例 。如果您想对项目执行其他操作,可以长按。

I am going to assume this adapter is for views inside a ListView. I see that you also have a Checkbox. Android really doesn't like having items that can be clicked or focused inside ListViews. You can do it but you really shouldn't. If you have a checkbox then you really want to use the "choice mode" of the ListView. See this example. If you want to perform other actions on the items you can use a long press.

凯凯我们等你回来 2024-12-31 16:24:50

您需要为整行设置focus属性“false”,然后它应该可以工作。

You need to set focus property "false" for the entire row, then it should work.

中性美 2024-12-31 16:24:50

你应该用holder制作listview,我认为你的点击事件应该可以工作。但这是您可以尝试的另一个代码..我写了我的。但你可以遵循它。

class FeedListAdapter extends BaseAdapter {
    ArrayList<LatestFeedInfo> m_list = new ArrayList<LatestFeedInfo>();

    public FeedListAdapter(ArrayList<LatestFeedInfo> list) {
        m_list = list;
    }

    @Override
    public int getCount() {
        return m_list.size();
    }

    @Override
    public Object getItem(int position) {
        return m_list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        holder = new ViewHolder();
        if (rowView == null) {
            LayoutInflater li = getLayoutInflater();
            rowView = li.inflate(R.layout.feed_item, null);
            rowView.setTag(holder);

            holder.txtFeedUrl = (TextView) rowView

        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        final LatestFeedInfo feed = m_list.get(position);
        holder.txtFeedUrl.setText(Html.fromHtml(feed.Activity));

        holder.txtFeedUrl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //here is the click event
            }
        });
        return rowView;
    }

}

public static class ViewHolder {
    TextView txtFeedUrl;
}

You should make listview with holder and i think your click event should work . But here is another code you can try.. I wrote mine. But you can follow it.

class FeedListAdapter extends BaseAdapter {
    ArrayList<LatestFeedInfo> m_list = new ArrayList<LatestFeedInfo>();

    public FeedListAdapter(ArrayList<LatestFeedInfo> list) {
        m_list = list;
    }

    @Override
    public int getCount() {
        return m_list.size();
    }

    @Override
    public Object getItem(int position) {
        return m_list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View rowView = convertView;
        holder = new ViewHolder();
        if (rowView == null) {
            LayoutInflater li = getLayoutInflater();
            rowView = li.inflate(R.layout.feed_item, null);
            rowView.setTag(holder);

            holder.txtFeedUrl = (TextView) rowView

        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        final LatestFeedInfo feed = m_list.get(position);
        holder.txtFeedUrl.setText(Html.fromHtml(feed.Activity));

        holder.txtFeedUrl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //here is the click event
            }
        });
        return rowView;
    }

}

public static class ViewHolder {
    TextView txtFeedUrl;
}
蓝咒 2024-12-31 16:24:50

请参阅以下类似的答案: https://stackoverflow.com/a/8237375/607968

Problem with click events insdie适配器视图项目的一个缺点是您将不知道单击了哪个子项目。您感兴趣的是 getTag()setTag() 函数,它们使您能够知道单击了哪个视图。请注意我们如何将位置设置为视图的标记,并在 onClick 中检索位置。对于处理适配器视图项内的任何子项的单击事件也是如此。

refer the follwing answer which is similar : https://stackoverflow.com/a/8237375/607968

Problem with click events insdie adapter view items is that you will not know, which child item was clicked. What you are interested in is the getTag() and setTag() functions, which enable you to know which view is clicked. Notice how we set the position as the tag for the view, and retrieve the position inside onClick. The same is true for handling click events for any child item inside an adapter view item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文