无法解析适配器中的自定义列表视图的布局

时间:2017-05-23 05:34:51

标签: android android-layout listview

我正在尝试为该列表创建一个自定义ListView和一个适配器,我正在对列表视图布局进行膨胀,但是它提示错误

  

找不到符号变量lv_grpMsg

有人可以告诉我如何更正此错误吗?

以下是适配器代码:

public class GrpMsgAdapter extends BaseAdapter{

private Fragment fragment;
private ArrayList data;
private static LayoutInflater layoutInflater = null;
public Resources resources;
GrpMsgModel grpMsgModel = null;
int i = 0;

public GrpMsgAdapter(Fragment fragment, ArrayList data, Resources resources){
    this.fragment = fragment;
    this.data = data;
    this.resources = resources;

    layoutInflater = (LayoutInflater)this.fragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {

    if(data.size() <= 0)
        return 1;

    return data.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

public static class ViewHolder{
    public ImageView iv_grpMsgDp;
    public TextView tv_grpMsgName, tv_grpMsgNoMem;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    ViewHolder viewHolder;

    if(convertView == null) {
        view = layoutInflater.inflate(android.R.layout.lv_grpmsg, null);

        viewHolder = new ViewHolder();

        viewHolder.iv_grpMsgDp = (ImageView) view.findViewById(R.id.iv_grpMsgDp);
        viewHolder.tv_grpMsgName = (TextView) view.findViewById(R.id.tv_grpMsgName);
        viewHolder.tv_grpMsgNoMem = (TextView) view.findViewById(R.id.tv_grpMsgNoMem);

        view.setTag("holder");
    }

    else{
        viewHolder = (ViewHolder)view.getTag();
    }

    if(data.size() <= 0){
        viewHolder.tv_grpMsgName.setText("No data");
    }

    else{
        grpMsgModel = null;
        grpMsgModel = (GrpMsgModel)data.get(position);

        viewHolder.tv_grpMsgName.setText(grpMsgModel.getGrpName());
        viewHolder.tv_grpMsgNoMem.setText(grpMsgModel.getNoMem());

        viewHolder.iv_grpMsgDp.setImageResource(resources.getIdentifier("com.example.nmss.coach" + grpMsgModel.getImageURL(), null, null));
    }

    return view;
}
}

我的ListView布局代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relGrpMsg">

<ImageView
    android:id="@+id/iv_grpMsgDp"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_marginStart="18dp"
    app:srcCompat="@mipmap/ic_launcher_round"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="12dp" />

<TextView
    android:id="@+id/tv_grpMsgName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Group name"
    android:layout_alignTop="@+id/iv_grpMsgDp"
    android:layout_toEndOf="@+id/iv_grpMsgDp"
    android:layout_marginStart="80dp" />

<TextView
    android:id="@+id/tv_grpMsgNoMem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/iv_grpMsgDp"
    android:layout_alignStart="@+id/tv_grpMsgName"
    android:layout_marginBottom="15dp"
    android:text="No. of members" />

<CheckBox
    android:id="@+id/cb_grpMsgSel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tv_grpMsgNoMem"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="24dp" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

应该是R.layout.lv_grpmsg而不是android.R.layout.lv_grpmsg

答案 1 :(得分:0)

使用Contains代替if (buildingCode.Any(x => x.Contains(hostName)))

和其他错误是if (hostName.Contains(buildingCode))

正确的是R.layout.lv_grpmsg