我是Android的新手,只是在学习阶段并且有一个查询,如果你能回答的话会很棒。
我试图制作的第一个应用程序是关于计算分数,它还有一个记分牌页面,显示哪个玩家得分多少。我想做的是在运行时接受播放器的名称,可能是使用弹出窗口,一旦播放器1出局,再次弹出以获得第二个播放器的名称。
以下是代码:
activity_scoreboard:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_of_players"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.cricketcounter.Scoreboard">
</ListView>
Scoreboard.java:
package com.example.android.cricketcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
public class Scoreboard extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scoreboard);
ArrayList<Players> players = new ArrayList<>();
players.add(new Players("Abc",0,0)); //I would like to pass in the user-inputted value instead of Abc here.
players.add(new Players("Def",0,0));
PlayerAdapter playerAdapter = new PlayerAdapter(this, players);
ListView playerList = (ListView)findViewById(R.id.list_of_players);
playerList.setAdapter(playerAdapter);
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/runsA"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ballsA"/>
</LinearLayout>
Players.java
package com.example.android.cricketcounter;
/**
* Created by Abhijeet on 01-07-2017.
*/
public class Players {
private String mPlayerName;
private int mRuns;
private int mBalls;
public Players(String playerName, int runs, int balls){
mPlayerName = playerName;
mRuns = runs;
mBalls = balls;
}
public String getPlayerName(){
return mPlayerName;
}
public int getRuns(){
return mRuns;
}
public int getBalls() {
return mBalls;
}
}
PlayerAdapter.java:
package com.example.android.cricketcounter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Abhijeet on 01-07-2017.
*/
public class PlayerAdapter extends ArrayAdapter<Players> {
public PlayerAdapter(Context context, ArrayList<Players> players){
super(context, 0, players);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Players currentPlayer = getItem(position);
TextView playerName = (TextView) listItemView.findViewById(R.id.name);
playerName.setText(currentPlayer.getPlayerName());
TextView runsScored = (TextView) listItemView.findViewById(R.id.runsA);
runsScored.setText(String.valueOf(currentPlayer.getRuns()));
TextView ballsTaken = (TextView) listItemView.findViewById(R.id.ballsA);
ballsTaken.setText(String.valueOf(currentPlayer.getBalls()));
return listItemView;
}
}