- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的主 Activity 有一个 ListView ,显示数据库中的项目。我在操作栏中有一个添加按钮。单击添加按钮时,会弹出一个对话框,用户填写新项目的字段,然后单击“添加”,它将项目添加到数据库中。唯一的问题是 mainactivity 上的 ListView 不会更新以显示新项目。我可以关闭应用程序并重新打开它,然后我会看到新项目。它只是不会立即更新。 (与删除项目相同的问题,只是删除发生在长按上)
主 Activity .java
package blah.blah.blah
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
public class InitActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Gets the data repository in write mode
PlayersDBHelper mDbHelper = new PlayersDBHelper(getBaseContext());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_init);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}//End onCreate()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.init, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_add:
showAddPlayerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void showAddPlayerDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new addPlayerDialog();
dialog.show(this.getFragmentManager(), "addPlayerFragment");
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0) {
Fragment playersFragment = new PlayersFragment();
return playersFragment;
} else if(position == 1){
Fragment otherFragment= new otherFragment();
return otherFragment;
} else {
Fragment otherFragment2 = new otherFragment2();
return otherFragment2;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}//End sectionsPagerAdapter()
public static class PlayersFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public int myFragmentId = 1;
private ListView mylistview;
private String[] values;
public ArrayAdapter<String> adapter;
public PlayersFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_players,
container, false);
mylistview = (ListView) rootView.findViewById(R.id.myListView);
registerForContextMenu(mylistview);
PlayersDBHelper mDbHelper = new PlayersDBHelper(rootView.getContext());
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
PlayerEntry._ID,
PlayerEntry.COLUMN_NAME_ID,
PlayerEntry.COLUMN_NAME_NAME,
PlayerEntry.COLUMN_NAME_POSITION
};
String selection = null; //Null will return all rows for given table
String[] selectionArgs = null; //Null should return all data
// How you want the results sorted in the resulting Cursor
String sortOrder =
PlayerEntry.COLUMN_NAME_NAME + " DESC";
Cursor c = db.query(
PlayerEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
values = new String[] {};
String array[] = new String[c.getCount()];
int i = 0;
c.moveToFirst();
while (c.isAfterLast() == false) {
array[i] = c.getString(c.getColumnIndexOrThrow(PlayerEntry.COLUMN_NAME_NAME));
i++;
c.moveToNext();
}
for(int x = 0; x < array.length ; x++){
Log.d("Logan", "Entry at:" + x + " is " + array[x]);
values = push(values, array[x]);
}
adapter = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1, values);
mylistview.setAdapter(adapter);
return rootView;
}
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.myListView) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(values[info.position]);
String[] menuItems = {"Edit", "Delete"};
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
PlayersDBHelper mDbHelper = new PlayersDBHelper(getActivity());
final SQLiteDatabase db = mDbHelper.getWritableDatabase();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = {"Edit", "Delete"};
String menuItemName = menuItems[menuItemIndex];
String listItemName = values[info.position];
if(menuItemName.equalsIgnoreCase("Edit")) {
} else {
//menuItemName === Delete
// Define 'where' part of query.
String selection = PlayerEntry.COLUMN_NAME_NAME + " =? ";
// Specify arguments in placeholder order.
String[] selectionArgs = { listItemName };
// Issue SQL statement.
db.delete(PlayerEntry.TABLE_NAME, selection, selectionArgs);
adapter.notifyDataSetChanged();
}
return true;
}
}
public static class otherFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public otherFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_lineup,
container, false);
return rootView;
}
}
public static class otherFragment2 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public otherFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_position,
container, false);
return rootView;
}
}
}
addPlayerDialog.java
package blah.blah.blah;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
public class addPlayerDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Gets the data repository in write mode
PlayersDBHelper mDbHelper = new PlayersDBHelper(getActivity().getBaseContext());
final SQLiteDatabase db = mDbHelper.getWritableDatabase();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
final LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View view = inflater.inflate(R.layout.addplayerdialog, null);
final ListView list = (ListView)view.findViewById(R.id.myListView);
builder.setView(view)
// Add action buttons
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
EditText fName = (EditText) view.findViewById(R.id.editFirstName);
Editable firstName = fName.getText();
EditText lName = (EditText) view.findViewById(R.id.editLastName);
Editable lastName = lName.getText();
EditText number = (EditText) view.findViewById(R.id.playerNumber);
int num = Integer.parseInt(number.getText().toString());
Spinner spinner = (Spinner) view.findViewById(R.id.positionSpinner);
String position = spinner.getSelectedItem().toString();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(PlayerEntry.COLUMN_NAME_ID, num);
values.put(PlayerEntry.COLUMN_NAME_NAME, firstName + " " + lastName);
values.put(PlayerEntry.COLUMN_NAME_POSITION, position);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
PlayerEntry.TABLE_NAME,
null,
values);
//**** HERE IS WHERE I THINK THE CHANGE NEEDS TO BE! ****
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Spinner spinner = (Spinner) view.findViewById(R.id.positionSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.positions, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return builder.create();
}
}
addPlayerDialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/fName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/lName" />
<EditText
android:id="@+id/playerNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/playerNumberHint"
android:inputType="number" />
<Spinner
android:id="@+id/positionSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
最佳答案
我认为您将数据插入了数据库,但忘记在调用 notifyDataSetChanged
之前调用 yourListViewAdapter.add
方法
关于弹出对话框更新数据库后的Android更新Mainactivity ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812361/
我有一个删除按钮,单击该按钮时我希望弹出一个对话框,然后单击“确定”它应该执行 Ajax 调用,否则不应该执行任何操作。这是代码 $('.comment-delete').click(function
public void exitGame() { //pop up dialogue Platform.exit(); } 我已经尝试了很多我在互联网上看到的不同的东西,但我什么都做不了。我所
我有一个典型的素面对话框,效果很好,但是当有人在对话框外单击时,我找不到任何关闭它的选项。我看到了一些jquery示例,我想我可以将其改编为primefaces对话框,但首先要确保还没有解决方案? 谢
我试图让 jquery 对话框在单击按钮时启动,但似乎不起作用。任何帮助将不胜感激: $('#wrapper').dialog({ autoOpen: false,
我试图单独更改标题栏颜色。所以我使用了 .ui-dialog-titlebar ,但它不起作用,所以我尝试使用 ui-widght-header ,它也反射(reflect)到数据表..请告知。 //
我的页面上有 div(box),我正在使用此脚本将 div 显示为对话框。在该 div 内,我有一个超链接,单击该超链接时,我想淡出对话框并关闭。对话框的内容淡出,但对话框的边框保持不变。如果我将 $
我当前有一个对话框,其内容有两个输入(这两个输入使用 .datepicker())。当我打开对话框时,第一个输入成为焦点,并且第一个日期选择器自动出现。我尝试隐藏 div 并模糊输入,但这会导致日期选
我想即时创建一个 jQuery 对话框。我正在使用这个: var newDiv = $(document.createElement('div')); $(newDiv).html('hello th
child: RaisedButton( color: const Color(0xFF5867DD), onPressed: (){ updateProfilePic();
我有下面的 jquery 代码,我已根据我的要求对其进行了自定义,但存在一些问题。首先,用户单击“单击此处”,不会显示对话框。当用户单击“关闭”时,对话框不会消失。非常感谢您提供的所有帮助。
如何创建一个对话框,该对话框的顶部有一个文本,其下方有一个空白区域,用户可以在其中键入内容,在右侧下方有一个 OKAY 按钮,当您单击该按钮时,对话框消失? 像这样: 最佳答案 String inpu
这是一个简单得多的问题。 private static AplotBaseDialog dlg; public Object execute(final ExecutionEvent event) t
我正在为我的应用程序开发小部件。应该有一些小部件可以实现相同的功能,唯一的区别在于它们的布局(主题/外观) 我会创建一个对话框或屏幕,用户可以在其中选择他喜欢的小部件。当我选择它们时,我在很多小部件中
我有 jQuery 对话框窗口,在某些操作中我有一个加载的 blockUI 插件。 我面临的问题是,即使 AJAX 图像仍在显示,我仍然能够在执行 ajax 操作时执行操作。 另一方面,JSP 页面在
我非常熟悉将 jQuery 对话框 div 设置为可见后将其附加到表单元素的技巧。我已经在 .NET 中这样做了一百次左右,而且效果很好!然而,我正在尝试在 Coldfusion 网站上执行此操作,这
我想使用jquery对话框来收集用户信息(例如用户名)。我如何使用 Jquery 做到这一点并将数据收集到 Javascript 变量中? 这是我迄今为止的尝试: // Dialog here, ho
如何设置 jquery 对话框按钮的工具提示?请参阅下面的内容...这里没有 id 或样式类。 jQuery("#dialog-form").dialog ({ autoOpen: false,
我有调用对话框的 JS 函数 function SomeFunction { $('#editformdialog').dialog('open'); } 这显然已经简化了。但是,我得到 a is u
我正在使用 jquery 模式对话框来显示部分 View 中的数据表。在部分 View 中,我有一些脚本,用于将 HTML 表更改为 jquery DataTables。因此,我需要确保表格在对话框中
我正在尝试添加透明的 JQuery 对话框。但我遇到了两个问题: 文本“Hello”的背景不会变得透明 删除标题栏后,我无法再拖动对话框 这些评论是我迄今为止尝试过的。 //Create ne
我是一名优秀的程序员,十分优秀!