Android Studio itemsAdapter和自定义ListView无法正常工作

时间:2015-11-29 18:06:58

标签: java android xml listview android-listview

我在Android Studio中尝试实现自定义ListView。我创建了一个名为“custom_layout_rachel.xml”的xml文件,并将其放在我的“layout”文件夹中。该文件包含我希望ListView看起来的代码。

我正在尝试将名为“activity_urgent__important.xml”的页面中的列表视图看作“custom_layout_rachel.xml”中的列表视图。在这个文件中,我有以下代码:

<ListView
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:id="@+id/lvItems"
      tools:listitem="@layout/custom_layout_rachel"
 />

在Android Studio中,自定义布局显示出来,但是当我在模拟器上运行应用程序时,它就不存在了。

此活动的java代码如下所示:

lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.custom_layout_rachel, items);
lvItems.setAdapter(itemsAdapter);

在第三行是我的错误。

有谁知道为什么我不能这样做或为什么我收到错误?

谢谢!!!

新:

       lvItems = (ListView) findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    readItems();
    itemsAdapter = new CustomListAdapter(this, items);
    lvItems.setAdapter(itemsAdapter);

在“自定义列表适配器(此项目)

上获取错误

我没有适配器代码,但我确实启动了以下内容,如果可以的话,我可以实现它:

public class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private List<String> items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
    {
        super(context, textViewResourceId, list);
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

public CustomListAdapter(Context context , List<String> list) {
    super(context, items);
}

@Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED);
            int color = Color.argb( 200, 255, 64, 64 );
            text.setBackgroundColor( color );

        }

        return mView;
    }

2 个答案:

答案 0 :(得分:1)

在您的第二个CustomListAdapter构造函数中,初始化mContextmContext以来的项目将用于getView()方法中的视图膨胀。

public CustomListAdapter(Context context , List<String> list) {
    super(context, items);
     mContext = context;
     items = list ;
}

答案 1 :(得分:0)

    If you are creating your own adpater extending String type. You don't have to pass android.R.layout.custom_layout_rachel in your third line of code. 
    You will be inflating your custom_layout for listview inside getView method ,within adapter. 

    Simply pass the context and values needed to be populated in Listview. 
    new ArrayAdapter<String>(this,items);
    Change your adapter constructor to the same. 

    if it doesn't work please post the adapter code. 

    Update your code like this.

    public class CustomListAdapter extends ArrayAdapter<String> {

        private Context mContext;
        private int id;
        private List<String> items ;
        private LayoutInflater inflater;

        public CustomListAdapter(Context context,List<String> list )
        {   super(context,list);
            this.mContext = context;
            this.items = list ;
             this.inflater=LayoutInflater.from(context)
        } 
     public int getCount()
    {
    items.length;
    (or)
    items.size();
    }
    @Override 
        public View getView(int position, View v, ViewGroup parent)
        { 
            if(v== null){
                  v = this.inflater.inflate(R.layout.custom_layout_rachel, null);
            } 

            TextView text = (TextView) v.findViewById(R.id.textView);

            if(items.get(position) != null )
            { 
                text.setTextColor(Color.WHITE);
                text.setText(items.get(position));
                text.setBackgroundColor(Color.RED);
                int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

            } 

            return v;
        } 

changes in your code 

  lvItems = (ListView) findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    readItems(); 
    itemsAdapter = new CustomListAdapter<String>(this, items);
    lvItems.setAdapter(itemsAdapter);