大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android实现记事本功能(26)-创新互联

本文实例为大家分享了Android实现记事本功能的具体代码,供大家参考,具体内容如下

为城阳等地区用户提供了全套网页设计制作服务,及城阳网站建设行业解决方案。主营业务为成都网站建设、成都网站制作、城阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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 List notepadWithDataBeanList;

 @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("确认删除吗?");
  builder.setTitle("提示");
  builder.setPositiveButton("确认", 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();
 }
}


当前名称:Android实现记事本功能(26)-创新互联
本文路径:http://dzwzjz.com/article/cojspp.html
在线咨询
服务热线
服务热线:028-86922220
TOP