【问题标题】:Android: Why ArrayAdapter Displays null at first?Android:为什么 ArrayAdapter 一开始显示 null?
【发布时间】:2011-01-21 11:43:19
【问题描述】:

当我填充一个 ArrayAdapter 时,首先屏幕可见部分中的行将是空白的,除了第一行,这将是一个不希望的结果。然后当我向下滚动然后返回时,所有屏幕都会正确设置...请有人告诉我为什么会发生这种行为?

代码:

        homeListView = (ListView) findViewById(R.id.home_list);
        homeArrayAdapter = new HomeListArrayAdapter(this, R.id.home_list,
                homeList);
        homeListView.setAdapter(homeArrayAdapter);

下一部分作为类扩展 ArrayAdapter,一个内部类到 Activity:

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.home, null);
        }

        Home item = (homeList).get(position);
        if (item != null) {
            if (item.type_text == Home.SIMPLE_SHOUT) {
                TextView tv1 = (TextView) findViewById(R.id.hTextView01);
                if (tv1 != null) {
                    tv1
                            .setText(String.format("   %s\t:",
                                    item.friend_name));
                    final String item_friend_id_01 = String
                            .valueOf(item.users_id);
                    tv1.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Toast toast = Toast.makeText(Tranz.this,
                                    item_friend_id_01, Toast.LENGTH_LONG);
                            toast.show();
                        }
                    });
                }
                TextView tv2 = (TextView) findViewById(R.id.hTextView02);
                if (tv2 != null) {
                    tv2.setText(String.format("\t%s", item.message));
                    tv2.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        }
                    });

                }
                TextView tv3 = (TextView) findViewById(R.id.hTextView03);
                if (tv3 != null) {
                    tv3.setVisibility(0);
                    tv3.setText(null);
                    tv3.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        }
                    });

                }
                ImageView imageView = (ImageView) findViewById(R.id.file_image);
                if (imageView != null) {
                    imageView.setBackgroundDrawable(null);
                }
            } else if (item.type_text == Home.FILE_SHOUT) {
                TextView tv1 = (TextView) findViewById(R.id.hTextView01);

代码被截断,所以整个代码没有出现在这里。对此我很抱歉。在这里,我尝试将 Home 类的对象中的值放入两个 textView 和一个 ImageView 中,这可能会因变量 item.type_text 返回的值而异

另外,如果有人能告诉我另一种最佳做法,请提供帮助。

【问题讨论】:

    标签: java android arrays arraylist


    【解决方案1】:

    我不确定这是否是问题所在,但也许这会有所帮助。

    我看到您将 null 作为第二个参数传递给 LayoutInflater.inflate。也许如果你传递 parent(这是作为 getView() 的第三个参数传入的 ViewGroup,可能是你的 ListView)你会得到更好的结果。

    【讨论】:

    • 谢谢 synic,但这不起作用...我的 Inflater 代码行中有一些错误...但通过将 Inflation 代码更改为下面给出的代码解决了它... LayoutInflater inflater = context.getLayoutInflater();查看视图 = inflater.inflate(R.layout.home_list, null);我不知道发生了什么,但它奏效了。再次感谢您的帮助。
    【解决方案2】:

    我遇到了与此类似的问题,我通过使用 view.findViewById(R.id.hTextVew01) 而不仅仅是 findViewById(R.id.hTextView01)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-21
      • 2014-03-23
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多