本文實(shí)例為大家分享了Android實(shí)現(xiàn)記事本功能的具體代碼,供大家參考,具體內(nèi)容如下
為城陽(yáng)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及城陽(yáng)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、城陽(yáng)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!MainActivity.java代碼:
package siso.smartnotef.activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import siso.smartnotef.R; import siso.smartnotef.adapter.NotepadeAdapter; import siso.smartnotef.db.DataHelper; import siso.smartnotef.global.GlobalParams; import siso.smartnotef.model.NotepadBean; import siso.smartnotef.model.NotepadWithDataBean; import siso.smartnotef.service.MainService; public class MainActivity extends AppCompatActivity implements View.OnClickListener, NotepadeAdapter.ClickFunction { private TextView tv_add; private ListView lv_contents; private ListnotepadWithDataBeanList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent1 = new Intent(MainActivity.this, MainService.class); startService(intent1); findViews(); setListeners(); initView(); } private void findViews() { tv_add = (TextView) findViewById(R.id.tv_add); lv_contents = (ListView) findViewById(R.id.lv_content); } private void setListeners() { tv_add.setOnClickListener(this); } private void initView() { DataHelper helper = new DataHelper(MainActivity.this); notepadWithDataBeanList = new ArrayList (); List notepadBeanList = helper.getNotepadList(); for (int i = 0; i < notepadBeanList.size(); i++) { if (0 == notepadWithDataBeanList.size()) { NotepadWithDataBean notepadWithDataBean = new NotepadWithDataBean(); notepadWithDataBean.setData(notepadBeanList.get(0).getDate()); notepadWithDataBeanList.add(notepadWithDataBean); } boolean flag = true; for (int j = 0; j < notepadWithDataBeanList.size(); j++) { int date = notepadWithDataBeanList.get(j).getData(); if (date == notepadBeanList.get(i).getDate()) { notepadWithDataBeanList.get(j).getNotepadBeenList().add(notepadBeanList.get(i)); flag = false; break; } } if (flag) { NotepadWithDataBean notepadWithDataBean = new NotepadWithDataBean(); notepadWithDataBean.setData(notepadBeanList.get(i).getDate()); notepadWithDataBeanList.add(notepadWithDataBean); notepadWithDataBeanList.get(notepadWithDataBeanList.size() - 1).getNotepadBeenList().add(notepadBeanList.get(i)); } } NotepadeAdapter adapter = new NotepadeAdapter(MainActivity.this, notepadWithDataBeanList, this); lv_contents.setAdapter(adapter); // setListViewHeightBasedOnChildren(lv_contents); } public void setListViewHeightBasedOnChildren(ListView listView) { if (listView == null) return; ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_add: Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(GlobalParams.TYPE_KEY, GlobalParams.TYPE_ADD); intent.putExtras(bundle); intent.setClass(MainActivity.this, AddContentActivity.class); startActivityForResult(intent, GlobalParams.ADD_REQUEST); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case GlobalParams.ADD_REQUEST: if (GlobalParams.ADD_RESULT_OK == resultCode) { initView(); } break; } } @Override public void clickItem(int position, int itemPosition) { Bundle bundle = new Bundle(); bundle.putInt(GlobalParams.TYPE_KEY, GlobalParams.TYPE_EDIT); bundle.putSerializable(GlobalParams.BEAN_KEY, notepadWithDataBeanList.get(position)); bundle.putInt(GlobalParams.ITEM_POSITION_KEY, itemPosition); Intent intent = new Intent(this, AddContentActivity.class); intent.putExtras(bundle); startActivityForResult(intent, GlobalParams.ADD_REQUEST); } @Override public void longClickItem(final int position, final int itemPostion) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("確認(rèn)刪除嗎?"); builder.setTitle("提示"); builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DataHelper helper = new DataHelper(MainActivity.this); helper.deleteNotepad(notepadWithDataBeanList.get(position).getNotepadBeenList().get(itemPostion).getId()); initView(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }