Подскажите, пожалуйста, как использовать SharedPreference для заполнения текстовых данных, содержащих заголовок и описание. Я жестко запрограммировал свой ListViewAdapter, и теперь он доставляет мне большие неприятности, когда я пытался добавлять все больше и больше данных. Мне сказали, что я могу добавить больше данных, если я использую SharedPreference и мои данные будут сохранены в виде базы данных где-нибудь (папка ресурсов или любая другая папка a) в формате .txt. Пожалуйста, если возможно, дайте мне пример Github, который работает. Спасибо!
Мои коды ниже
Это ListViewAdapter
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;
}
}