无法在ArrayAdapter<>上访问getView

时间:2016-08-18 07:12:25

标签: android

也许,有人可以告诉我为什么会出错。 我正在尝试学习本教程 (http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications/) 但我想将它应用于自定义适配器..

menuListList.setAdapter(new ArrayAdapter<String>(MenuListActivity.this, R.layout.menu_list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row;

            if (convertView == null){
                row = getLayoutInflater().inflate(R.layout.menu_list, null);
            } else {
                row = convertView;
            }
            Log.w("tes", "te3s");
            TextView menuListTitle = (TextView) row.findViewById(R.id.menuListTitle);
            menuListTitle.setText(titleList[position]);

            return row;

        }
    });

我可以访问menuListList.setAdapter()之前和之后的日志,但我无法捕获日志,我的ListView( menuListList )变为空白< / p>

这是我的全班

public class MenuListActivity extends AppCompatActivity {

ListView menuListList;
String[] titleList = {
        "Purchase Order",
        "Receive Item",
        "Expired Item"
};
Integer[] imageList = {
        R.drawable.po,
        R.drawable.ri,
        R.drawable.ex
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_list);
    menuListList = (ListView)findViewById(R.id.menuListList);
    Log.w("tes", "tes1");
    menuListList.setAdapter(new ArrayAdapter<String>(MenuListActivity.this, R.layout.menu_list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row;

            if (convertView == null){
                row = getLayoutInflater().inflate(R.layout.menu_list, parent, false);
            } else {
                row = convertView;
            }
            Log.w("tes", "te2s");
            TextView menuListTitle = (TextView) row.findViewById(R.id.menuListTitle);
            menuListTitle.setText(titleList[position]);

            return row;

        }
    });
    Log.w("tes", "te3s");
}

}

我可以达到&#34; tes1&#34;的log.w和&#34; te3s&#34;但不是关于&#34; te2s&#34;

1 个答案:

答案 0 :(得分:1)

这是您的完整示例

您提供的代码段来自此示例

不需要listview,因为您正在延长ListActivity.

public class MainActivity extends ListActivity {
    private List<String> strings;
    private LayoutInflater mInflater;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        strings = new ArrayList<String>();

        strings.add("This");
        strings.add("is");
        strings.add("a");
        strings.add("long");
        strings.add("list");
        strings.add("which");
        strings.add("will");
        strings.add("be");
        strings.add("displayed");
        strings.add("in");
        strings.add("the");
        strings.add("screen");
        strings.add("with");
        strings.add("one");
        strings.add("word");
        strings.add("in");
        strings.add("a");
        strings.add("line");

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, strings) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row;

                if (null == convertView) {
                    row = mInflater.inflate(R.layout.list_item, null);
                } else {
                    row = convertView;
                }

                TextView tv = (TextView) row.findViewById(android.R.id.text1);
                tv.setText(getItem(position));

                return row;
            }
        });
    }
}

更新自定义适配器完整演示

<强> MainActivity

public class MainActivity extends Activity {

    Context context;

    private CoustomAdapter coustomAdapter;
    private ListView menuListList;
    private ArrayList<CategoriesVo> dataList;


    String[] titleList = {
            "Purchase Order",
            "Receive Item",
            "Expired Item"
    };
    Integer[] imageList = {
            R.drawable.image_1,
            R.drawable.image_1,
            R.drawable.image_1
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        //connect list view
        menuListList = (ListView) findViewById(R.id.menuListList);
        // data array for coustom adapter
        dataList = new ArrayList<>();

        //Adding data to array then we pass it to our adapter
        for (int i = 0; i < titleList.length; i++) {

            CategoriesVo categoriesVo = new CategoriesVo();
            categoriesVo.setImage(imageList[i]);
            categoriesVo.setCname(titleList[i]);
            dataList.add(categoriesVo);
        }

        // passing to adatepet
        coustomAdapter = new CoustomAdapter(this, dataList);
        menuListList.setAdapter(coustomAdapter);
    }
}

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <ListView
        android:id="@+id/menuListList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

<强> CoustomAdapter

public class CoustomAdapter extends BaseAdapter {

    Context myContext;
    private static LayoutInflater inflater = null;
    ArrayList<CategoriesVo> data;

    public CoustomAdapter(Context context, ArrayList<CategoriesVo> data) {
        myContext = context;
        this.data = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public CategoriesVo getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (convertView == null) {
            row = inflater.inflate(R.layout.row_layout, null);

            holder = new ViewHolder();
            holder.title = (TextView) row.findViewById(R.id.textView);
            holder.image = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        CategoriesVo item = data.get(position);
        holder.title.setText(item.getCname());
        holder.image.setImageResource(item.getImage());
        return row;
    }

    static class ViewHolder {

        TextView title;
        ImageView image;
    }
}

<强> row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:weightSum="10">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toEndOf="@+id/imageView"
        android:text="New Text" />
</RelativeLayout>

<强> CategoriesVo

public class CategoriesVo {

    String cid, cname;
    int image;

    public CategoriesVo() {
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
}