为什么我的listview是空的?

时间:2015-01-28 11:22:12

标签: java android listview

我有一个arraylist,用户可以在其中输入文字。它在屏幕上显示为列表视图。这是有效的,但是当我试图获得arraylist的值时,它说:索引0无效,大小为0.所以我猜测由于某种原因listview不填充?

这就是我在列表中添加值的方法:

public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
    public Button mBtnIstrinti;
    public Button mBtnPrideti;
    public Button mBtnPradeti;
    public EditText mYrasytiVarda;
    public ListView mZaidejai;

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zaidejai);
        mBtnPrideti = (Button) findViewById(R.id.pridėtiBtn);
        mBtnPrideti.setOnClickListener(this);
        mYrasytiVarda = (EditText) findViewById(R.id.VardoYrasymoBtn);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

        // set the mZaidejai variable to your list in the xml

        mZaidejai = (ListView) findViewById(R.id.sarašas);
        mZaidejai.setAdapter(adapter);
        mZaidejai.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            // remove item from List.


            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                list.remove(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(ZaidejaiActivity.this);
                builder.setMessage("Delete?");
                builder.setTitle("Confirm Action");
                builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        adapter.notifyDataSetChanged();
                    }
                    //checked.clear();
                });
                builder.setNegativeButton("Cancel", null);
                builder.create();
                builder.show();
            }
        });
        mBtnPradeti = (Button) findViewById(R.id.žaistiBtn);
        mBtnPradeti.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // count items
                int i;
                for (i = adapter.getCount() - 1; i >= 0; i--) {
                    String obj = adapter.getItem(i);
                    // send items to other activity

                    Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
                    pradetiZaidima.putExtra("playerList", obj);
                    startActivity(pradetiZaidima);

                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        String input = mYrasytiVarda.getText().toString();
        if(input.length() > 0)
        {
            // add string to the adapter, not the listview
            adapter.add(input);



            // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
        }else{
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Klaida:");
            alertDialog.setMessage("Blogai yrašytas vardas");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
// here you can add functions
                }
            });

            alertDialog.show();
        }

    }

修改

在此活动中,我想获取列表的值:

public class ZaidimasActivity extends ZaidejaiActivity {

    public TextView mZaidejas;
    public TextView mKlausimas;
    public Button mKitasKlausimas;
    public Button mGryzti;
         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_zaidimas);
             /** //get the player list from ZaidejaiActivity
            Bundle recdData = getIntent().getExtras();
            String myVal = recdData.getString("playerList"); */


            //show the first players name

            mZaidejas = (TextView)findViewById(R.id.ZaidejoVardas);
            mZaidejas.setText(list.get(0));


           /** mGryzti = (Button)findViewById(R.id.GryztiMeniuBtn);

            mKitasKlausimas = (Button)findViewById(R.id.KitasBtn);
            mKitasKlausimas.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                }
            });
     */

5 个答案:

答案 0 :(得分:1)

不幸的是你的list==empty。所以在其上添加一些值。

list.add("ABC");
list.add("XYZ");

然后setAdapter

 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

 mZaidejai = (ListView) findViewById(R.id.sarašas);
 mZaidejai.setAdapter(adapter);

答案 1 :(得分:0)

您没有为其添加任何值。您的列表为空,并为其添加一些值,例如list.add("hyd"); list.add("city"); 并将这些值传递给适配器

答案 2 :(得分:0)

宁愿做

adapter.add(input);

待办事项

list.add(input);
adapter.notifyDataSetChanged();

答案 3 :(得分:0)

所以,正如其他人所说,您应该将adapter.add(input);方法上的onClick更改为

list.add(input);
adapter.notifyDataSetChanged();

另外,在一个不相关的问题上,您应该在list.remove(position);来电之前将onClick电话转移到肯定按钮的以下adapter.notifyDataSetChanged();方法,因此如果用户取消操作:)

答案 4 :(得分:0)

第一次列表适配器为空(getCount()= 0)并且你在for中执行-1

   mBtnPradeti.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // count items
            int i;

          if (adapter.getCount() > 0)
          {

            for (i = adapter.getCount() - 1; i >= 0; i--) {
                String obj = adapter.getItem(i);
                // send items to other activity

                Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
                pradetiZaidima.putExtra("playerList", obj);
                startActivity(pradetiZaidima);

            }

          }

        }
    });

并为此代码:          mZaidejas.setText(list.get(0));

但是在创建你的清单是空的!!!