Android:LayoutInflater 和 findViewById 问题

标签 android android-inflate

我试图在循环中使用 LayoutInflater,但每次实例化其中一个时,我需要修改至少 2 个在每个实例化中找到的 TextView。我目前拥有的东西适本地实例化并添加膨胀的 xml View ,就像我想要的那样,但结果是只有第一个 instatiaton 的资源被修改,因此它被多次覆盖。

            LayoutInflater inflater;
            TextView eTitle;

            for(String[] episode : episodes) {

                //Log.d("Episodes", episode[0] + " / " + episode[2]);
                inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.episode_list_item, listView);
                eTitle = (TextView) findViewById(R.id.episode_list_item_title);

                eTitle.setText(episode[2]);
            }

我的列表的示例输出:

  • 04 - 第 04 集
  • 剧集标题
  • 剧集标题
  • 剧集标题

它应该看起来像这样:

  • 01 - 第 01 集
  • 02 - 第 02 集
  • 03 - 第 03 集
  • 04 - 第 04 集

我需要做什么来确保我从我的膨胀布局中获得相应的唯一 TextView ID?

最佳答案

为什么不使用 ListView 呢?

 LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(String[] episode : episodes) {
    View view = inflater.inflate(R.layout.episode_list_item, null);
    mTitle = (TextView) view.findViewById(R.id.episode_list_item_title);
    mTitle.setText(episode[2]);

listView.addView(view);

}

关于Android:LayoutInflater 和 findViewById 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8956002/

相关文章:

android - Google Map APIv2 实现,二进制 XML 文件行 #6 : Error inflating class fragment

android - 自定义 ListView, getView 方法

android - 膨胀包含 PDFViewCtrl 的 View

java - Android DialogFragment 和在终止/恢复应用程序后无法启动 Activity ComponentInfo

android - 什么是 onCreateOptionsMenu(菜单菜单)

android - 搜索 View 工具栏关闭图标

android - 错误 :Failed to complete Gradle execution.

java - 访问 DownloadProvider 时避免 SecurityException

javascript - 使用 WebView Javascript Android 进行网站登录

java - 在 RecyclerView 中膨胀两种类型的 .XML