skapaid skapaid - 4 years ago 228
Android Question

how to add toolbar to listview

i am trying to add a toolbar to a listview in android studio... i understand that listview does not support toolbar and i should use appcompatactivity. I have tried to change the listview and i get numerous errors... i have looked at about 80 different samples and cant figure out what i am doing wrong at all.... if anyone has ever used a toolbar and a listview i would love any extra help you could give....

here is my current listview code

package mypackage.android;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import mypackage.android.database.CommonNamesAdapter;

public class CommonNames extends ListActivity {

CommonNamesAdapter cnTable;
ListView cnListView;
Cursor c;

private static final int COMMON_NAME_ACTIVITY_START = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.common_names_list);

cnTable = new CommonNamesAdapter();
cnTable.open(getApplicationContext());

c = cnTable.fetchAllCommonNames();
startManagingCursor(c);

if(c!=null){
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.common_names_row,
c,
new String[] {c.getColumnName(1)},
new int[] {R.id.commonName});
setListAdapter(adapter);
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

c.moveToPosition(position);

Intent i = new Intent(this, CommonNameDescription.class);
i.putExtra(CommonNamesAdapter.COMMON_NAME_ROWID, id);
i.putExtra(CommonNamesAdapter.COMMON_NAME, c.getString(
c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME)));
i.putExtra(CommonNamesAdapter.COMMON_NAME_COUNT, c.getString(
c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME_COUNT)));
startActivityForResult(i, COMMON_NAME_ACTIVITY_START);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//Bundle extras = intent.getExtras();
switch(requestCode) {
default: break;
}
}

@Override
protected void onDestroy(){
super.onDestroy();
c.close();
cnTable.close();
}

}


here is what i would like the code to look like but it doesnt work

package mypackage.android;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import mypackage.android.database.CommonNamesAdapter;

public class CommonNames extends AppCompatActivity {

CommonNamesAdapter cnTable;
ListView cnListView;
Cursor c;

private static final int COMMON_NAME_ACTIVITY_START = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.common_names_list);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

cnTable = new CommonNamesAdapter();
cnTable.open(getApplicationContext());

c = cnTable.fetchAllCommonNames();
startManagingCursor(c);

if(c!=null){
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.common_names_row,
c,
new String[] {c.getColumnName(1)},
new int[] {R.id.commonName});
setListAdapter(adapter);
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

c.moveToPosition(position);

Intent i = new Intent(this, CommonNameDescription.class);
i.putExtra(CommonNamesAdapter.COMMON_NAME_ROWID, id);
i.putExtra(CommonNamesAdapter.COMMON_NAME, c.getString(
c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME)));
i.putExtra(CommonNamesAdapter.COMMON_NAME_COUNT, c.getString(
c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME_COUNT)));
startActivityForResult(i, COMMON_NAME_ACTIVITY_START);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//Bundle extras = intent.getExtras();
switch(requestCode) {
default: break;
}
}

@Override
protected void onDestroy(){
super.onDestroy();
c.close();
cnTable.close();
}

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

return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean bRet=false;//set true is menu selection handled
switch (item.getItemId()) {
case R.id.action_settings_get_pro:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent);
bRet=true;
break;
case R.id.action_settings_get_pro2:
Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
startActivity(intent2);
bRet=true;
break;
case R.id.action_settings_app_help:
Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_about_app:
Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
bRet=true;
break;
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
startActivity(intent3);
bRet=true;
break;
case R.id.action_settings_privacy_policy:
Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
startActivity(intentprivacy);
bRet=true;
break;
case R.id.action_settings_all_our_apps:
Intent intent4 = new Intent(Intent.ACTION_VIEW);
intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
startActivity(intent4);
bRet=true;
break;
default:
bRet=super.onOptionsItemSelected(item);
}
return bRet;
}


}


any help would be greatly appreciated i have been pulling my hair out for hours now...

Answer Source

Do like this(Use AppCompatActivity)

public class CommonNames extends AppCompatActivity implements AdapterView.OnItemClickListener {

        CommonNamesAdapter cnTable;
        ListView cnListView;
        Cursor c;

        private static final int COMMON_NAME_ACTIVITY_START = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.common_names_list);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            cnListView= (ListView) findViewById(R.id.cnListView);

            cnTable = new CommonNamesAdapter();
            cnTable.open(getApplicationContext());

            c = cnTable.fetchAllCommonNames();
            startManagingCursor(c);

            if(c!=null){
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                        R.layout.common_names_row,
                        c,
                        new String[] {c.getColumnName(1)},
                        new int[] {R.id.commonName});
                cnListView.setAdapter(adapter);
                cnListView.setOnItemClickListener(this);

            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            //Bundle extras = intent.getExtras();
            switch(requestCode) {
                default: break;
            }
        }

        @Override
        protected void onDestroy(){
            super.onDestroy();
            c.close();
            cnTable.close();
        }

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

            return true;
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            boolean bRet=false;//set true is menu selection handled
            switch (item.getItemId()) {
                case R.id.action_settings_get_pro:
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(getString(R.string.pro_version_url)));
                    startActivity(intent);
                    bRet=true;
                    break;
                case R.id.action_settings_get_pro2:
                    Intent intent2 = new Intent(Intent.ACTION_VIEW);
                    intent2.setData(Uri.parse(getString(R.string.pro_version_url)));
                    startActivity(intent2);
                    bRet=true;
                    break;
                case R.id.action_settings_app_help:
                    Toast.makeText(this, this.getString(R.string.action_settings_app_help_text), Toast.LENGTH_SHORT).show();
                    bRet=true;
                    break;
                case R.id.action_settings_about_app:
                    Toast.makeText(this, this.getString(R.string.action_settings_about_text), Toast.LENGTH_SHORT).show();
                    bRet=true;
                    break;
                case R.id.action_settings_rate_app:
                    Intent intent3 = new Intent(Intent.ACTION_VIEW);
                    intent3.setData(Uri.parse(getString(R.string.rate_this_app_url)));
                    startActivity(intent3);
                    bRet=true;
                    break;
                case R.id.action_settings_privacy_policy:
                    Intent intentprivacy = new Intent(Intent.ACTION_VIEW);
                    intentprivacy.setData(Uri.parse(getString(R.string.privacy_policy_url)));
                    startActivity(intentprivacy);
                    bRet=true;
                    break;
                case R.id.action_settings_all_our_apps:
                    Intent intent4 = new Intent(Intent.ACTION_VIEW);
                    intent4.setData(Uri.parse(getString(R.string.all_our_apps_url)));
                    startActivity(intent4);
                    bRet=true;
                    break;
                default:
                    bRet=super.onOptionsItemSelected(item);
            }
            return bRet;
        }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        c.moveToPosition(position);

        Intent i = new Intent(this, CommonNameDescription.class);
        i.putExtra(CommonNamesAdapter.COMMON_NAME_ROWID, id);
        i.putExtra(CommonNamesAdapter.COMMON_NAME, c.getString(
                c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME)));
        i.putExtra(CommonNamesAdapter.COMMON_NAME_COUNT, c.getString(
                c.getColumnIndexOrThrow(CommonNamesAdapter.COMMON_NAME_COUNT)));
        startActivityForResult(i, COMMON_NAME_ACTIVITY_START);
    }
}

And in the layout file change the id of listview

<ListView 
android:id="@+id/cmListView"
android:height="" ....../>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download