当我尝试添加用户定义的对象时,list.add崩溃了

时间:2013-06-29 03:52:15

标签: android listview arraylist

我正在尝试创建一个Android应用程序,它接受用户输入来创建一个播放器列表并保持顺序,但我一直在尝试使用一个带有int位置和String名称的ArrayList,但是当我尝试{{ 1}}我的应用程序崩溃 我一直试图添加到一个ArrayList playList.add(player)我试过playerList = new ArrayList<Player>();,但没有运气  数据将被传回并显示在Toast中,但现在显示在ListView项目中。我错过了什么? 这是我的MainActivity:

List<Player> = new ArrayList<Player>

这是我的customListViewAdapter:

    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();
    }

xml mainActivity:

    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;  
        }
    }

添加到listView的行:

   <?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>

2 个答案:

答案 0 :(得分: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只在活动的生命周期内被调用过一次。

您还必须致电:

adapter.notifyDataSetChanged(); 

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

答案 1 :(得分:0)

您已将playerList声明为类变量,因此您无需再次在onCreate中声明,只需使用之前声明过的变量。

并在Toast.makeText中使用您的onActivityResult方法

Toast.makeText(this, position + " " + name, Toast.LENGTH_LONG).show();