我遇到一个问题,我有一个listview从一个数组接收多个项目。这些项目来自不同的作者。我创建了一个标签复选框。因此,当作者最喜欢的句子1被标记时,它被写入sharedPreference并转移到新活动。当作者B的句子1的最喜欢的标记时,它只是替换作者A的句子1的标记。
然后我注意到listview中的每个位置都重复,例如:作者A>句子1 - >位置0作者B>句子1 - >有位置0
由于这个原因,我无法记录不同项目中的短语,但在收藏夹中的相同位置,我不知道是否有可能获得阵列内的位置而不是listview,因为那里你可以获得不会重复的真实位置,或者如果可能的话,不要在列表视图中重复它。
我的代码是:
// DECLARANDO COMPONENTES
private ListView lista;
private AlertDialog alerta;
private String[] frasesR;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frase_autor_frase);
// VINCULANDO COMPONENTES
lista = (ListView) findViewById(R.id.lista_id);
// VERIFICANDO VALOR NULO E ADICIONANDO NO ARRAY
Bundle extra = getIntent().getExtras();
List<String> textos = new ArrayList<>();
int numeroDeTextos = 911;
for (int i = 0; i < numeroDeTextos; i++) {
String texto = extra.getString("texto" + i);
// Verifica se é nulo ou vázio.
if (texto == null || texto.trim().length() == 0) {
continue;
}
textos.add(texto);
}
// CRIANDO O ARRAY
final String[] frases = textos.toArray(new String[0]);
// ENVIANDO FRASES PARA PARAMETRO GLOBAL
frasesR = frases;
// UTILIZANDO O ADPTADOR PARA RECEBER O LISTVIEW
ArrayAdapter<String> adaptador = new ArrayAdapter<String>
(
// Primeiro Parametro do Array Adpater é o Context
getApplicationContext(),
// Segundo Parametro do Array Adpater é o Layout
android.R.layout.simple_list_item_1,
android.R.id.text1,
// Terceiro Parametro do Array Adapter é indicar o nome do Array para exibição
frasesR
);
lista.setAdapter(adaptador);
// EVENTO DE CLIQUE
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
// CHECK BOX
View checkBoxView = View.inflate(FraseAutorFrase.this, R.layout.checkbox, null);
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save to shared preferences
SharedPreferences sharedPreferences = getSharedPreferences("arquivoPreferencia", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("frase"+position, frasesR[position]);
editor.commit();
}
});
checkBox.setText("Marcar como Favorito?");