在 Android 的自定义适配器中设置 Textview 文本时出现问题

2024-02-20

我无法在其中设置 textview 的 setText 属性getView()自定义适配器的方法。

我已经尝试过以下解决方案,但它对我不起作用:

  • 解决方案一 https://stackoverflow.com/questions/5612844/how-to-get-a-list-item-position-by-clicking-the-button-inside-it
  • 解决方案二 https://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-click
  • 解决方案三 https://stackoverflow.com/questions/22602517/custom-list-adapter-not-binding-values-to-textview

Listview.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imgLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:divider="@null"
    android:dividerHeight="5dp"
    android:scrollbars="none" />

</RelativeLayout>

下面是我的 keep_resing xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customRowLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/Btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/thumb_down_disabled" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/resing"
            android:textColor="#357EC7"
            android:textSize="10sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lyricsTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

        <RelativeLayout
            android:id="@+id/parentLayout"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:background="@drawable/playing_view"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/helloTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5dp"
                android:singleLine="true"
                android:text="Hello"
                android:textColor="#357EC7"
                android:textSize="14sp" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:background="@drawable/volume" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/keepBtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/thumb_up_disabled" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/keep"
            android:textColor="#357EC7"
            android:textSize="10sp" />
    </LinearLayout>
</RelativeLayout>

下面是我的适配器代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.stream.Position;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.PowerManager;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class KeepReSingAdapter extends BaseAdapter implements OnClickListener {

    /*********** Declare Used Variables *********/
    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;
    public Resources res;
    SongCue tempValues = null;
    int i = 0;

    private ArrayList<SongCue> songCue;
    private int cueIndex = 0;
    private String selectedSongId;
    private MediaPlayer mMediaPlayer;
    ViewHolder holder;
    View vi;
    boolean right_button_flag, left_button_flag;
    SessionManager session;

    // int left_button_flag;

    /************* CustomAdapter Constructor *****************/
    public KeepReSingAdapter(Activity a, ArrayList d, Resources resLocal) {

        /********** Take passed values **********/
        activity = a;
        data = d;
        res = resLocal;

        /*********** Layout inflator to call external xml layout () ***********/
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        selectedSongId = SessionManager.getInstance(
                activity.getApplicationContext()).getString(
                AppConstants.SONG_ID);

        right_button_flag = false;
        left_button_flag = false;

        // Session class instance
        session = new SessionManager(activity.getApplicationContext());

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder {

        public TextView lyricsTxt, helloTxt;
        // public TextView text1;
        public ImageButton leftBtn, rightBtn;
        public RelativeLayout parentLayout;
        public LinearLayout rowLayout;

        // public TextView textWide;
        // ImageView image;

    }

    /****** Depends upon data size called for each row , Create each ListView row *****/
    public View getView(final int position, View convertView, ViewGroup parent) {

        vi = convertView;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.keep_resing, null);

            holder = new ViewHolder();
            holder.lyricsTxt = (TextView) vi.findViewById(R.id.lyricsTxt);
            holder.helloTxt = (TextView) vi.findViewById(R.id.helloTxt);

            holder.leftBtn = (ImageButton) vi.findViewById(R.id.reSingBtn);
            holder.rightBtn = (ImageButton) vi.findViewById(R.id.keepBtn);
            holder.parentLayout = (RelativeLayout) vi
                    .findViewById(R.id.parentLayout);
            holder.rowLayout = (LinearLayout) vi
                    .findViewById(R.id.customRowLayout);





            vi.setTag(holder);

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

        }

        if (data.size() <= 0) {



        } else {
            /***** Get each Model object from Arraylist ********/
            tempValues = null;
            tempValues = (SongCue) data.get(position);


            holder.lyricsTxt.setText(tempValues.getLyric());

            List<Song> AllSong = SplashScreen_Activity.songs;
            if (selectedSongId != null) {
                for (Song tempsong : AllSong) {
                    if (tempsong.songId.equals(selectedSongId)) {
                        songCue = (ArrayList<SongCue>) tempsong.songCues.songCue;
                    }
                }
            } else {

            }




            holder.parentLayout.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {

                Toast.makeText(activity.getApplicationContext(),
                        " " + position, Toast.LENGTH_SHORT).show();

                     holder.helloTxt.setText("Test");

                    Toast.makeText(activity.getApplicationContext(),
                            holder.helloTxt.getText().toString(),
                            Toast.LENGTH_LONG).show();


                   }
               });



        }
        vi.setOnClickListener(new OnItemClickListener(position));
        return vi;
    }


    @Override
    public void onClick(View v) {
        Log.v("CustomAdapter", "=====Row button clicked=====");

    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener implements OnClickListener {
        private int mPosition;

        OnItemClickListener(int position) {
            mPosition = position;
        }

        @Override
        public void onClick(View arg0) {


        }
    }

}

问题是,我无法将文本视图文本更改为“测试”。

Note:如果我通过执行“来看到 Toast 的价值”holder.helloTxt.getText().toString()“它向我展示”Test".

我不知道为什么会出现这个问题。任何帮助将不胜感激。

Thanks


最后我得到了我的问题的解决方案,并猜测这是代码中的一个小更新。

我将代码更新为:

final ViewHolder holder; 

inside getView() of CustomAdapter而不是在全球范围内声明它。

再次感谢大家的回答。

它帮助我详细研究我的代码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android 的自定义适配器中设置 Textview 文本时出现问题 的相关文章

  • 检索子值 -firebase-

    System out println ref child email protected cdn cgi l email protection child email 我正在尝试获取 child 的值 但我始终获取该值的 URL 当我尝试使
  • 使用 APDU 命令的有效 NFC 读取比特率是多少?

    我目前正在使用 Android IsoDep trancieve 函数发送和接收累计 1628 字节的数据 该函数分布在 35 个 APDU 命令 选择应用程序 身份验证 读取 中 字节计数包括返回的 MAC 校验和以及由 transcie
  • Android Studio - 错误:未捕获翻译错误:com.android.dx.cf.code.SimException:本地 0001:无效

    我刚刚使用 Android Studio 设置了一台新计算机 并从 bitbucket 导入了我的项目 问题是我现在在尝试构建项目时遇到此错误 信息 Gradle 任务 app clean app generateDebugSources
  • SQLite (Android):使用 ORDER BY 更新查询

    Android SQLite 我想要在 myTable 中的其他行之间插入行在android中使用SQLite 为此 我尝试增加从第 3 行开始的所有行的 id 这样 我就可以在位置 3 处插入新行 myTable 的主键是列 id 表中没
  • Android Lollipop prepareAsync() 需要很长时间才能返回

    在 Samsung Galaxy Note 4 上的 Android Lollipop 几周前刚刚从 4 4 4 更新 上 prepareAsync 几乎需要 20 秒来加载实时流 在 4 4 4 上 只需要 2 3 秒 并且没有错误 见下
  • 如何使用闹钟音量和 setAudioAttributes 来播放铃声?

    所以我正在尝试了解音频属性 这是我到目前为止所拥有的 alarm getSound will return a proper URI to pick a ringtone Ringtone tone RingtoneManager getR
  • Android - 当不在栏顶部时推送通知空白

    我在使用 Android 推送通知时遇到一个小问题 如果有 3 个通知 并且只有其中一个显示标题和消息 位于酒吧顶部的那个 如果有人知道可能是什么问题 请告诉我 请参阅此链接上的图像 这就是我接收通知的方式http postimg org
  • 如何在 Android 上创建 YouTube 的双击手势?

    我在 Android 上有 exoplayer 的应用程序 我已经创建了 youtube双击手势用动画向前或向后跳跃 10 秒 如何在双击时创建具有波纹效果的半圆 像这样 这个怎么做 我也想实现这样的功能 所以我自己编写了它来 复制 You
  • Android-工具栏中的SearchView

    我只想在我的应用程序中添加 searchview 但我不想搜索任何东西 只是我想要用户输入的查询 到目前为止 我尝试了这段代码 但是当我运行我的应用程序时它崩溃了 Update 我尝试了这个 但即使我的应用程序崩溃了 main menu x
  • SQLite支持android的数据类型有哪些

    谁能告诉我 SQLITE 中支持 ANDROID 的数据类型列表 我想确认 TIME 和 DATE 数据类型 这里有一个list http www sqlite org datatype3 htmlSQLite 的数据类型 支持时间和日期间
  • Android Studio - 如何关闭“单词‘word’中的拼写错误?”

    当命名变量或给出字符串参数时 Android Studio 似乎对我如何标记事物有问题 有办法把它关掉吗 是的 打开Preferences gt Editor gt Inspections gt Spelling gt 关闭Typo并按OK
  • 从多个选项卡中的编辑文本字段获取文本

    我正在尝试创建一个使用选项卡作为输入表单的 Android 应用程序 基本上 我希望对其进行设置 以便用户可以在一个选项卡上输入一些信息 然后提交该信息 或者转到另一个选项卡并输入更多信息 然后从两个选项卡提交信息 我正在使用操作栏和片段来
  • Android Web Intent 问题

    G day 免责声明 我不是 Android 开发人员 我正在对我所描述的问题进行质量检查 我用来描述这个问题的技术术语可能是错误的 我正在测试一个 Android 应用程序 该应用程序在其清单中描述它可以使用 type 的地址处理 Web
  • Google 移动广告和 Kindle Fire

    我最近用 Google 移动广告替换了 AdMob 库 对此我有一个疑问 广告会出现在 Amazon Kindle Fire 设备上吗 我问这个是因为我知道 Google 移动广告依赖于 Google Play 服务 所以我有点困惑 Goo
  • jar 中的 apklib 有什么优点?

    我正在关注这个问题 https stackoverflow com questions 6059502 whats the difference between apklib and jar files但它并没有完全回答我的问题 jar 中
  • 如何在Android Compose中使用otf类型字体文件?

    我正在学习使用 Android Jetpack Compose 现在我有一个正则 otf字体文件在资产 字体 我想在文本中使用它 java lang RuntimeException Font asset not found commonu
  • “无法实例化活动”错误

    我的一个 Android 应用程序拥有大约 100 000 个用户 每周大约 10 次 我会通过 Google 的市场工具向我报告以下异常情况 java lang RuntimeException Unable to instantiate
  • Android:无法发送http post

    我一直在绞尽脑汁试图弄清楚如何在 Android 中发送 post 方法 这就是我的代码的样子 public class HomeActivity extends Activity implements OnClickListener pr
  • putFragment() - 片段 x 当前不在 FragmentManager 中

    上面的标题被问了很多次 但答案似乎与FragmentStatePagerAdapter这与我的问题无关 我正在使用该方法putFragment Bundle String Fragment 直接地 The 安卓文档 http develop
  • 异步更新后更新Android Listview

    我正在将 HTTP 调用从同步调用转换为异步调用 由于连接在后台运行 因此当我最初设置列表适配器时 数据不存在 如何在 HTTP 调用后更新列表适配器 我尝试了一些方法 例如在数据发送回之前不设置适配器并再次设置适配器 但没有任何效果 这是

随机推荐