-4

I have this error in Java:

01-19 14:08:37.402: E/AndroidRuntime(375): FATAL EXCEPTION: main
01-19 14:08:37.402: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial2/com.example.tutorial2.MainActivity}: java.lang.ClassCastException: com.example.tutorial2.UserCustomAdapter
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 14:08:37.402: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:08:37.402: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:507)
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 14:08:37.402: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)
01-19 14:08:37.402: E/AndroidRuntime(375): Caused by: java.lang.ClassCastException: com.example.tutorial2.UserCustomAdapter
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.example.tutorial2.MainActivity.onCreate(MainActivity.java:42)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-19 14:08:37.402: E/AndroidRuntime(375):  ... 11 more

This the MainActivity.java I think that this activity is wrong.

 package com.example.tutorial2;

    import java.util.ArrayList;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListAdapter;
    import android.widget.Toast;

    public class MainActivity extends Activity {ListView userList; UserCustomAdapte userAdapter;
     ArrayList<User> userArray = new ArrayList<User>();

    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main)
  /**
   * add item in arraylist
   */
  userArray.add(new User("Mumer", "Spain", "Spain"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  /**
   * set item into adapter
   */
  userAdapter = new UserCustomAdapter(MainActivity.this, R.layout.row,
    userArray);
  userList = (ListView) findViewById(R.id.listView);
  userList.setItemsCanFocus(false);
  userList.setAdapter((ListAdapter) userAdapter);
  /**
   * get on item click listener
   */
  userList.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     final int position, long id) {
    Log.i("List View Clicked", "**********");
    Toast.makeText(MainActivity.this,
      "List View Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });

 }

User.java

package com.example.tutorial2;

public class User {
         String name;
         String address;
         String location;

         public String getName() {
          return name;
         }

         public void setName(String name) {
          this.name = name;
         }

         public String getAddress() {
          return address;
         }

         public void setAddress(String address) {
          this.address = address;
         }

         public String getLocation() {
          return location;
         }

         public void setLocation(String location) {
          this.location = location;
         }

         public User(String name, String address, String location) {
          super();
          this.name = name;
          this.address = address;
          this.location = location;
         }

        }

UserCustomAdapter.java

package com.example.tutorial2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UserCustomAdapter {
     Context context;
     int layoutResourceId;
     ArrayList<User> data = new ArrayList<User>();

     public UserCustomAdapter(Context context, int layoutResourceId,
       ArrayList<User> data) {
      super();
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.data = data;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;
      UserHolder holder = null;

      if (row == null) {
       LayoutInflater inflater = ((Activity) context).getLayoutInflater();
       row = inflater.inflate(layoutResourceId, parent, false);
       holder = new UserHolder();
       holder.textName = (TextView) row.findViewById(R.id.textView1);
       holder.textAddress = (TextView) row.findViewById(R.id.textView2);
       holder.textLocation = (TextView) row.findViewById(R.id.textView3);
       holder.btnEdit = (Button) row.findViewById(R.id.button1);
       holder.btnDelete = (Button) row.findViewById(R.id.button2);
       row.setTag(holder);
      } else {
       holder = (UserHolder) row.getTag();
      }
      User user = data.get(position);
      holder.textName.setText(user.getName());
      holder.textAddress.setText(user.getAddress());
      holder.textLocation.setText(user.getLocation());
      holder.btnEdit.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Edit Button Clicked", "**********");
        Toast.makeText(context, "Edit button Clicked",
          Toast.LENGTH_LONG).show();
       }
      });
      holder.btnDelete.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Delete Button Clicked", "**********");
        Toast.makeText(context, "Delete button Clicked",
          Toast.LENGTH_LONG).show();
       }
      });
      return row;

     }

     static class UserHolder {
      TextView textName;
      TextView textAddress;
      TextView textLocation;
      Button btnEdit;
      Button btnDelete;
     }
    }
halfer
  • 18,701
  • 13
  • 79
  • 158
  • Here you go: `Caused by: java.lang.ClassCastException: com.example.tutorial2.UserCustomAdapter` – keyser Jan 19 '14 at 14:44
  • Even if you are new, you could at least give some background to what research you have done. – Martin Davies Jan 19 '14 at 15:01
  • Your UserCustomAdapter should extends by BaseAdapter. Just update public class UserCustomAdapter {...} to public class UserCustomAdapter extends BaseAdapter{...}. Please see this link to reference how it should looks like: http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – mamakurka Jan 19 '14 at 17:36

2 Answers2

2

You UserCustomAdapter is not implementing ListAdapter and that is why you cannot cast it to ListAdapter.

Stuck
  • 8,838
  • 11
  • 40
  • 62
0

Use the below

 public class UserCustomAdapter extends ArrayAdapter{ // extend ArrayAdapter 

   public UserCustomAdapter(Context context, int layoutResourceId,
   ArrayList<User> data) {
   super(context,layoutResourceId,data); // change constructor

And set adapter as

 userList.setAdapter(userAdapter);
Raghunandan
  • 129,147
  • 24
  • 216
  • 249