0

I want to display a list in fragment. i am using a custom arraylist. it is giving me classcast exception. i searched for the soluo solvetion but it dosnt solve my problem. i am new to android so i have no idea how this.. can anyone help..

this is my Item class
public class Item  {
    public String item_name;
    public String item_desc;
    public String item_qty;


    public Item(String item_name, String item_desc,String item_qty) {
        super();
        this.item_name = item_name;
        this.item_desc = item_desc;
        this.item_qty = item_qty;
    }

    public String getItem_name(){
        return item_name;
    }

   public void setItem_name(String item_name)
    {
        this.item_name=item_name;
    }

   public String getItem_desc()
    {
        return  item_desc;
    }
   public  void setItem_desc(String item_desc)
    {
        this.item_desc=item_desc;
    }
    public String getItem_qty()
    {
        return  item_qty;
    }
    public void setItem_qty(String item_qty) {
        this.item_qty = item_qty;
    }
}
this is my addstock fragment

public class AddStock extends Fragment {

    EditText edit_item_name,edit_item_desc,edit_item_qty;
    Button btn_save;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_add_stock, container, false);

        edit_item_name = (EditText)rootView.findViewById(R.id.edittext_item_name);
       edit_item_desc = (EditText) rootView.findViewById(R.id.editText_item_desc);
       edit_item_qty = (EditText) rootView.findViewById(R.id.editText_item_qty);
       btn_save = (Button) rootView.findViewById(R.id.button_Save);

        MainActivity act=(MainActivity)this.getActivity();
        final ArrayList stock = act.arrayList;

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String item_name=edit_item_name.getText().toString();
                String item_desc=edit_item_desc.getText().toString();
                String item_qty=edit_item_qty.getText().toString();

                stock.add(item_name);
                stock.add(item_desc);
                stock.add(item_qty);

                Toast.makeText(AddStock.this.getActivity(), "Saved", Toast.LENGTH_LONG).show();
            }
        });
    return rootView;
    }
}
this is my viewstock fragment
public class ViewStock extends Fragment {

    public ViewStock() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview= inflater.inflate(R.layout.fragment_view_stock, container, false);

         ListView  listview = (ListView)rootview.findViewById(R.id.list);
       // Context mCtx = getActivity().getApplicationContext();
        MainActivity act=(MainActivity)this.getActivity();
        ItemAdapter adapter = new ItemAdapter(getActivity(),R.layout.list_item,act.arrayList);
        listview.setAdapter(adapter);

        return  rootview;
    }
}

this is my main activity
Public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FrameLayout mContentFrame;
    FragmentManager fragmentManager;
    private static final String PREFERENCES_FILE = "mymaterialapp_settings";
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    private boolean mUserLearnedDrawer;
    private boolean mFromSavedInstanceState;
    private int mCurrentSelectedPosition;
    public ArrayList<Item> arrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_nav_drawer);

        setUpToolbar();
        arrayList=new ArrayList<Item>();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);

        mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(this, PREF_USER_LEARNED_DRAWER, "false"));

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        setUpNavDrawer();

        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        mContentFrame = (FrameLayout) findViewById(R.id.nav_contentframe);

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                Fragment newFragment;
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.navigation_item_1:
                        newFragment = new AddStock();
                        transaction.replace(R.id.nav_contentframe, newFragment);
                        transaction.addToBackStack(null);
                        transaction.commit();
                        mCurrentSelectedPosition = 0;
                        mDrawerLayout.closeDrawers();
                        return true;
                    case R.id.navigation_item_2:
                        newFragment = new ViewStock();
                        transaction.replace(R.id.nav_contentframe, newFragment);
                        transaction.addToBackStack(null);
                        transaction.commit();
                        mCurrentSelectedPosition = 1;
                        return true;
                    default:
                        return true;
                }
            }
        });
        //fragmentManager.beginTransaction().replace(R.id.nav_contentframe, fragment).commit();

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, 0);
        Menu menu = mNavigationView.getMenu();
        menu.getItem(mCurrentSelectedPosition).setChecked(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setUpToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }

    private void setUpNavDrawer() {
        if (mToolbar != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.drawable.ic_drawer);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            });
        }

        if (!mUserLearnedDrawer) {
            mDrawerLayout.openDrawer(GravityCompat.START);
            mUserLearnedDrawer = true;
            saveSharedSetting(this, PREF_USER_LEARNED_DRAWER, "true");
        }

    }

    public static void saveSharedSetting(Context ctx, String settingName, String settingValue) {
        SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(settingName, settingValue);
        editor.apply();
    }

    public static String readSharedSetting(Context ctx, String settingName, String defaultValue) {
        SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
        return sharedPref.getString(settingName, defaultValue);
    }
}

this is my ItemAdapter 
public class ItemAdapter extends ArrayAdapter<Item> {

        Context context;

    public ItemAdapter(Context context, int resourceId,
                                 ArrayList<Item> items) {
        super(context, resourceId, items);
        this.context = context;
    }

   /*     public int getCount() {
            return items.size();
        }

        public Item getItem(int position) {
            return items.get(position);
        }

    public long getItemId(int position) {

        return position;
    }
*/
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Item rowItem = getItem(position);
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {

                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.list_item, null);

                holder.txtItemName = (TextView) convertView.findViewById(R.id.txt_item_name);
                holder.txtItemDesc = (TextView) convertView.findViewById(R.id.txt_item_desc);
                holder.txtItemQty = (TextView) convertView.findViewById(R.id.txt_item_qty);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtItemName.setText(rowItem.getItem_name());
            holder.txtItemDesc.setText(rowItem.getItem_desc());
            holder.txtItemQty.setText(rowItem.getItem_qty());

            return convertView;
        }

        private class ViewHolder {
            TextView txtItemName;
            TextView txtItemDesc;
            TextView txtItemQty;
        }
}

this is the exception m getting

java.lang.ClassCastException: java.lang.String cannot be cast to  com.example.owner.stock.Item
            at com.example.owner.stock.ItemAdapter.getView(ItemAdapter.java:60)
            at android.widget.AbsListView.obtainView(AbsListView.java:2344)
            at android.widget.ListView.makeAndAddView(ListView.java:1864)
            at android.widget.ListView.fillDown(ListView.java:698)
            at android.widget.ListView.fillFromTop(ListView.java:759)
            at android.widget.ListView.layoutChildren(ListView.java:1673)
            at android.widget.AbsListView.onLayout(AbsListView.java:2148)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15614)
            at android.view.ViewGroup.layout(ViewGroup.java:4968)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
07-22 23:34:00.334  20992-20992/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
2
  • Where is the custom array list you mention. This code is very hard to follow. Consider posting only code that you are having problems with Commented Jul 22, 2015 at 19:54
  • i am getting exception on row Item rowItem = getItem(position); of ItemAdapter class Commented Jul 22, 2015 at 19:56

1 Answer 1

0

You are adding String objects to a list that is typed to take Item objects.

Instead of:

stock.add(someString);
stock.add(someString);
stock.add(someString);

Do

stock.add(new Item(someString, someString, someString));
3
  • yes i got it but what is d solution for it . how can i take Item objects? Commented Jul 22, 2015 at 20:01
  • @user3623979 see above Commented Jul 22, 2015 at 20:11
  • great. Please mark the answer as resolved for other devs on the forums Commented Jul 22, 2015 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.