Creating an custom arrayadapter

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 659 Views - Last Post: 22 April 2019 - 09:37 AM

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Creating an custom arrayadapter

Posted 20 April 2019 - 03:57 AM

I'm trying to use a custom array adapter that I have created. I have no errors but my phone crashes when I try to click on my button to add an item in my listview because I'm making a todo app.

This is the log I get in my item adapter class and main activity something goes wrong but I don't have any errors.


2019-04-20 12:39:25.646 20123-20123/com.izildo.android.todoapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.izildo.android.todoapp, PID: 20123
java.lang.ClassCastException: com.izildo.android.todoapp.MainActivity$1 cannot be cast to android.content.Context
at com.izildo.android.todoapp.ItemAdapter.<init>(ItemAdapter.java:24)
at com.izildo.android.todoapp.MainActivity$1.onclick(MainActivity.java:45)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Main class
 @SuppressLint({"WrongViewCast", "CutPasteId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        itemText = findViewById(R.id.item_text);
        final String itemT = itemText.getText().toString();

        itemList = findViewById(R.id.item_list);

        itemAdd = findViewById(R.id.item_add);

        listView = findViewById(R.id.item_list);
        final ArrayList<Items> itemsList = new ArrayList<>();

        itemAdd.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {
                itemsList.add(new Items(itemT));

                itemAdapter = new ItemAdapter(this, itemsList); <<< error here
                listView.setAdapter(itemAdapter);

                showToast("oke");
            }
        });

    }

    private void showToast(String text){
        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
    }



adapter
public class ItemAdapter extends ArrayAdapter<Items> {

    private Context mContext;
    private List<Items> itemsList;


    public ItemAdapter(@NonNull View.onclickListener context, @SuppressLint("SupportAnnotationUsage") @LayoutRes ArrayList<Items> list){
        super((Context) context, 0, list); << error here
        mContext = (Context) context;
        itemsList = list;
    }

    public View getView(int position, @Nullable View covertView, @NonNull ViewGroup parent){
        View listItem = covertView;
        if(listItem == null){
            listItem = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent,false);
        }

        Items currentItem = itemsList.get(position);

        TextView text = listItem.findViewById(R.id.item_text2);
        text.setText(currentItem.getItemText());

        return listItem;

    }
}


This post has been edited by [email protected]: 20 April 2019 - 04:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating an custom arrayadapter

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 20 April 2019 - 10:24 PM

Don't use this, use getApplication() instead since your MainActivity is extend to AppCompatActivity.

This post has been edited by wseng92: 20 April 2019 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 02:35 AM

Done that, no log errors anymore but don't know why onclick my custom adapter not displaying the text from my textbox into the listview.
Was This Post Helpful? 0
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 02:48 AM

Check itemsList size. Perhaps it is 0 ?
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 03:16 AM

I have put some breakpoints in the main activity and itsays itemslist = 1, but then later says item adapter = null.
So at the last line in the onclick
listView.setAdapter(itemAdapter);
has nothing to display.

error snippet: https://imgur.com/vf2uNu6
Was This Post Helpful? 0
  • +
  • -

#6 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 03:37 AM

Move listView.setAdapter(itemAdapter); outside setonclickListener

This post has been edited by wseng92: 21 April 2019 - 03:39 AM

Was This Post Helpful? 0
  • +
  • -

#7 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 04:06 AM

View Postwseng92, on 21 April 2019 - 03:37 AM, said:

Move listView.setAdapter(itemAdapter); outside setonclickListener


Still the same.
Was This Post Helpful? 0
  • +
  • -

#8 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 04:10 AM

1. Why you need declare itemsList as final?
2. Use log to check currentItem.getItemText(),what value you get ?
Was This Post Helpful? 0
  • +
  • -

#9 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 04:23 AM

1. I don't know the IDE told me to change it to that, so I did, should I remove it?
2. The breakpoint only direct to
super(context, 0, list);
in my itemadapter class and back to mainactivity
itemAdapter = new ItemAdapter(getApplication(), itemsList);


I don't know do I log it right this way?
 TextView text = listItem.findViewById(R.id.item_text2);
        text.setText(currentItem.getItemText());
        Log.v(TAG, "The current item: " + text);

        return listItem;


Was This Post Helpful? 0
  • +
  • -

#10 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 04:25 AM

Check log, what does this line displayed ?
 Log.v(TAG, "The current item: " + currentItem.getItemText());


Was This Post Helpful? 0
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 04:38 AM

Nothing, I only I have this line al the time: D/EGL_emulation: eglMakeCurrent: 0xddef31c0: ver 3 0 (tinfo 0xe55c6230).
But I don't know what it means, I searched it up, google can't find it.
Was This Post Helpful? 0
  • +
  • -

#12 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 05:33 AM

Nevermind I will continue to do some research, at least thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#13 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 06:08 AM

You using java ? You suppose will get error on setonclickListener. It should be setonclickListener

This post has been edited by wseng92: 21 April 2019 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#14 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 24-February 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 11:47 AM

I don't understand it is already like that.
Was This Post Helpful? 0
  • +
  • -

#15 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 707
  • Joined: 23-September 15

Re: Creating an custom arrayadapter

Posted 21 April 2019 - 11:22 PM

Have you solved?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2