android:单击列表项中的按钮刷新listview

时间:2014-03-13 12:03:25

标签: android database android-listview

我有一个listview,它从数据库中检索标记为收藏夹的行,即在“收藏夹”栏中有布尔值1的行。它有一个textview和一个按钮。单击该按钮后,该特定项目将从收藏夹中删除,即数据库中的该行将更新,其最喜欢的值将设置为布尔值0.这一点非常有效。但我希望在此之后刷新列表视图,以便特定项目从列表视图中消失。我尝试使用notifyDataSetChanged();,但它不起作用。我该怎么做?代码的相关部分粘贴在下面:

活动类:

public class ListActivity extends Activity{
    public Cursor c;
    public int lcid;
    private ListView lView;
    private ArrayList<ListItemNew> newList;
    private ListNewAdapter mAdapterNew;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Context context = getApplicationContext();
    setContentView(R.layout.favs);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    final DataBaseHelper db = new DataBaseHelper(context);
    try {db.createDataBase();} catch (IOException e) {e.printStackTrace();}
    db.openDataBase();  

    newList = new ArrayList<ListItemNew>();

    c=db.getFavs();
    prepareList();
    db.close();

    lView = (ListView) findViewById(R.id.listView1);

    mAdapterNew = new ListNewAdapter(getApplicationContext(), newList);
    lView.setAdapter(mAdapterNew); 
    lView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
        mAdapterNew.notifyDataSetChanged();
        lView.setAdapter(mAdapterNew);
        final Intent k = new Intent(ListActivity.this, MainActivity.class);
    k.putExtra("id", x);
        finish();
        startActivity(k);
      }
    });



}

public void prepareList()
{
      c.moveToFirst();
      do{
          newList.add(new ListItemNew(c.getString(0), c.getString(1)));
      }while (c.moveToNext()); 

}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return true;
}



}

适配器类:

public class ListNewAdapter extends BaseAdapter {
     public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item_new, null);
        }

        TextView txtTitle = (TextView) convertView.findViewById(R.id.list_new_title);
        txtTitle.setText(newListItems.get(position).getTitle());
        Button imgBtn = (Button) convertView.findViewById(R.id.delete_btn);
        //.......declarations etc

        imgBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
                alertbox.setMessage("Are you sure you wanna remove this item from your favorites?");
                alertbox.setTitle("Warning");
                alertbox.setIcon(R.drawable.trn_03);

                alertbox.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {

                                final DataBaseHelper db = new DataBaseHelper(context);
                                try {db.createDataBase();} catch (IOException e) {e.printStackTrace();}
                                db.openDataBase(); 
                                final int newid = Integer.valueOf(id);
                                db.UpdateRemoveFav(newid); 
                                db.close();
                                notifyDataSetChanged();
                            }
                        });
                alertbox.setNegativeButton("No", null);
                alertbox.show();

            }
        });

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

mAdapterNew = new ListNewAdapter(getApplicationContext(), newList);
lView.notifyDataSetChanged();
lView.setAdapter(mAdapterNew); 
lView.setOnItemClickListener(new OnItemClickListener() {
 lView.notifyDataSetChanged();
lView.setAdapter(mAdapterNew);
//go to another activity
});