简体   繁体   English

从arraylist中删除项目时,无法使用notifydatasetchanged刷新listview

[英]Unable to refresh listview with notifydatasetchanged when deleting item from arraylist

Im having a problem beeing able to refresh my listview when deleting items on longclick. 我在删除longclick上的项目时无法刷新我的列表视图时遇到问题。 It seems to be that the items from the arraylist are being deleted, yet the adapter is'nt refreshing immediately, even though I'm calling adapter.notifydatasetchanged. 似乎是删除了arraylist中的项,但是即使我正在调用adapter.notifydatasetchanged,适配器也不会立即刷新。 Can't figure out why the adapter will not set the new values. 无法弄清楚适配器为什么不设置新值。 Any help very much appreciated. 任何帮助,非常感谢。

public class MySongBookActivity extends ActionBarActivity {
ArrayList<String> test;
ListView list;
private static final String TAG = "MySongBookActivity";
CustomAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_song_book);

    try {
        DataBaseHandler db = new DataBaseHandler(this);
        final ListView list = (ListView) findViewById(R.id.mySongBookListView);
        list.setDivider(null);
        TinyDB tinydb = new TinyDB(getBaseContext());
        final ArrayList<String> test = tinydb.getList("hello");

        String[] values = test.toArray(new String[0]);

        final CustomAdapter adapter = new CustomAdapter(this,
                R.layout.list_item_row, values);

        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String data = (String) list.getItemAtPosition(position);
                data.toString();
                int hej = list.getCheckedItemPosition();

                Intent i = new Intent(MySongBookActivity.this,
                        SBLyricsActivity.class);
                i.putExtra("titelName", data);
                i.putExtra("pos", position);

                startActivity(i);

            }

        });
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> av, View view,
                    int position, long id) {
                return onLongListItemClick(view, position, id);
            }

            protected boolean onLongListItemClick(View view, int position,
                    long id) {
                Log.i(TAG, "onLongListItemClick id=" + position);
                test.remove(position);
                list.setAdapter(adapter);
                adapter.notifyDataSetChanged();

                Log.i("test.size()", "" + test.size());
                return true;
            }

        });



    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_song_book, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my_song_book,
                container, false);
        return rootView;
    }
}

} }

My adapter class: 我的适配器类:

public class CustomAdapter extends BaseAdapter {

private String[] data;
private Context context;

public CustomAdapter(Context context,int resourceId, String[] data1) {
    super();
    this.data = data1;

    this.context = context;
}

@Override
public int getCount() {
    return data.length;
}

@Override
public Object getItem(int position) {
    return data[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = LayoutInflater.from(context).
            inflate(R.layout.list_item_row, parent, false);

    TextView text1 = (TextView) rowView.findViewById(R.id.txtTitle);


    text1.setText(data[position]);


    return rowView;
}

} }

Your adapter is bound to String[] values = test.toArray(new String[0]); 您的adapter绑定到String[] values = test.toArray(new String[0]); , that is a copy of test . ,那是test的副本。 So when you change test , values don't change and so doesn't the content of the list . 因此,当您更改testvalues不会更改, list的内容也不会更改。

What you can do is to bind adapter to the ArrayList itself: 您可以做的是将适配器绑定到ArrayList本身:

public CustomAdapter(Context context,int resourceId, ArrayList<String> data1) {
    this.data = data1; // this.data should be List<String>
    this.context = context;
}

// change other methods accordingly

So to initialize the adapter you do: 因此,要初始化适配器,请执行以下操作:

final ArrayList<String> test = tinydb.getList("hello");

final CustomAdapter adapter = new CustomAdapter(this,
        R.layout.list_item_row, test);

Also, you don't have to call list.setAdapter(adapter); 另外,您不必调用list.setAdapter(adapter); in onLongListItemClick() (I think you did it out of despair) onLongListItemClick() (我认为您是出于绝望)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM