开发者

Android: Why ArrayAdapter Displays null at first?

When i populate an ArrayAdapter, at first the rows in the visible part of the screen will be blank except the first row, which will be an undesired result. Then when I scroll it down and then come back all the screen will be set correctly...please some one tell me why this behavior happens ?

Code :

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

Next Part as Class extending ArrayAdapter, an Inner Class to 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(n开发者_开发知识库ull);
                }
            } else if (item.type_text == Home.FILE_SHOUT) {
                TextView tv1 = (TextView) findViewById(R.id.hTextView01);

The code is getting truncated, so the whole code is not appearing here. I'm sorry about that. Here what i have tried is to put the values in the object of class Home into two textViews and an ImageView, which may vary upon the value returned by variable item.type_text

Also if somebody can tell me another best practice to do this, please help.


I'm not sure if this is the problem, but perhaps this will help.

I see you're passing null to LayoutInflater.inflate as the second parameter. Perhaps if you pass parent (which is the ViewGroup passed in as the third argument to getView(), probably your ListView) you will get better results.


I had a problem similar to this that i solved by using view.findViewById(R.id.hTextVew01) instead of just findViewById(R.id.hTextView01)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜