简体   繁体   English

如何在自定义适配器Android Studio Java中将标签设置为一行

[英]How to set a tag to a row in a custom adapter Android Studio Java

I am trying to set a tag to the row (or at least something in the row, preferably the row though). 我正在尝试为行设置标签(或至少在行中设置某些内容,但最好是该行)。 Then when I click on the row, I can get the tag. 然后,当我单击该行时,可以获得标签。 I thought that this is what I have to do, but the tag is always null, regardless of what I tap on. 我以为这是我要做的,但是无论我点击什么,该标记始终为null。

My code example shows my attempt at setting a tag to a textview, as I have no idea how to set it to the row. 我的代码示例显示了我尝试将标签设置为textview的尝试,因为我不知道如何将其设置为该行。

This is my getView code in my customAdapter class 这是我的customAdapter类中的getView代码

int counter = 0;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater = LayoutInflater.from(getContext());

    View customView = layoutInflater.inflate(R.layout.my_row, parent, false);

    ViewHolder holder = new ViewHolder();

    holder.name = (TextView)customView.findViewById(R.id.nameTextView);
    holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView);

    holder.name.setText(allNames.get(position));
    holder.picture.setImageResource(allPictures.get(position));

    //This does not work ---> holder.name.setTag(counter);

    counter ++;
    return customView;
}

This is my onClickListener in a different class 这是我在另一个类中的onClickListener

listVIew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {

           Toast.makeText(getApplicationContext(), "tag is " + view.getTag(), Toast.LENGTH_SHORT).show();

        }
    });

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Try this, 尝试这个,

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());

        View customView = layoutInflater.inflate(R.layout.my_row, parent, false);

        ViewHolder holder = new ViewHolder();

        holder.name = (TextView)customView.findViewById(R.id.nameTextView);
        holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView);

        holder.name.setText(allNames.get(position));
        holder.picture.setImageResource(allPictures.get(position));

        //This does not work ---> holder.name.setTag(counter);

        customView.setTag(Integer.valueOf(counter));

        counter ++;
        return customView;
    }

Try this: 尝试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if(convertView == null) {

        // inflate the layout
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        convertView = layoutInflater.inflate(R.layout.my_row, parent, false);

        // set up the ViewHolder
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.nameTextView);
        holder.picture = (ImageView) convertView.findViewById(R.id.pictureImageView);

        // store the holder with the view.
        convertView.setTag(holder);

    } else {
        // we've just avoided calling findViewById() on resource everytime
        // just use the viewHolder
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText(allNames.get(position));
    holder.picture.setImageResource(allPictures.get(position));
    holder.name.setTag(counter);

    counter++;

    return convertView;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自定义适配器与ListView的特定行上的按钮进行交互(Java Android) - How to interact with a button on a certain row of a ListView with custom adapter (Java Android) 在 Android Studio 中使用自定义适配器设置图像源 - Using a custom adapter to set an image source in Android Studio Android:具有基本适配器的自定义列表视图:如何在点击列表器中设置行项目? - Android: custom List view with base adapter: How to set on click listner for row items? 如何为自定义布局适配器 android studio 创建搜索过滤器 - How to create a search filter for custom layout adapter android studio 如何在一个适配器Android工作室Java中使用两个arrayList? - How to use two arrayList in one adapter Android studio Java? Android:如何在自定义适配器中设置侦听器并获取图像按钮单击的位置 - Android : how to set listener and get position of imagebutton click in a custom adapter 如何使用两个以上的值填充自定义列表适配器?-JAVA Android - How to populate a Custom List Adapter with more than 2 values ?- JAVA android 在适配器中获取活动-Java中的Android Studio - Get an Activity in a Adapter - Android studio in Java Android Java-将自定义适配器与ListView一起使用选择一个复选框,并同时选择另一个行项目 - Android Java - Using custom adapter with ListView Select a checkbox and another row item also selected Android自定义适配器如何工作? - How Android Custom Adapter works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM