gpt4 book ai didi

java - 使用 MultidexApplication 可以解决代码太大错误吗

转载 作者:行者123 更新时间:2023-12-01 18:36:18 26 4
gpt4 key购买 nike

我有一个正在服务的 ListView android 项目。我已经在 ListViewAdapter 中对歌词进行了硬编码,并且更改它似乎需要大量工作。我想增加歌词,但由于硬编码,我遇到了“代码太大错误”。我想将 ListViewAdapter 中的歌词从现有的 700 首歌词增加到 900 首歌词。是否有解决方法可以使用 MultidexApplication 增强 64kb 限制?

这是我的代码

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();
}


}

模型类

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;
}
}

最佳答案

减少代码的一种方法是创建一个单独的方法

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

并在每个 if 情况下调用它。

这只会减少重复的代码行。

还在您的模型类中,单独输入标题和歌词。

并根据点击位置,从列表中获取模型,并从该模型中获取标题和歌词,并传入 openFavContent()。

这样您就不需要每次都比较标题。

关于java - 使用 MultidexApplication 可以解决代码太大错误吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60034233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com