ONItemClickListener 调用方法后
Posted
技术标签:
【中文标题】ONItemClickListener 调用方法后【英文标题】:After ItemClickListener call methot 【发布时间】:2020-09-16 11:24:46 【问题描述】:我需要在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();
【问题讨论】:
我试图让您的问题更具可读性。您应该尝试提出一个展示您的问题的最小代码示例。很少有人愿意通读这样的一大堆代码。 【参考方案1】:将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);
);
【讨论】:
是的,这可行,但问题是,gridview 有 2 个列表,第一个 randdomList 已更新,但第二个 imageList 未更新(这是我的问题) 其实我需要这个 gridView.setAdapter(adapter);创建随机列表(); gridView.setAdapter(适配器);第一次不更新以上是关于ONItemClickListener 调用方法后的主要内容,如果未能解决你的问题,请参考以下文章
未调用 DrawerLayout 中的自定义 BaseAdapter ListView onItemClickListener
滚动后单击项目时列表视图中 onitemclicklistener 上的 Nullpointerexception
传递嵌套在片段中的 AdapterView.OnItemClickListener() 中的活动上下文 - 方法未定义
android 模拟微信消息 OnItemClickListener()方法