我正在开发一种基于纸牌的游戏,在该游戏中,我必须在每个回合之后显示每个玩家的得分。为此,我创建了一个记分板类,并使用Listview显示了球员的姓名和得分。
以下是我的代码
ScoreCardPopup类
公共类ScoreCardPopup {
private List<Items> itemsList = new ArrayList<Items>();
private ListView listView;
private CustomListAdapter adapter;
private ArrayList<String> playernames = new ArrayList<>();
private ArrayList<Integer> playerscore = new ArrayList<>();
Context context;
public ScoreCardPopup(ArrayList<String> playernames, ArrayList<Integer> playerscore) {
this.playernames = playernames;
this.playerscore = playerscore;
}
public ScoreCardPopup(ArrayList<String> playernames, ArrayList<Integer> playerscore, Context context) {
this.playernames = playernames;
this.playerscore = playerscore;
this.context = context;
}
public void showScoreCard() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_score_card);
//Initialize and create a new adapter with layout named list found in activity_main layout
listView = (ListView) dialog.findViewById(R.id.list);
adapter = new CustomListAdapter(context, itemsList);
listView.setAdapter(adapter);
int i = 0;
while (i < playernames.size()) //Add to the Items array
{
Items items = new Items();
items.setName(playernames.get(i).toString());
items.setScore(playerscore.get(i).toString());
itemsList.add(items);
i++;
}
//All done, so notify the adapter to populate the list using the Items Array
adapter.notifyDataSetChanged();
dialog.show();
}
}
CustomListAdapter类
public class CustomListAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Items> items;
public CustomListAdapter(Context context, List<Items> items) {
this.context = context;
this.items = items;
}
/**
* How many items are in the data set represented by this Adapter.
*
* @return Count of items.
*/
@Override
public int getCount() {
return items.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return items.get(position);
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param scoreView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public View getView(int position, View scoreView, ViewGroup parent) {
ViewHolder holder;
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (scoreView == null) {
scoreView = inflater.inflate(R.layout.list_row, parent, false);
holder = new ViewHolder();
holder.name = (TextView) scoreView.findViewById(R.id.name);
holder.score = (TextView) scoreView.findViewById(R.id.score);
scoreView.setTag(holder);
} else {
holder = (ViewHolder) scoreView.getTag();
}
final Items m = items.get(position);
holder.name.setText(m.getName());
holder.score.setText(m.getScore());
return scoreView;
}
static class ViewHolder {
TextView name;
TextView score;
}
}
物品类
public class Items {
private String name, score;
public Items() {
}
public Items(String name, String score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="16dp"
android:textColor="#222222" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:textSize="14dp"
android:textColor="#222222" />
</RelativeLayout>
score_card.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:cacheColorHint="#FFFFFF"
android:clipToPadding="false"
android:dividerHeight="1dp"
android:paddingBottom="5dp"
tools:context=".ScoreCard.ScoreCard"></ListView>
<Button
android:id="@+id/OK"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="134dp"
android:layout_marginTop="502dp"
android:paddingBottom="0dp"
android:text="@string/button_OK" />
</RelativeLayout>
实施此代码后,我能够创建一个球员水平列表及其各自的总得分。
得分板图像。
但是现在我想垂直显示它,以及每个回合的分数,因为我需要在每个回合之后显示每个球员的排名。就像下面的图片
任何有关实施新记分卡的建议都会很有帮助。