[英]Loading Buttons into a ListView - Android Studio
我正在創建一個動態菜單,該菜單從文件讀取並為該文件中的整個按鈕創建一個按鈕。 我可以在下面的代碼中看到,使用simple_list_item_1加載列表,但是它調用.toString方法,因此該列表被作為內存引用的文本占用,而不是按鈕的內容,不是實際的按鈕。 我已經讀完書了,也不知道該怎么做,因為我讀到的東西遠遠超出了我的需要。 當代碼從Widget庫中制作普通按鈕時,最終的工作是使用ImageButton。 我尚未制作圖像,因此該按鈕是“調試”占位符。 (對不起,但是這是不必要的信息,只是說如果有人有更好的主意,然后列出按鈕即可。)
謝謝!
這是我的代碼:
public class GameSelect extends Activity {
private IDGen makeID = new IDGen();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This is the main layout of the Activity
RelativeLayout gameSelectMenu = new RelativeLayout(this);
//Rules for the Title
RelativeLayout.LayoutParams titleZone = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
titleZone.addRule(RelativeLayout.CENTER_HORIZONTAL);
titleZone.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//This is the Title
ImageView titleImage = new ImageView(this);
titleImage.setBackgroundResource(R.drawable.gameselect_gameselecttitle);
gameSelectMenu.addView(titleImage, titleZone);
//Rules for the Button Panel
RelativeLayout.LayoutParams buttonPanelPram = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
buttonPanelPram.addRule(RelativeLayout.CENTER_VERTICAL);
buttonPanelPram.addRule(RelativeLayout.CENTER_HORIZONTAL);
//This is the sub-parent
final RelativeLayout selectedGameMenu = new RelativeLayout(this);
//selectedGameMenu.setId(idParent);
selectedGameMenu.setBackgroundColor(Color.GRAY);
//TODO The prams for this needs to be better.
//This is the LinearLayout
ListView buttonPanel = new ListView(this);
ArrayList<Button> btnList = new ArrayList<>();
try {
//This is used to declare the AssetManager
AssetManager content = getAssets();
//We load the file into the Stream
InputStream input = content.open("PlayerDataSheet.txt");
//Buffer the Stream here
BufferedReader buff = new BufferedReader(new InputStreamReader(input));
//Load the first line of stream into this var
String line = buff.readLine();
//Starts the Dynamic button process
while (line != null) {
Button gameBtn = new Button(this);
gameBtn.setText(line);
gameBtn.setId(makeID.newID());
gameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedGameMenu.setVisibility(View.VISIBLE);
}
});
btnList.add(gameBtn);
line = buff.readLine();
}
//Closing all streams that were opened. May have been the reason for frame loss
buff.close();
input.close();
ListAdapter btnAdp = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, btnList);
buttonPanel.setAdapter(btnAdp);
} catch (FileNotFoundException e) {
Log.e("File", "File for button data was not found");
} catch (IOException e) {
Log.e("File", "File was found, input stream is empty");
}
//Adds the newly filled button panel to the main parent
gameSelectMenu.addView(buttonPanel, buttonPanelPram);
buttonPanel.setVisibility(View.VISIBLE);
//This is just a testing line to occupy the sub-parent until I have what I need
TextView testingOne = new TextView(this);
testingOne.setText("If you see this it worked!");
selectedGameMenu.addView(testingOne);
selectedGameMenu.setVisibility(View.INVISIBLE);
//Rules for the SubParent
RelativeLayout.LayoutParams subParent = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
subParent.addRule(RelativeLayout.CENTER_VERTICAL);
subParent.addRule(RelativeLayout.CENTER_HORIZONTAL);
//Adds the sub-parent to the main parent
gameSelectMenu.addView(selectedGameMenu, subParent);
setContentView(gameSelectMenu);
}
}
ListView具有“ OnItemSelctedListener”,它將告訴您選擇了哪個項目。 最好創建一個Java Object EX GameItem來代表文件的一行:
public class GameItem{
private String gameName;
//getters and setters
@Override
public String toString(){
return gameName;
}
}
然后,您將替換以下代碼:
ArrayList<Button> btnList = new ArrayList<>();
有:
ArrayList<GameItem> btnList = new ArrayList<>();
和這個:
while(line != null)
{
Button gameBtn = new Button(this);
gameBtn.setText(line);
gameBtn.setId(makeID.newID());
gameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedGameMenu.setVisibility(View.VISIBLE);
}
});
btnList.add(gameBtn);
line = buff.readLine();
}
有:
while(line != null)
{
GameItem tempGameItem = new GameItem();
tempGameItem.setGameName(line);
btnList.add(tempGameItem);
line = buff.readLine();
}
然后,您應該實現OnItemSelectedListener:
buttonPanel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedGameMenu.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
最后,您應該將Adapter設置為ListView:
ListAdapter btnAdp = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1 ,btnList);
buttonPanel.setAdapter(btnAdp);
您的代碼將如下所示:
ListView buttonPanel = new ListView(this);
buttonPanel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedGameMenu.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayList<Button> btnList = new ArrayList<>();
try
{
//... your code
while(line != null)
{
GameItem tempGameItem = new GameItem();
tempGameItem.setGameName(line);
btnList.add(tempGameItem);
line = buff.readLine();
}
//Closing all streams that were opened. May have been the reason for frame loss
buff.close();
input.close();
ListAdapter btnAdp = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1 ,btnList);
buttonPanel.setAdapter(btnAdp);
}
//... your code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.