gpt4 book ai didi

java - ItemClickListener 调用方法后

转载 作者:行者123 更新时间:2023-12-01 16:29:43 25 4
gpt4 key购买 nike

我需要在gridView.setOnItemClickListener末尾调用createRandomList()方法,但是当我制作这个 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();
}

}

最佳答案

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);
}
createRandomList();
gridView.setAdapter(adapter);
}
});

关于java - ItemClickListener 调用方法后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62076682/

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