接收打算与对象和更新ListView

时间:2015-04-26 13:16:30

标签: android listview

我是Android的初学者 我有个问题。

我创建了一个应用程序,其中我有2个活动:ShipList(主要活动)和AddShip 当我从AddShip活动发送一艘船时,ShipList活动中的ListView没有更新(它没有添加这个新船)。

我不知道自己做错了什么 我试图在互联网上找到一些东西,但我找不到任何有用的东西。

有人可以检查接收代码是否正确吗?

问题是当意图用船打开MainActicity时,ListView没有更新。

AddShip代码:

    public class AddShip extends Activity implements View.OnClickListener {

        Button addShip;
        TextView shipName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_ship);
            initialized();
            addShip.setOnClickListener(this);
        }

        private void initialized() {
            addShip = (Button) findViewById(R.id.button);
            shipName = (TextView)findViewById(R.id.editText);
        }

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.button:
                    String name;
                    name = shipName.getText().toString();
                    if (name.matches("")) {
                        Toast.makeText(getApplicationContext(),
                                "Proszę podać nazwę statku!", Toast.LENGTH_LONG)
                                .show();
                    }else{
                        Ship newShip = new Ship(name);
                        Intent intent = new Intent(this, ShipList.class);
                        intent.putExtra("ship", newShip);
                         Toast.makeText(getApplicationContext(),
                                "Dodano nowy statek o nazwie: " + newShip.getShipName(), Toast.LENGTH_LONG)
                                .show();
                        startActivity(intent);
                    }
                    break;
            }

        }
    }

ShipList code:

public class ShipList extends Activity implements View.OnClickListener {

    Button addShip;
    ListView listview;
    List<Ship> shipList;

    private void initialized() {
        addShip = (Button) findViewById(R.id.btnAdd);
        listview = (ListView) findViewById(R.id.list_view);
        shipList = new ArrayList<Ship>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ship_list);
        initialized();
        przykładoweStatki();
        populateListView();
        addShip.setOnClickListener(this)

    }
    private void przykładoweStatki() {
        shipList.add(new Ship("statek 1"));
        shipList.add(new Ship("statek 2"));
        shipList.add(new Ship("statek 3"));
        shipList.add(new Ship("statek 4"));
    }
    private void populateListView() {
        ArrayAdapter<Ship> adapter = new ShipArrayAdapter();
        Ship val=(Ship) getIntent().getParcelableExtra("ship");
        if(val!=null) {
            shipList.add(val);
            adapter.notifyDataSetChanged();
        }
        listview.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAdd:
                startActivity(new Intent("com.mkozykowski.shipproject.AddShip"));
                break;
        }

    }
//---------------------------------------------------------------------------------class
    private class ShipArrayAdapter extends ArrayAdapter<Ship> {

        public ShipArrayAdapter() {
            super(ShipList.this, R.layout.ship_list_row,shipList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView =  convertView;
            if (itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.ship_list_row,parent,false);
            }
            Ship currentShip =  shipList.get(position);
            //Fill the view
            ImageView ivDeleteShip = (ImageView) itemView.findViewById(R.id.deleteShip);
            ImageView ivSignalShip = (ImageView) itemView.findViewById(R.id.signalShip);
            TextView tvShipName = (TextView) itemView.findViewById(R.id.tvShipName);
            tvShipName.setText((CharSequence) currentShip.getShipName());

            ivSignalShip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   startActivity(new Intent("com.mkozykowski.shipproject.AddSignal"));
                    //TODO:
                }
            });
            ivDeleteShip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //TODO: zaimplementuj usuwanie statkow
                    Toast.makeText(getApplicationContext(),
                            "Usunieto statek!", Toast.LENGTH_LONG)
                            .show();
                }
            });
            return itemView;



        }
    }
}

任何建议都会非常有用。

3 个答案:

答案 0 :(得分:0)

ShipArrayAdapter添加此方法:

 @Override
 public int getCount() {
     return shipList.size();
 }

答案 1 :(得分:0)

我更改了主要活动:

Button addShip;
    ListView listview;
    List<Ship> shipList;

    private void initialized() {
        addShip = (Button) findViewById(R.id.btnAdd);
        listview = (ListView) findViewById(R.id.list_view);
        shipList = new ArrayList<Ship>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ship_list);
        initialized();
        populateListView();
        addShip.setOnClickListener(this);

    }

    private void populateListView() {
        ArrayAdapter<Ship> adapter = new ShipArrayAdapter();
        listview.setAdapter(adapter);
        Intent intent = getIntent();
        if(intent.getExtras()!= null) {
            Ship val = (Ship) intent.getExtras().getSerializable("ship");
            shipList.add(val);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAdd:
                startActivity(new Intent("com.mkozykowski.shipproject.AddShip"));
                break;
        }

    }

现在新船已添加到ListView,但新船从ListView

中删除旧船

答案 2 :(得分:0)

我测试了你的代码。这个对我有用。我可以看到新的船名。