简体   繁体   English

需要帮助弄清楚为什么我不能使用自定义ArrayAdapter更改TextView文本

[英]Need help figuring out why I cannot change TextView text using a custom ArrayAdapter

I have a very simple app: 我有一个非常简单的应用程序:

activity_main.xml activity_main.xml中

<RelativeLayout>
    <ListView android:id="@+id/emoticon_listview" />
</RelativeLayout>

item.xml item.xml

<TextView />

MainActivity.java MainActivity.java

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int start = 0x1F004;
    int end = 0x1F65C;

    ListView listView = (ListView)findViewById(R.id.emoticon_listview);
    Integer[] emoticon_array = new Integer[end - start]

    for(int codepoint=start, i=0; codepoint<end; codepoint++, i++){
        emoticon_array[i] = codepoint;
    }

    CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(this, R.layout.item, emoticon_array);
    listView.setAdapter(customArrayAdapter);
}

CustomArrayAdapter.java CustomArrayAdapter.java

public class CustomArrayAdapter extends ArrayAdapter {
    public customArrayAdapter(Context context, int resource, Object[] objects){
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        TextView textView = (TextView) convertView;

        if(textView != null){
            textView.setText(new String(Characters.toChars(Integer.parseInt(textView.getText().toString))));
            // -or-
            textView.setText("Hello, world!");
            // -or-
            textView.setText("whatever ...");
        }
    }

    return super.getView(position, convertView, parent);
}

The problem is that no matter what I put in the textView.setText() of the overridden getView() of my CustomArrayAdapter it always displays what is in the original emoticon_array. 问题是,不管我放在textView.setText()的重载的getView()我的CustomArrayAdapter它始终显示的是在原来emoticon_array。

I even tried this: 我什至尝试了这个:

return super.getView(position, textView, parent);

But that didn't do anything either. 但这也无济于事。

What am I doing wrong? 我究竟做错了什么?

Thanks :) 谢谢 :)

You are using a custom layout R.layout.item so 您正在使用自定义布局R.layout.item因此

  • You need to inflate that layout in getview 您需要在getview中膨胀该布局
  • find your views(TextView) and apply changes then return that view 找到您的视图(TextView)并应用更改,然后返回该视图

     @Override public View getView(int position, View convertView, ViewGroup parent){ TextView textView; if(convertView == null){ convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false); // to reuse view next time } // find your text view textView = (TextView)convertView.findViewById(R.id.yourTextViewID); textView.setText(getItem(position).toString()); //textView.setText("Hello, world!"); // -or- //textView.setText("whatever ..."); // return inflated view return convertView; } 

Note : Don't use Object array and use getItem to fetch the item using position to display the item from array 注意:请勿使用Object数组,而应使用getItem来获取position的项,以显示数组中的项

You should call super.getView() at the beginning of your own getView() method: 您应该在自己的getView()方法的开头调用super.getView()

@Override
public View getView(int position, View convertView, ViewGroup parent){
    TextView textView = (TextView) super.getView(position, convertView, parent);
    textView.setText("Hello, world!");
    // -or-
    textView.setText("whatever ...");

    return textView;
}

This allows you to take advantage of the logic to recycle convertView if it is not null . 如果它不是null这使您可以利用逻辑来回收convertView Then you can make whatever changes you wish and return the resulting view directly. 然后,您可以进行所需的任何更改,然后直接返回结果视图。

Note that I have removed this line: 请注意,我删除了这一行:

    textView.setText(new String(Characters.toChars(Integer.parseInt(textView.getText().toString))));

First of all, it is missing a pair parentheses which causes a compiler error. 首先,它缺少一对括号,这会导致编译器错误。 More importantly, this does a lot of work for not visible benefit. 更重要的是,这样做会带来很多好处,而看不到好处。 The end result is either an error, if the original text is not numerical, or setting the text back to the original value. 如果原始文本不是数字,则最终结果是错误,或者将文本设置回原始值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM