Привет, у меня есть игра, которая, когда она будет завершена, мне бы хотелось, чтобы в списке просмотра основного действия была отмечена галочка в конце элемента списка. В настоящее время, когда игра заканчивается, флажок появляется на всех элементах списка, а не только на том, который он закончил. Может ли кто-нибудь помочь мне в этом? Заранее большое спасибо.
Код для адаптера:
public class CrosswordAdapter extends ArrayAdapter<Crossword> {
private int mColorResourceId;
SharedPreferences sp;
String title;
public CrosswordAdapter(Context context, ArrayList<Crossword> crosswords, int colorResourceId) {
super(context, 0, crosswords);
mColorResourceId = colorResourceId;
sp = getContext().getSharedPreferences("crosswords", context.MODE_PRIVATE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Crossword currentCrossword = getItem(position);
TextView titleTextView = (TextView) listItemView.findViewById(R.id.txt_title);
titleTextView.setText(currentCrossword.getTitle());
if (sp.contains("words_completed" + sp.getString("id", title))
&& sp.getInt("words_completed" + sp.getString("id", title), 0) == sp.getInt("words_all"
+ sp.getString("id", title), 0)){
listItemView.findViewById(R.id.completed).setVisibility(View.VISIBLE);}
notifyDataSetChanged();
View textContainer = listItemView.findViewById(R.id.text_container);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
textContainer.setBackgroundColor(color);
return listItemView;
}
}
Кроссворд
// if all complete
if (sp_main.contains("words_completed" + getIntent().getStringExtra("id"))
&& sp_main.getInt("words_completed" + getIntent().getStringExtra("id"), 0) == sp_main.getInt("words_all"
+ getIntent().getStringExtra("id"), 0)) {
chronometer2.stop();
long elapsed = (Long)(SystemClock.elapsedRealtime()-chronometer2.getBase());
Bundle extras = new Bundle();
extras.putLong("time", elapsed);
Intent intent = new Intent(CrosswordActivity.this, CrosswordGameOverActivity.class);
intent.putExtras(extras);
startActivity(intent);
finish();
String title = getIntent().getStringExtra("id");
sp_main.edit().putBoolean("complete", true).commit();
sp_main.edit().putString("id", title).commit();
}
И активность на главной странице
public class CrosswordMainpageActivity extends AppCompatActivity {
ListView listView;
SharedPreferences sp;
String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossword_mainpage);
ImageView imageView = findViewById(R.id.home_button);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new intent to open the {@link DisordersActivity}
Intent imageViewIntent = new Intent(CrosswordMainpageActivity.this, MainActivity.class);
// Start the new activity
startActivity(imageViewIntent);
}
});
// fullscreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// preferences
sp = getSharedPreferences("crosswords", MODE_PRIVATE);
ArrayList<Crossword> crosswords = new ArrayList<Crossword>();
crosswords.add(new Crossword("organelles"));
crosswords.add(new Crossword("cytoskeleton"));
crosswords.add(new Crossword("cell cycle"));
crosswords.add(new Crossword("stem cells"));
crosswords.add(new Crossword("blood stem cells"));
crosswords.add(new Crossword("red blood cells"));
crosswords.add(new Crossword("white blood cells"));
crosswords.add(new Crossword("blood disorder"));
crosswords.add(new Crossword("protein"));
crosswords.add(new Crossword("amino acid"));
crosswords.add(new Crossword("carbohydrate"));
crosswords.add(new Crossword("long fatty acids"));
crosswords.add(new Crossword("saturated fatty acids"));
crosswords.add(new Crossword("unsaturated fatty acids"));
crosswords.add(new Crossword("carbohydrate metabolism"));
crosswords.add(new Crossword("biochemical reactions"));
crosswords.add(new Crossword("enzymes"));
crosswords.add(new Crossword("hormones"));
crosswords.add(new Crossword("brain"));
crosswords.add(new Crossword("liver"));
crosswords.add(new Crossword("heart"));
crosswords.add(new Crossword("CVS-arteries"));
crosswords.add(new Crossword("neurotransmitter"));
crosswords.add(new Crossword("nervous system"));
crosswords.add(new Crossword("muscular system"));
crosswords.add(new Crossword("immune system"));
crosswords.add(new Crossword("anti-cancer drugs"));
crosswords.add(new Crossword("drugs"));
crosswords.add(new Crossword("tissue culture 1"));
crosswords.add(new Crossword("tissue culture 2"));
crosswords.add(new Crossword("intro to microbiology 1"));
crosswords.add(new Crossword("intro to microbiology 2"));
crosswords.add(new Crossword("bacteria"));
crosswords.add(new Crossword("fungal"));
crosswords.add(new Crossword("parasite"));
crosswords.add(new Crossword("virus"));
crosswords.add(new Crossword("viral diseases"));
crosswords.add(new Crossword("bacterial diseases"));
crosswords.add(new Crossword("genetic disorders"));
crosswords.add(new Crossword("cancer"));
CrosswordAdapter adapter = new CrosswordAdapter(this, crosswords, R.color.category_crossword);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), CrosswordActivity.class);
String title = "organelles";
myIntent.putExtra("id", title);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), CrosswordActivity.class);
String title = "cytoskeleton";
myIntent.putExtra("id", title);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), CrosswordActivity.class);
String title = "cellcycle";
myIntent.putExtra("id", title);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), CrosswordActivity.class);
String title = "stemcells";
myIntent.putExtra("id", title);
startActivityForResult(myIntent, 0);
}