0

我有一个我无法理解如何解决的问题。我有一个movie_item.xml,其中包含一个textView 和一个按钮作为组件。我的自定义 ArrayAdapter 获取 ArrayLists,其中包含 10 个项目,这意味着我的 ArrayAdapter 应该在我的 ListView 中绘制布局 10 次。问题是当 ArrayAdapter 第二次使用布局时,我的应用程序 textView 变为空。

这是我的代码:

CustomArrayAdapter.java

package com.example.labb4;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomArrayAdapter extends ArrayAdapter {
ArrayList<Integer> movieIDList;
public CustomArrayAdapter(Context context, ArrayList titleList, ArrayList idList) {
    super(context, 0, titleList);
    movieIDList = idList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Om vyn används första gången
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false);
    }
    String title = getItem(position).toString();
    int movieID = movieIDList.get(position);
    TextView text = (TextView) convertView.findViewById(R.id.TitleTextView);
    Log.d("movies","text: "+text+"");
    text.setText(title);
    text.setId(movieID);
    Button button = (Button) convertView.findViewById(R.id.button);
    Log.d("movies","button "+button+"");
    button.setText("Lägg till");
    //Returnera hela Vy
    return convertView;
 }
}

这是 res 文件夹中的 movie_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/TitleTextView"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="TextView" />

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

如您所见,我使用 inflate() 来处理布局文件,并且布局文件已经包含了我工作所需的所有组件。但是,当我运行程序时,输出是

在此处输入图像描述

繁荣,textView 第二次变为空。我的应用程序崩溃了,因为 setText on null 是不可能的。但最神秘的部分是,如果我从 CustomArrayAdapter 上的 getView 中删除 textView 代码并只打印出按钮,

   public View getView(int position, View convertView, ViewGroup parent) {
    //Om vyn används första gången
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false);
    }
    String title = getItem(position).toString();
    int movieID = movieIDList.get(position);
    Button button = (Button) convertView.findViewById(R.id.button);
    Log.d("movies"," button "+button);
    button.setText("Lägg till");
    //Returnera hela Vy
    return convertView;
}

在此处输入图像描述 按钮永远不会为空!

我只是不明白是什么让 TextView 为空?

如果我做一个 if 语句来检查 test 是否为空,我的应用程序就可以工作,

     TextView text = (TextView) convertView.findViewById(R.id.TitleTextView);
    if(text!=null){
        Log.d("movies","text: "+text+"");
        text.setText(title);
        text.setId(movieID);
    }

但我认为这不是一个正确的解决方案,对我来说感觉就像..解决了效果,而不是问题的原因..因为 Log.d 显示 TextView 为 null 超过 10 次..

在此处输入图像描述

那么...为什么我的 TextView 为空?我该如何解决这个问题?我已经尝试过清理和重建项目。重新启动 android studio 也没有帮助我。

4

0 回答 0