Abubakar
Abubakar

Reputation: 374

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

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

Upvotes: 1

Views: 84

Answers (2)

Tulsi
Tulsi

Reputation: 719

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.

Upvotes: 1

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

Your problem is that you are using setText() with an integer parameter. 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.

To fix this, you have to pass the parameter as a String. So replace this line:

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

With this:

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

Upvotes: 2

Related Questions