samedi 12 décembre 2015

pass data into/and start of new activity from listview item selection in alert dialog

In this app there is an alert dialog that pops up when either the left nav bar or action bar New List button is selected. The alert dialog contains a list of created projects, so you have the option to select a project or to create a project. The list of projects populate depending on what is in the SQLite database.

A little background, the main activity has a listview that displays a list of these same projects. Currently I am able to select a project from the list, pass the row data, start a new activity, and populate text views in the new activity with the data from the selected project into another activity.

I need to do the same thing here, but from the alert dialog listview. It needs to pass the project name into the newly started New List activity and display it either in the action bar or in a text view.

As you can see in the alert dialog for the left nav bar, if position 2 is selected the alert dialog comes up and the list populates from the database. The displayToastForId(idInDB); works, so I know I am accessing the data. When I try to use passListName(idInDB); it doesn't do anything.

I've searched and experimented for a week on this now. What am I doing wrong?

Intent i;    

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

    getSupportActionBar().setTitle("Home");


    mDrawerList = (ListView)findViewById(R.id.navList);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

    addDrawerItems();
    setupDrawer();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    registerListClickCallbackMainList();
    try {
        openDB();
        populateProjects();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    i = new Intent(this, NewList.class);

}




//Starting New List activity and passing project name to edit text
public void passListName(long idInDB) throws SQLException {
    Cursor cursor = NewProjectDatabaseAdapter.getListInfo(idInDB);
    if (cursor.moveToFirst()) {
        String projectName = cursor.getString(NewProjectDatabaseAdapter.COL_LIST_PROJECT_NAME);

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("project_name", String.valueOf(projectName));
        startActivity(i);
    }
    cursor.close();
}
//end of passing data




/////////////////toast for item clicked on listview////////////////////
//gets all data from row clicked
public void displayToastForId(long idInDB) throws SQLException {
    Cursor cursor = NewProjectDatabaseAdapter.getProjectInfo(idInDB);
    if (cursor.moveToFirst()) {
        long idDB = cursor.getLong(NewProjectDatabaseAdapter.COL_PROJECT_ROWID);
        String project_name = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_NAME);
        String address = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_ADDRESS);
        String job_number = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_JOB_NUMBER);
        String site_contact = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_SITE_CONTACT);
        String phone = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_SITE_CONTACT_PHONE_NUMBER);
        String email = cursor.getString(NewProjectDatabaseAdapter.COL_PROJECT_SITE_CONTACT_EMAIL_ADDRESS);

        String message = "ID: " + idDB + "\n"
                + "Size: " + project_name + "\n"
                + "Type: " + address + "\n"
                + "Length: " + job_number + "\n"
                + "Color: " + site_contact + "\n"
                + "Color: " + phone + "\n"
                + "Cut: " + email;
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }
    cursor.close();
}
/////////////////////end of toast for item clicked on listview//////////////////////




////////////////////////////////////////////////////////////////
//This code adds the drawer items to the left navigation bar.
///////////////////////////////////////////////////////////////
public void addDrawerItems() {
    String[] osArray = { "Home", "Search", "New List", "New Project", "Reminders", "Preferences", "Help" };
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);


    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {

            DrawerLayout mDrawerLayout;
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


            if (position == 0){
//Do nothing if left nav
            }
            if (position == 1){
                Intent myIntent = new Intent(view.getContext(), Search.class);
                startActivityForResult(myIntent, 0);
                mDrawerLayout.closeDrawers();
                finish();
            }
            if (position == 2){


                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = inflater.inflate(R.layout.new_list_custom_dialog, null);
                alertDialog.setView(convertView);
                alertDialog.setPositiveButton("Create", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface alert, int which) {
//Do something here on click

                        startActivity(new Intent(MainActivity.this, NewProject.class));
                        finish();
                        alert.dismiss();

                    }


                });
                alertDialog.setTitle("Select a Project");
                try{
                    openDB();


                } catch (SQLException e){
                    e.printStackTrace();
                }
//Testing fetching projects info from database to populate list
                Cursor cursor1 = NewProjectDatabaseAdapter.fetchProjectName();

                startManagingCursor(cursor1);


///setup mapping from cursor to view fields//////
                String[] fromDatabase = new String[]
                        {NewProjectDatabaseAdapter.KEY_PROJECT_NAME};

                int[] toList = new int[]
                        {R.id.alert_dialog_project_name};

////create adapter to map columns of database to rows in listview
                @SuppressWarnings("deprecation") final
                SimpleCursorAdapter myCursorAdapter1 =
                        new SimpleCursorAdapter(
                                MainActivity.this,
                                R.layout.main_activity_projects_alert_dialog_list_item,
                                cursor1,
                                fromDatabase,
                                toList,
                                0
                        );

                ListView lv = (ListView) convertView.findViewById(R.id.listView1);

                lv.setAdapter(myCursorAdapter1);





                alertDialog.show();

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View viewClicked,
                                            int position, long idInDB) {

                        try {
                            displayToastForId(idInDB);
                            //passListName(idInDB);




                             } catch (SQLException e) {
                            e.printStackTrace();
                        }


                    }
                });


            }
            if (position == 3){
                Intent myIntent = new Intent(view.getContext(), NewProject.class);
                startActivityForResult(myIntent, 0);
                mDrawerLayout.closeDrawers();
                finish();
            }
            if (position == 4){
                Intent myIntent = new Intent(view.getContext(), Reminders.class);
                startActivityForResult(myIntent, 0);
                mDrawerLayout.closeDrawers();
                finish();
            }
            if (position == 5){

            }
            if (position == 6){

            }
            if (position == 7){

            }
            if (position == 8){

            }

        }
    });
}

//End of adding items to left nav bar////////////////////////////////////////

Aucun commentaire:

Enregistrer un commentaire