0

我正在尝试创建 Android 应用程序,该应用程序需要用户输入来创建播放器列表并保持顺序,但我一直在尝试使用带有 int 位置和字符串名称的 ArrayList 来执行此操作,但是当我尝试playList.add(player)我的应用程序崩溃时。
我一直在尝试添加到playerList = new ArrayList<Player>();我尝试过的 ArrayListList<Player> = new ArrayList<Player>中,但没有成功。
数据被传回并显示在 Toast 但现在是 ListView 项目。我错过了什么?这是我的主要活动:

    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TableRow;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnTouchListener {
        private static final int REQUEST_CODE = 10;
        private SharedPreferences savedPlayerPositions;
        private PlayerActivity playerActivity;
        public List<Player> playerList = new ArrayList<Player>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // set up preferences
            savedPlayerPositions = getSharedPreferences("playerPositions",
                    MODE_PRIVATE);

            ListView lv = (ListView) findViewById(R.id.list);
            List<Player> playerList = new ArrayList<Player>();

            CustomListViewAdapter adapter = new CustomListViewAdapter(this,
                    playerList);
            lv.setAdapter(adapter);

            Button addPlayerButton = (Button) findViewById(R.id.button1);
            addPlayerButton.setOnClickListener(addPlayerButtonListener);

            playerActivity = new PlayerActivity();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public OnClickListener addPlayerButtonListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                // onClick go to player input screen and PlayerActivity
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, PlayerActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        };
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 10) {

        String position = data.getStringExtra("battingPosition");
        String name = data.getStringExtra("playerName");
        int positionInt = Integer.valueOf(position);
        Player player = new Player(positionInt, name);
        **playerList.add(player);**
        Toast.makeText(getApplicationContext(), position + " " + name,
                Toast.LENGTH_LONG).show();
    }

这是我的 customListViewAdapter:

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.TextView;

    public class CustomListViewAdapter extends BaseAdapter
    {  
        LayoutInflater inflater;
        List<Player> playerList;

        public CustomListViewAdapter(Activity context, List<Player> playerList) {  
            super();

            this.playerList = playerList;
            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}

        @Override  
        public int getCount() {  
            // TODO Auto-generated method stub  
            return playerList.size();  }  

        @Override  
        public Object getItem(int position) {  
            // TODO Auto-generated method stub  
            return null;  }  

        @Override  
        public long getItemId(int position) {  
            // TODO Auto-generated method stub  
            return 0;  }

        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  

            Player player = playerList.get(position);
            View vi=convertView;

            if(convertView==null)
                vi = inflater.inflate(R.layout.new_player_view, null);

            //assign components from new_player_view
            TextView playerInfo = (TextView)vi.findViewById(R.id.txtV_player_input_name_title);
            Button editButton = (Button) vi.findViewById(R.id.btn_Edit_Title);
            editButton.setOnClickListener(MainActivity.editPlayerButtonListener);

            playerInfo.setText(player.batPosition + " " + player.name);

            return vi;  
        }
    }

xml 主要活动:

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/txtV_battingOrder_Title"
            android:textSize="20sp" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_AddButton_title" />

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="442dp"
            android:layout_weight="0.89" >
        </ListView>

    </LinearLayout>

添加到 listView 的行:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:padding="5dp" >

        <TextView
            android:id="@+id/txtV_player_input_name_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="21dp"
            android:maxLength="2"
            android:maxLines="1"
            android:width="180dp" />

        <Button
            android:id="@+id/btn_Edit_Title"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="@dimen/btn_edit_player_width"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/txtV_player_input_position_title"
            android:layout_alignBottom="@+id/txtV_player_input_position_title"
            android:layout_alignParentRight="true"
            android:layout_marginRight="14dp"
            android:padding="3dp"
            android:text="@string/bnt_Edit_title" />

    </RelativeLayout>
4

2 回答 2

0

您已经声明playerList为类变量,因此您无需再次声明,onCreate只需使用您之前声明的变量即可。

并像这样使用你的Toast.makeText方法onActivityResult

Toast.makeText(this, position + " " + name, Toast.LENGTH_LONG).show();
于 2013-06-29T04:14:05.857 回答
0

改变:

public List<Player> playerList = new ArrayList<Player>();

public ArrayList<Player> playerList = new ArrayList<Player>();

并在您的 onCreate 内部进行更改:

List<Player> playerList = new ArrayList<Player>();

playerList = new ArrayList<Player>();

但是您不必重新声明两次,因为 onCreate 在 Activity 的生命周期中只被调用一次。

您还必须致电:

adapter.notifyDataSetChanged(); 

每次播放列表数组更改时。

于 2013-06-29T04:02:28.707 回答