简体   繁体   中英

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

I have a very simple app:

activity_main.xml

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

item.xml

<TextView />

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

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.

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

  • You need to inflate that layout in getview
  • find your views(TextView) and apply changes then return that view

     @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

You should call super.getView() at the beginning of your own getView() method:

@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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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