为什么我的onClick()方法中的自定义适配器为NULL?

时间:2016-07-06 06:02:33

标签: android listview

我试图实现自定义ListView,我一直在关注这个SO问题ListView with Add and Delete Buttons in each Row in android中的建议。但是我仍然做错了,因为当我点击下面屏幕截图中的添加按钮时......

enter image description here ...我发现错误消息:

onClick() adapter is NULL!!!

这是我的代码:

public class MyActivity extends ListActivity implements OnClickListener {

... 


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.bookmarks);

        addButton = (Button) findViewById(R.id.add_button);
        addButton.setOnClickListener(this);

        bookmarkNameEditText = (EditText) findViewById(R.id.editText);
        Intent i = getIntent();
        default_bookmark_name   = i.getStringExtra("default_bookmark_name");
        currentTrack            = i.getIntExtra("currentTrack",0);
        currentTrackPosition    = i.getIntExtra("currentTrackPosition", 0);
        bookmarkNameEditText.setText(default_bookmark_name);

        datasource = new BookmarkDataSource(this);
        datasource.open();

        ArrayList<Bookmark> values = datasource.getAllBookmarks();
        //instantiate custom adapter
        MyCustomAdapter adapter = new MyCustomAdapter(values, this);

        //handle listview and assign adapter
        ListView lView = (ListView)findViewById(android.R.id.list);
        lView.setAdapter(adapter);
    }


@Override
public void onClick(View src) {
    ArrayAdapter<Bookmark> adapter = (ArrayAdapter<Bookmark>) getListAdapter();
    if (adapter == null ) {
        Log.i(TAG, "MyActivity onClick() adapter is NULL!!!");
        return;
    }

    switch (src.getId()) {
        case R.id.add_button:
            Log.i(TAG, "ADD BUTTON CLICKED");
            String new_bookmark_name = bookmarkNameEditText.getText().toString();
            Log.i(TAG, "MyActivity onClick() new_bookmark_name = [" + new_bookmark_name + "] currentTrackPosition = [" + currentTrackPosition + "]");
            Bookmark bm = datasource.createBookmark(new_bookmark_name, currentTrack,currentTrackPosition);
            //adapter.add(bm);
            Log.i(TAG,"MyActivity onClick() createdBookmark with name = " + bm.getName());
            adapter.add(bm);
            adapter.notifyDataSetChanged();
            break;
    } // end switch
} // end onClick()

2 个答案:

答案 0 :(得分:1)

您应该在onCreate()

中设置适配器的方式
setListAdapter(adapter);

有关使用ListActivity的详情,请参阅https://developer.android.com/reference/android/app/ListActivity.html

答案 1 :(得分:1)

如果要添加到ListView,则无需获取适配器。您需要添加适配器所持有的List。

这样做:

将值ArrayList声明为类成员:

 public class MyActivity extends ListActivity implements OnClickListener {
 ArrayList<Bookmark> values 

然后在你的onClick:

直接添加到此值ArrayList:

values.add(bm)
正如你所做的那样

和notifyDataSetChanged()。应该工作得很好。

完整代码:

public class MyActivity extends ListActivity implements OnClickListener {
ArrayList<Bookmark> values
... 


@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.bookmarks);

    addButton = (Button) findViewById(R.id.add_button);
    addButton.setOnClickListener(this);

    bookmarkNameEditText = (EditText) findViewById(R.id.editText);
    Intent i = getIntent();
    default_bookmark_name   = i.getStringExtra("default_bookmark_name");
    currentTrack            = i.getIntExtra("currentTrack",0);
    currentTrackPosition    = i.getIntExtra("currentTrackPosition", 0);
    bookmarkNameEditText.setText(default_bookmark_name);

    datasource = new BookmarkDataSource(this);
    datasource.open();

    values = datasource.getAllBookmarks();
    //instantiate custom adapter
    MyCustomAdapter adapter = new MyCustomAdapter(values, this);

    //handle listview and assign adapter
    ListView lView = (ListView)findViewById(android.R.id.list);
    lView.setAdapter(adapter);
}


@Override
public void onClick(View src) {


switch (src.getId()) {
    case R.id.add_button:
        Log.i(TAG, "ADD BUTTON CLICKED");
        String new_bookmark_name = bookmarkNameEditText.getText().toString();
        Log.i(TAG, "MyActivity onClick() new_bookmark_name = [" + new_bookmark_name + "] currentTrackPosition = [" + currentTrackPosition + "]");
        Bookmark bm = datasource.createBookmark(new_bookmark_name, currentTrack,currentTrackPosition);
        //adapter.add(bm);
        Log.i(TAG,"MyActivity onClick() createdBookmark with name = " + bm.getName());
        values.add(bm);
        adapter.notifyDataSetChanged();
        break;
} // end switch
} // end onClick()