我正在尝试将工具栏添加到android studio中的listview ...我明白listview不支持工具栏,我应该使用appcompatactivity。我试图改变列表视图,我得到了很多错误...我已经看了大约80个不同的样本,并且无法弄清楚我做错了什么....如果有人曾经使用过工具栏和列表视图我会喜欢你能给予的任何额外帮助....
这是我当前的列表视图代码
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();
}
}
这是我希望代码看起来像但它不起作用
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;
}
}
任何帮助都会非常感激我现在已经把头发拉了几个小时......
答案 0 :(得分:1)
这样做(使用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);
}
}
在布局文件中更改listview的id
<ListView
android:id="@+id/cmListView"
android:height="" ....../>