简体   繁体   English

调用自定义列表视图适配器时我的应用程序失败

[英]My app is failing when i call to my custom listview adapter

when i call to my custom adapter constructor it doesnt shows an error but when i call to setAdapter then its terminate my app 当我调用自定义适配器构造函数时,它不会显示错误,但是当我调用setAdapter时,它将终止我的应用程序

ListView listView =(ListView)findViewById(R.id.slistview);
        EmployeeListAdapter suerveryAdapter= new EmployeeListAdapter(SuerveyUserActivity.this,arrayList,getResources());
       listView.setAdapter(suerveryAdapter);

and my custom adapter class code is 我的自定义适配器类代码是

public class EmployeeListAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<serveyData> data;
private static LayoutInflater layoutInflater = null;
public Resources res;
serveyData employee;

public EmployeeListAdapter(Activity a,ArrayList d,Resources r)
{
    activity = a;
    data = d;
    res = r;
    layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    /*for (serveyData e : data) {
        String NameAndEmail = data.size()+"000"+e.getId() + "," + e.getName()+ "," +e.getDesciption();
        Toast.makeText(activity, NameAndEmail,Toast.LENGTH_SHORT).show();
    }*/

}

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

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

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

public class ViewHolder
{
    public TextView tID;
    public TextView tName;
    public Button bDelete;
}

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

    View v1 = convertView;

    final ViewHolder holder;

    if(convertView == null)
    {
        v1 = layoutInflater.inflate(R.layout.single_item,null);
        holder = new ViewHolder();
        holder.tID = (TextView) v1.findViewById(R.id.tvID);
        holder.tName = (TextView) v1.findViewById(R.id.tvName);
        holder.bDelete = (Button) v1.findViewById(R.id.btnDelete);
        v1.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)v1.getTag();
    }

    if(data.size() < 1)
    {
        holder.tName.setText("No Data Found");
        holder.bDelete.setVisibility(View.GONE);
    }
    else
    {
        employee = new serveyData();
        employee = (serveyData) data.get(position);
        holder.tID.setText(employee.getId());
        holder.tName.setText(employee.getName());

        holder.bDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent i = new Intent(activity, AddEmployee.class);
                        activity.startActivity(i);*/
            }
        });
    }

    return v1;
}}

and error log is 错误日志是

enter code here
1-02 21:17:50.269 2957-2957/com.example.abbkr.android2k17_proj E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                                              at android.content.res.Resources.getText(Resources.java:229)
                                                                              at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                              at android.widget.TextView.setText(TextView.java:3620)
                                                                              at com.example.abbkr.android2k17_proj.CustomAdapter.SuerveryAdapter.getView(SuerveryAdapter.java:110)
                                                                              at android.widget.AbsListView.obtainView(AbsListView.java:2267)
                                                                              at android.widget.ListView.makeAndAddView(ListView.java:1769)
                                                                              at android.widget.ListView.fillDown(ListView.java:672)
                                                                              at android.widget.ListView.fillFromTop(ListView.java:733)
                                                                              at android.widget.ListView.layoutChildren(ListView.java:1622)
                                                                              at android.widget.AbsListView.onLayout(AbsListView.java:2102)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
                                                                              at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
                                                                              at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
                                                                              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
                                                                              at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                              at android.view.View.layout(View.java:13754)
                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4362)
                                                                              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
                                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
                                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
                                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                                                                              at android.view.Choreographer.doFrame(Choreographer.java:525)
                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                                                                              at android.os.Handler.handleCallback(Handler.java:615)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                              at android.os.Looper.loop(Looper.java:137)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                              at dalvik

i passed arraylist to my custom adapter class , so please help 我将arraylist传递给了我的自定义适配器类,所以请帮忙

Your problem is that you are using setText() with an integer parameter. 您的问题是您正在使用带有整数参数的setText() In this case, Android expects a string resource ID, which is also an integer, so it tries to find the resource with the ID 1, which does not exist of course. 在这种情况下,Android需要一个字符串资源ID,它也是一个整数,因此它会尝试查找ID为1的资源,这当然是不存在的。

To fix this, you have to pass the parameter as a String . 要解决此问题,您必须将参数作为String传递。 So replace this line: 因此,请替换此行:

holder.tID.setText(employee.getId());

With this: 有了这个:

holder.tID.setText(String.valueOf(employee.getId()));

you need to set using 您需要设置使用

holder.tID.setText(String.valueOf(employee.getId()));

Because what you are doing us setting an integer using setText() would look for string resource but it would not be there and hence will throw an error. 因为您正在做的事情是使用setText()设置整数,因此会查找字符串资源,但它不会存在,因此将引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用自定义适配器过滤自定义 ListView 时出现崩溃和 nullPointer - I get a crash and nullPointer when Filtering through my custom ListView with Custom Adapter 如何在ListView适配器中缓存自定义视图? - How can I cache custom views in my ListView Adapter? 我的自定义列表视图适配器重复最后一项 - My custom listview adapter repeats the last item 使用自定义适配器SQL在我的列表视图中没有数据 - No data appears on my listview with custom adapter sql 如何从我的自定义适配器更新ListView - How update ListView from my Custom Adapter 如何按字母顺序对 listView 的文件进行排序并集成列表适配器?我正在使用自定义适配器 - How do i sort alphabetically the files of my listView and integrate a list adapter?I'm using a custom adapter 如何将ListView适配器转换为RecyclerView适配器? - How do I convert my ListView adapter into RecyclerView adapter? 使用自定义适配器后我的应用程序崩溃 - My App crashes after using a Custom Adapter 如何从主要活动类中的自定义列表视图适配器管理onClick函数 - How can I manage onClick function from my custom listview adapter in main activity class 尝试使用 notifyDataSetChanged 更新我的 listView 自定义列表适配器但不工作 - Trying to update my custom list adapter of listView using notifyDataSetChanged but not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM