我在this教程之后创建了数据库,并且我在主活动中创建了一个按钮,该按钮将用户带到另一个活动,在那里他们将添加一些新项目.但我不知道如何使用ArrayList执行此 *** 作,因为我想在ListVIEw中添加新项目.我希望在我的应用程序中有类似this的东西.任何帮助都会被贬低.
这是DataBase的代码:
public class DatabaseHandler extends sqliteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database name private static final String DATABASE_name = "itemmanager"; // Contacts table name private static final String table_ITEMS = "items"; // Contacts table Columns names private static final String KEY_ID = "ID"; private static final String KEY_Title = "Title"; private static final String KEY_PRICE = "price"; public DatabaseHandler(Context context) { super(context, DATABASE_name, null, DATABASE_VERSION); } // Creating tables @OverrIDe public voID onCreate(sqliteDatabase db) { String CREATE_ITEMS_table = "CREATE table " + table_ITEMS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_Title + " TEXT," + KEY_PRICE + " TEXT" + ")"; db.execsql(CREATE_ITEMS_table); } // Upgrading database @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execsql("DROP table IF EXISTS " + table_ITEMS); // Create tables again onCreate(db); } /** * All CRUD(Create, Read, Update, Delete) Operations */ // Adding new item voID addItem(Item item) { sqliteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_Title, item.getTitle()); // Title name values.put(KEY_PRICE, item.getPrice()); // Price // Inserting Row db.insert(table_ITEMS, null, values); db.close(); // Closing database connection } // Getting item Item getItem(int ID) { sqliteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(table_ITEMS, new String[] { KEY_ID, KEY_Title, KEY_PRICE }, KEY_ID + "=?", new String[] { String.valueOf(ID) }, null, null, null, null); if (cursor != null) cursor.movetoFirst(); Item item = new Item(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)); // return item return item; } // Getting All Items public List<Item> getAllitems() { List<Item> itemsList = new ArrayList<Item>(); // Select All query String selectquery = "SELECT * FROM " + table_ITEMS; sqliteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawquery(selectquery, null); // looPing through all rows and adding to List if (cursor.movetoFirst()) { do { Item item = new Item(); item.setID(Integer.parseInt(cursor.getString(0))); item.setTitle(cursor.getString(1)); item.setPrice(cursor.getString(2)); // Adding contact to List itemsList.add(item); } while (cursor.movetoNext()); } // return items List return itemsList; } // Updating single item public int updateItem(Item item) { sqliteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_Title, item.getTitle()); values.put(KEY_PRICE, item.getPrice()); // updating row return db.update(table_ITEMS, values, KEY_ID + " = ?", new String[] { String.valueOf(item.getID()) }); } // Deleting single item public voID deleteItem(Item item) { sqliteDatabase db = this.getWritableDatabase(); db.delete(table_ITEMS, KEY_ID + " = ?", new String[] { String.valueOf(item.getID()) }); db.close(); } // Getting items Count public int getItemsCount() { String countquery = "SELECT * FROM " + table_ITEMS; sqliteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getCount(); }}
这是MainActivity的代码:
public class MainActivity extends BaseActivity { button addItem; private Toolbar toolbar; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout frameLayout = (FrameLayout)findVIEwByID(R.ID.frame_container); // inflate the custom activity layout LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); VIEw activityVIEw = layoutInflater.inflate(R.layout.activity_main, null,false); frameLayout.addVIEw(activityVIEw); // Setting toolbar toolbar = (Toolbar) findVIEwByID(R.ID.app_bar); setSupportActionbar(toolbar); getSupportActionbar().setdisplayHomeAsUpEnabled(true); getSupportActionbar().setHomebuttonEnabled(true); DatabaseHandler db = new DatabaseHandler(this); addItem = (button) findVIEwByID(R.ID.button1); addItem.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(MainActivity.this, AddActivity.class); startActivity(intent); } }); }}
这是我的主要活动布局:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/mainContent" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <include androID:ID="@+ID/app_bar" layout="@layout/app_bar" /> <button androID:ID="@+ID/button1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:text="@string/button" /> <ListVIEw androID:ID="@+ID/List" androID:layout_margin="5dp" androID:layout_below="@+ID/relativeLayout" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_above="@+ID/button1" androID:layout_centerHorizontal="true" androID:divIDer="@color/List_divIDer_row" androID:divIDerHeight="10.0sp" androID:ListSelector="@drawable/List_row_selector" > </ListVIEw> <relativeLayout androID:ID="@+ID/relativeLayout" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/ListVIEw1" androID:layout_alignRight="@+ID/ListVIEw1" androID:layout_below="@+ID/app_bar" androID:padding="10dp" > <TextVIEw androID:ID="@+ID/item_text" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_alignParenttop="true" androID:layout_marginleft="10dp" androID:layout_margintop="4dp" androID:text="Items" androID:textcolor="#474747" androID:textSize="16sp" /> <TextVIEw androID:ID="@+ID/item_count" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_marginleft="5dp" androID:layout_margintop="4dp" androID:layout_toRightOf="@+ID/item_text" androID:text="(2)" androID:textcolor="#474747" androID:textSize="14sp" /> <TextVIEw androID:ID="@+ID/total_amount" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:text="Rs. 5700" androID:textcolor="#000000" androID:textSize="20dp" /> </relativeLayout></relativeLayout>
解决方法:
简而言之,您需要传递将项目添加回主要活动的“结果”.然后,如果添加了项目,则应调用适配器以更新适配器数据.
// Your prevIoUs code addItem = (button) findVIEwByID(R.ID.button1); addItem.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(MainActivity.this, AddActivity.class); startActivityForResult(intent, ADD_ITEM_REQ_CODE); } }); // Then handle the resultpublic voID onActivityResult(int reqCode, int resultCode, Intent data){ if (reqCode == ADD_ITEM_REQ_CODE && resultCode == RESulT_OK){ // get your ListVIEw adapter and update data mlistadapter.notifyDataSetChanged(); }}
第二个活动,添加项目的活动应该在成功添加项目时调用setResult(RESulT_OK),否则调用setResult(RESulT_CANCELLED).
您的代码还有其他一些功能,例如,您在哪里填充ListVIEw?通常,您可以通过findVIEwByID获取实例,并使用相应的数据为该ListVIEw设置适配器.
老实说,我认为你应该再看看这些教程,特别是这些:
http://developer.android.com/training/basics/intents/result.html
http://developer.android.com/guide/topics/ui/layout/listview.html
以上是内存溢出为你收集整理的java – 如何创建新活动,我可以选择从数据库中添加新项目?全部内容,希望文章能够帮你解决java – 如何创建新活动,我可以选择从数据库中添加新项目?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)