我的布局无法使用layoutinflater从getView引用?

时间:2015-01-12 08:59:49

标签: android listview android-listview layout-inflater

Layoutinflater中使用getView时,我不能将我的布局称为资源?

下面是我的适配器类的getView方法以及我想使用convertView = layoutinflater.inflate(R.layout.XXXXXX, null);

引用的布局内容

适配器类

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItemStructure> itemsList;

private void NavDrawerItemStructure(Context context, ArrayList<NavDrawerItemStructure> itemsList) {
    // TODO Auto-generated method stub
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.itemsList.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout., root)
    }       
    return convertView;            
 }
 }

list_view_item_design.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector">

<TextView 
    android:id="@+id/tv_itemTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/list_item_title"
    android:gravity="center_vertical"
    android:layout_centerInParent="true"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

更改此部分;

LayoutInflater layoutinflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutinflater.inflate(R.layout., root)

有了这个;

LayoutInflater vi = LayoutInflater.from(context);
convertView = vi.inflate(R.layout.song_adapter_cell, null);

再试一次。