gpt4 book ai didi

java - Android 切换按钮保存状态自定义 ListView

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:43 24 4
gpt4 key购买 nike

你好 friend ,我是 android 新手,我想知道如何在自定义 ArrayAdapter 中保存 ToggleButton 状态,我在 ArrayAdapter 的帮助下创建自定义 ListView 现在我想如何在 Adapter 中保存其状态,这是我的代码,请解释一下用于实现此目标的逻辑,我处于初学者水平,不知道如何实现此目标?

package bible.swordof.God;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.opengl.Visibility;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.hitomi.cmlibrary.CircleMenu;
import com.hitomi.cmlibrary.OnMenuSelectedListener;
import com.hitomi.cmlibrary.OnMenuStatusChangeListener;

import java.util.List;
import java.util.Locale;

import es.dmoral.toasty.Toasty;

import static android.content.Context.MODE_PRIVATE;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static android.support.constraint.Constraints.TAG;
import static android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext;
import static android.support.v4.content.ContextCompat.startActivity;

public class FullverseAdopter extends ArrayAdapter < String > {
private ALLVERSE activity;

private List < String > versenumber;
private List < String > verseid;
private List < String > verselist;
private List < String > refernce;
TextToSpeech textToSpeech;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDb;
private boolean save;
public static final String MyPREFERENCES = "MyPrefs";


//check for availabe language
int result;


public FullverseAdopter(ALLVERSE context, int resource, List < String > versenumber, List < String > verselist, List < String > refernce, List < String > verseid) {
super(context, resource, versenumber);
this.activity = context;
this.versenumber = versenumber;
this.verselist = verselist;
this.refernce = refernce;
this.verseid = verseid;



}


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

@Override
public String getItem(int position) {
return versenumber.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.versedisplayrow, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder

holder.versenumber = (TextView) convertView.findViewById(R.id.versenumber);
holder.verselist = (TextView) convertView.findViewById(R.id.verse);


convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.versenumber.setText(versenumber.get(position));
holder.verselist.setText(verselist.get(position));

//share verse
holder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toasty.info(activity, "Sharing a verse.", Toast.LENGTH_SHORT, true).show();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, refernce.get(position) + ":" + versenumber.get(position) + '\n' + verselist.get(position));
sendIntent.setType("text/plain");
activity.startActivity(sendIntent);

}
});


textToSpeech = new TextToSpeech(activity, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {
result = textToSpeech.setLanguage(Locale.ENGLISH);
} else {

Toast.makeText(activity, "YOUR DEVICE NOT SUPPORTED", Toast.LENGTH_SHORT).show();
}
}
});




//My toggle button
holder.bookmark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {


} else {

}
}
});
//mDBHelper = new DatabaseHelper(activity);
//mDb = mDBHelper.getWritableDatabase();


//ContentValues contentValues=new ContentValues();
//contentValues.put("id",verseid.get(position));
//contentValues.put("bookname",refernce.get(position));
//contentValues.put("versenumber",versenumber.get(position));
//contentValues.put("verse",verselist.get(position));
//long check=mDb.insert("favourite",null,contentValues);

//Log.d("MY_TAG","DB IS NOW "+check);

//Toasty.success(activity, "Added in favouite"+check, Toast.LENGTH_SHORT, true).show();

holder.speakverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(activity, "I AM CLICKED", Toast.LENGTH_SHORT).show();

if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA) {

Toast.makeText(activity, "Language not supported or Missing", Toast.LENGTH_SHORT).show();

} else {


textToSpeech.speak(verselist.get(position), TextToSpeech.QUEUE_FLUSH, null);

}
}
});


/* holder.removebookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mDBHelper = new DatabaseHelper(activity);
mDb = mDBHelper.getWritableDatabase();

// long delete= mDb.delete("favourite","id=?",new String[]{verseid.get(position)});




//Toasty.error(activity, "Remove in favouite"+delete, Toast.LENGTH_SHORT, true).show();
}
});*/


return convertView;
}

static class ViewHolder {
private ToggleButton favourite;
private TextView versenumber;
private TextView verselist;
private CircleMenu circleMenu;
private ImageView share;

//toggle button
public ToggleButton bookmark;

private boolean defaultvalue;

private ALLVERSE activity;
private ImageView speakverse;

public ViewHolder(View v) {
versenumber = (TextView) v.findViewById(R.id.versenumber);
verselist = (TextView) v.findViewById(R.id.verse);
share = (ImageView) v.findViewById(R.id.share);
bookmark = (ToggleButton) v.findViewById(R.id.adbookmark);
speakverse = (ImageView) v.findViewById(R.id.speakverse);

}
}
}

最佳答案

理想情况下,适配器是一个用于调整特定数据集以在 ListView 中呈现 View 的对象。因此,您应该为该 ListView 中可见的每一行都有一个数据结构(类对象),并将其放入数组列表中。根据 getView 方法中的位置,检索该行的数据并根据该数据初始化 View 。现在,该数据集可以以任何格式(最好是 JSON)保存在服务器或本地内存中,并且可以在将来检索。

关于java - Android 切换按钮保存状态自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202517/

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