0

I'am creating a android app where json data is parsed through API and stored in database. And that stored data is displayed to the listview. The code shown below is used for displaying the store data.

My question is how can I get the selected listview item in ActionBarCallback class so that when long pressed I can able to delete the passed item through action bar icon.

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;
    }
}
user3201163
  • 7
  • 1
  • 5

1 Answers1

0

Create a constructor for ActionbarCallback that takes the item as an argument.

onItemLongClick() could look like this:

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

Your adapter has to properly implement getItem() of course.

fweigl
  • 21,278
  • 20
  • 114
  • 205