Мне нужно вызвать метод createRandomList()
в конце gridView.setOnItemClickListener
, но когда я делаю это gridView
, imageList
не обновляется.
Если я вызываю его в textTT.setOnClickListener
работает. Но мне нужно, чтобы он работал после gridView.setOnItemClickListener
.
Я имею в виду, что после gridView.setOnItemClickListener
метод createRandomList()
должен автоматически вызываться снова.
public class FragmentTrainingTest extends Fragment {
Context context;
TextView textTT;
ArrayList<kayitSinifi> kelimeList = new ArrayList<>();
int rand,position;
Random r = new Random();
MediaPlayer noVoice,yesVoice;
ArrayList<kayitSinifi> randomList = new ArrayList<>();
ArrayList<Integer> imageList = new ArrayList<>();
gridAdapter adapter;
boolean ilkKez = true;
@SuppressLint("ClickableViewAccessibility")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_training_test, container, false);
noVoice = MediaPlayer.create(context,R.raw.glitchinthematrix);
yesVoice= MediaPlayer.create(context,R.raw.fillingyourinbox);
final GridView gridView = inflate.findViewById(R.id.gridView);
textTT= inflate.findViewById(R.id.txtTrainingTest);
createRandomList();ilkKez = false;
adapter = new gridAdapter(context,randomList,imageList);
gridView.setAdapter(adapter);
textTT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createRandomList(); gridView.setAdapter(adapter);
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("tester","Item clicked");
if(position == rand){
yesVoice.start();
imageList.set(rand,R.drawable.ic_check_black_24dp);
} else {
noVoice.start();
imageList.set(position,R.drawable.ic_close_black_24dp);
imageList.set(rand,R.drawable.ic_check_black_24dp);
}
gridView.setAdapter(adapter);
// createRandomList(); // please focus here
}
});
return inflate;
} //end of onCreateView
private void createRandomList() {
ArrayList<Integer> randomNumber = new ArrayList<>();
// if (ilkKez) {} else try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
for (int i=0; randomList.size()>0;i++) randomList.remove(0);
for (int i=0; imageList.size()>0;i++) imageList.remove(0);
for (int i = 0; i< 6; i++) {
do {
rand = r.nextInt(kelimeList.size());
}
while(randomNumber.contains(rand));
randomNumber.add(rand);
randomList.add(kelimeList.get(rand));
imageList.add(R.drawable.ic_fingerprint_black_24dp);
}
rand = r.nextInt(5); // 6 sayi belirlendi bu ise esas oglan
textTT.setText(randomList.get(rand).anaDil);
textTT.setBackgroundColor(Color.parseColor("#4CAF50"));
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
context=activity;
dbaseHelper db = new dbaseHelper(context);
kelimeList=db.dbList2();
}
}