Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用不同数量的XML填充android中的ListView,并从列表中提取数据?_Java_Android_Xml_Listview - Fatal编程技术网

Java 如何用不同数量的XML填充android中的ListView,并从列表中提取数据?

Java 如何用不同数量的XML填充android中的ListView,并从列表中提取数据?,java,android,xml,listview,Java,Android,Xml,Listview,我正在尝试为我学校的一个游戏俱乐部创建一个类似于括号的应用程序,但是我在第一个活动中遇到了很多麻烦。我希望有一个球员姓名的输入框列表,并且能够按下按钮添加更多球员(列表中有更多输入框),并将框中的姓名带入下一个活动,其中列表中的每个项目都有一个球员姓名(textView)和另一个输入框,用于该球员在该轮的得分,这将最终转入最终活动,该活动将显示进入下一轮的球员名单 我曾考虑为不同数量的玩家创建多个XML,但这将限制游戏中可以玩的玩家数量,也意味着我必须为每个玩家数量创建3个XML(editTex

我正在尝试为我学校的一个游戏俱乐部创建一个类似于括号的应用程序,但是我在第一个活动中遇到了很多麻烦。我希望有一个球员姓名的输入框列表,并且能够按下按钮添加更多球员(列表中有更多输入框),并将框中的姓名带入下一个活动,其中列表中的每个项目都有一个球员姓名(textView)和另一个输入框,用于该球员在该轮的得分,这将最终转入最终活动,该活动将显示进入下一轮的球员名单

我曾考虑为不同数量的玩家创建多个XML,但这将限制游戏中可以玩的玩家数量,也意味着我必须为每个玩家数量创建3个XML(editText,textView&editText,textView)

到目前为止,我有:

MainActivity.java

package com.example.bracketapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListActivity;

public class MainActivity extends ListActivity {

    public static ArrayList<Player> players = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        players.add( new Player() );
        players.add( new Player() );

        this.setListAdapter(new ArrayAdapter<Player>(
                 this, R.layout.player_row,
                 R.id.textView1, ));
    }

    public void createPlayer() {
        players.add( new Player() );
    }
}
和player_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:text="Player Name" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="6dp"
        android:ems="10"
        android:inputType="textPersonName" />

</LinearLayout>


我不知道这里是否很清楚,但我完全不知道我在做什么,非常感谢任何帮助。谢谢

ArrayAdapter
似乎具有方法
add
/
insert
,可以执行您想要的操作(向列表中添加另一项)。否则,在ArrayList中添加一个新播放器,并在适配器上调用
notifyDataSetChanged
。我的问题是让项目实际显示在ListView中,而不是管理播放器的数据
package com.example.bracketapp;

public class Player {
    String name;
    int points;

    public Player(){
        name = null;
        points = 0;
    }

    public String getName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int newPoints){
        points = newPoints;
    }

    public void setName(String newName) {
        name = newName;
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:text="Player Name" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="6dp"
        android:ems="10"
        android:inputType="textPersonName" />

</LinearLayout>