0

I have a serving ListView android project. I hardcoded the lyrics in the ListViewAdapter already and changing it seems like a lot of work. I wanted to increase the lyrics but i encountered "code too large error" due to hardcoding. I want to increase the lyrics i have therein from existing 700 lyrics to 900 lyrics in the ListViewAdapter. Is there a workaround to enhance 64kb limitation using MultidexApplication?

this is my code

public class ListViewAdapter extends BaseAdapter{

    //Variables
    Context mContext;
    LayoutInflater inflater;
    List<Model> modellist;
    ArrayList<Model> arrayList;

    //Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
    }

    public class ViewHolder{
        TextView mTitleTv, mDescTv;
        ImageView mIconTv;
    }

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

    @Override
    public Object getItem(int i) {
        return modellist.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view==null){
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.row, null);

            //locate the views in row.xml
            holder.mTitleTv = (TextView) view.findViewById(R.id.mainTitle);
            holder.mDescTv = (TextView) view.findViewById(R.id.mainDesc);
            holder.mIconTv = view.findViewById(R.id.mainIcon);

            view.setTag(holder);
        }
        else {
            holder = (ViewHolder)view.getTag();
        }
        //set the result into textview
        holder.mTitleTv.setText(modellist.get(i).getTitle());
        holder.mDescTv.setText(modellist.get(i).getDesc());
        //Set the result in imagview
        holder.mIconTv.setImageResource(modellist.get(i).getIcon());

        //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 1")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 001");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 2")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 002");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 3")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 003");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 4")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 004");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 5")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 005");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 6")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 006");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                } if (modellist.get(i).getTitle().equals("Song 001 | This song lyrics 7")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", "Song 007");
                    intent.putExtra("contentTv", "This is song lyrics detail\n\n\n\n");
                    mContext.startActivity(intent);
                }
//etc... up to 900 lyrics

            }
        });


        return view;
    }

    //filter
    public void filter(String charText){
        charText = charText.toLowerCase(Locale.getDefault());
        modellist.clear();
        if (charText.length()==0){
            modellist.addAll(arrayList);
        }
        else {
            for (Model model : arrayList){
                if (model.getTitle().toLowerCase(Locale.getDefault()).contains(charText)){
                    modellist.add(model);
                }
            }
        }
        notifyDataSetChanged();
    }


}

The Model Class

public class Model {

    String title;
    String desc;
    int icon;

    //constructor
    public Model(String title, String desc, int icon) {
        this.title = title;
        this.desc = desc;
        this.icon = icon;
    }

    //getters


    public String getTitle() {
        return this.title;
    }

    public String getDesc() {
        return this.desc;
    }

    public int getIcon() {
        return this.icon;
    }
}
Joseph
  • 400
  • 4
  • 19

1 Answers1

0

One way to reduce code is to create a separate method

public openFavContent(String title, String lyrics){
Intent intent =  new Intent(mContext, FavouritesContentActivity.class);
                    intent.putExtra("actionBarTitle", title);
                    intent.putExtra("contentTv", lyrics);
                    mContext.startActivity(intent);
    }

and call this from each if case.

This will just reduce repeated code lines.

Also in your model class, make an entry for title and lyrics separate.

and based on the position click, get model from the list and get title and lyrics from that model and pass in openFavContent().

This way you don't need to compare title every time.

Kishan Maurya
  • 3,356
  • 8
  • 21