gpt4 book ai didi

java - 列表和适配器

转载 作者:行者123 更新时间:2023-12-01 11:49:07 25 4
gpt4 key购买 nike

如何从 mGames 的数组中记录用户对他们最喜欢的游戏的投票?

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.view.ListView;

public class GameListActivity extends ListActivity {

public String[] mGames = {
"Age of Empires",
"Civilization",
"Minecraft",
"SimCity"
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_list);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mGames);
setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("List Item: ", "Clicked!");
}


public void sendVote(String game) {
VoteService.recordVote(game);
}
}

辅助方法...sendVote(),将记录用户的投票 对于他们最喜欢的游戏...我如何从列表中调用正确的游戏?

最佳答案

您需要实现项目监听器:

public class GameListActivity extends ListActivity implements OnItemClickListener

并在onCreate方法中设置监听器:

ListView list = (ListView) findViewById(R.id.your_list);
list.setListAdapter(adapter);
list.setOnClickListener(this);

关于java - 列表和适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921613/

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