List在BaseAdapter上显示错误的基础

时间:2012-03-23 06:50:13

标签: android listview baseadapter

MyAdapter是BaseAdapter。 部分内容如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewTag viewTag;
if(convertView == null) {
convertView = myInflater.inflate(R.layout.myrow, null);
viewTag = new ViewTag((ImageView)convertView.findViewById(R.id.myicon),  (TextView)convertView.findViewById(R.id.mytext));
convertView.setTag(viewTag);
}
else {
viewTag = (ViewTag) convertView.getTag();
}
viewTag.mytext.setText(MyTitle[position]);
if(Icon[position].equals("Yes")) {
viewTag.myicon.setVisibility(View.VISIBLE);
}
else {
viewTag.myicon.setVisibility(View.INVISIBLE);
}
}
class ViewTag {
ImageView myicon;
TextView mytext;
public ViewTag(ImageView iv1, TextView tv1) {
this.myicon = iv1;
this.mytext = tv1;
}
}

并在活动中调用如下:

private Runnable updateRunnable = new Runnable() { 
public void run() {
MyAdapter.notifyDataSetChanged();
handler.postDelayed(updateRunnable, 1000);
}
};

为什么图标显示错误?与Icon数组不同。 Icon arrar总是只有“是”。 所以列表一次只能显示一个图标。 但有时显示不止一个。 如何修改?

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行此操作: -

package com.CommonMethod.Activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ListViewDemo extends Activity {
    ListView LS;
    LayoutInflater Linflater;
    String _StrError[] = new String[] { "Yes", "No", "Yes", "No", "No", "Yes",
            "Yes", "No", "Yes", "No" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewdemo);
        LS = (ListView) findViewById(R.id.LSOne);

        Linflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LS.setAdapter(new ListAdapter(this));
    }

    public class ListAdapter extends BaseAdapter {

        public ListAdapter(Context con) {

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 10;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 10;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ReturnView = convertView;
            int _intPostion = getItemViewType(position);
            ViewHolder Holder = Holder = new ViewHolder();
            if (ReturnView == null) {
                ReturnView = Linflater.inflate(R.layout.rawlistview, null);
                Holder.LL = (LinearLayout) ReturnView.findViewById(R.id.LLOne);
                Holder.IV = (ImageView) ReturnView
                        .findViewById(R.id.imageView1);
                if (_StrError[_intPostion].equalsIgnoreCase("Yes")) {

                    Holder.IV.setBackgroundResource(R.drawable.ic_launcher);
                } else {

                    Holder.IV.setBackgroundResource(R.drawable.logo);
                }

                ReturnView.setTag(Holder);

            } else {
                Holder = (ViewHolder) ReturnView.getTag();
                ReturnView.setTag(Holder);
            }
            return ReturnView;
        }

        public class ViewHolder {

            LinearLayout LL;
            ImageView IV;
        }

    }
}