如何将特定的listview项传递给ActionBar CallBack类

时间:2014-01-18 16:47:34

标签: android android-actionbar

我正在创建一个Android应用程序,其中json数据通过API解析并存储在数据库中。并且存储的数据将显示在列表视图中。下面显示的代码用于显示商店数据。

我的问题是如何在ActionBarCallback类中获取所选的listview项目,以便在长按时我可以通过操作栏图标删除传递的项目。

package com.async2.example;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.async2.example.db.DBcontact;

public class SQLView extends Activity {
    static final String ID = "id";
    static final String NAME = "name";
    static final String ADDRESS = "address";
    static final String GENDER = "gender";
    static final String EMAIL = "email";
    static final String MOBILE = "mobile";

    private ActionMode mActionMode;

    ArrayList<HashMap<String, String>> listFill;
    ListView lv;
    DBcontact db;
    Cursor data;
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listFill = new ArrayList<HashMap<String, String>>();
        lv = (ListView) findViewById(R.id.listView1);
        db = new DBcontact(getApplicationContext());
        db.open();

        data = db.getInfo();

        String[] id = new String[data.getCount()];
        String[] name = new String[data.getCount()];
        String[] address = new String[data.getCount()];
        String[] email = new String[data.getCount()];
        String[] gender = new String[data.getCount()];
        String[] mobile = new String[data.getCount()];

        data.moveToFirst();
        for (int i = 0; i < data.getCount(); i++) {

            String d = data.getString(0);
            String n = data.getString(1);
            String a = data.getString(2);
            String e = data.getString(3);
            String g = data.getString(4);
            String m = data.getString(5);

            id[i] = d;
            name[i] = n;
            address[i] = a;
            email[i] = e;
            gender[i] = g;
            mobile[i] = m;

            data.moveToNext();
        }
        data.close();

        for (int i = 0; i < name.length; i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(ID, id[i]);
            map.put(NAME, name[i]);
            map.put(ADDRESS, address[i]);
            map.put(EMAIL, email[i]);
            map.put(GENDER, gender[i]);
            map.put(MOBILE, mobile[i]);
            // System.out.println("name:" + map.get("NAME"));
            listFill.add(map);
        }

        final MyCustomAdapter adapter;
        adapter = new MyCustomAdapter(getApplicationContext(), listFill);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub


                String rid = ((TextView) view.findViewById(R.id.list_id))
                        .getText().toString();
                String name = ((TextView) view.findViewById(R.id.list_name))
                        .getText().toString();
                String email = ((TextView) view.findViewById(R.id.list_email))
                        .getText().toString();
                String address = ((TextView) view
                        .findViewById(R.id.list_address)).getText().toString();
                String gender = ((TextView) view.findViewById(R.id.list_gender))
                        .getText().toString();
                String mobile = ((TextView) view.findViewById(R.id.list_mobile))
                        .getText().toString();

                // Starting single contact activity
                Intent in = new Intent(getApplicationContext(), EditPage.class);

                in.putExtra(ID, rid);
                in.putExtra(NAME, name);
                in.putExtra(ADDRESS, address);
                in.putExtra(GENDER, gender);
                in.putExtra(EMAIL, email);
                in.putExtra(MOBILE, mobile);
                startActivity(in);
            }
        });

        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view,
                    int position, long arg3) {
                // // TODO Auto-generated method stub
                mActionMode = SQLView.this
                        .startActionMode(new ActionBarCallBack());

                return true;

            }
        });

        // adapter.notifyDataSetChanged();
    }

    class ActionBarCallBack implements ActionMode.Callback {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub

            switch (item.getItemId()) {
            case R.id.item_delete:


                        DBcontact ex = new DBcontact(getApplicationContext());

                        ex.open();
                        ex.deleteEntry();
                        ex.close();


                Toast.makeText(SQLView.this, "deleted", Toast.LENGTH_LONG)
                        .show();
                //

                mode.finish();
                return true;
            case R.id.item_edit:
                Toast.makeText(getApplicationContext(), "edited",
                        Toast.LENGTH_SHORT).show();
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            mode.getMenuInflater().inflate(R.menu.contextual, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub

            mode.setTitle("ListView is Clicked");

            return false;
        }

    }

    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        db.close();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        db.close();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        db.open();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contextual_menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            Intent i = new Intent(SQLView.this, SQLView.class);
            startActivity(i);
            Toast.makeText(this, "refreshed", Toast.LENGTH_SHORT).show();

            break;

        default:
            break;
        }

        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

ActionbarCallback创建一个构造函数,将该项作为参数。

onItemLongClick()可能如下所示:

       mActionMode = SQLView.this
                    .startActionMode(new ActionBarCallBack(adapter.getItem(position)));

您的适配器当然必须正确实现getItem()