package com.yizhen.note;
import java.util.ArrayList;
import java.util.List;
import com.yizhen.note.model.Note;
import com.yizhen.note.sqlite.DBManage;
import com.yizhen.note.util.ToastUtil;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class HomepageActivity extends Activity implements OnClickListener {
private Context mContext = HomepageActivity.this;
private TextView homepage_add;
private ListView eventList;
private TextView homepage_search;
private EventAdapter eventAdaper = new EventAdapter();
DBManage db = new DBManage(mContext);
// 事件集合
ArrayList<Note> noteList = new ArrayList<Note>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
homepage_add = (TextView) findViewById(R.id.homepage_add);
eventList = (ListView) findViewById(R.id.eventList);
homepage_search = (TextView) findViewById(R.id.homepage_search);
homepage_add.setOnClickListener(this);
homepage_search.setOnClickListener(this);
List<Note> list=new ArrayList<Note>();
try{
list = db.findAll();
noteList.addAll(list);
eventList.setAdapter(eventAdaper);
for (Note n : list) {
System.out.println("--------" + n.getTitle() + "," + n.getContent());
}
}catch(Exception e){
e.printStackTrace();
ToastUtil.toastShow(mContext, "你还没有记录过事情");
}
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.homepage_add:
intent = new Intent(mContext, AddEventActivity.class);
startActivity(intent);
break;
case R.id.homepage_search:
intent = new Intent(mContext, SearchEventActivity.class);
startActivity(intent);
break;
}
}
public void eventAdapter() {
noteList.clear();
noteList.addAll(db.findAll());
eventAdaper.notifyDataSetChanged();
}
class ViewHolder {
TextView homepage_title;
TextView homepage_delete;
TextView homepage_edt;
TextView homepage_content;
TextView homepage_time;
}
//适配器
class EventAdapter extends BaseAdapter {
ViewHolder viewHolder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView homepage_title = null;
TextView homepage_content = null;
TextView homepage_time = null;
TextView homepage_delete = null;
TextView homepage_edt = null;
LinearLayout eventitem = null;
final Note note = noteList.get(position);
System.out.println("=======" + note.getTitle() + "," + note.getContent());
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.event_item, null);
viewHolder = new ViewHolder();
homepage_title = (TextView) convertView.findViewById(R.id.homepage_title);
homepage_content = (TextView) convertView.findViewById(R.id.homepage_content);
homepage_time = (TextView) convertView.findViewById(R.id.homepage_time);
homepage_delete = (TextView) convertView.findViewById(R.id.homepage_delete);
homepage_edt = (TextView) convertView.findViewById(R.id.homepage_edt);
eventitem = (LinearLayout) convertView.findViewById(R.id.homepage_item);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
homepage_title.setText(note.getTitle());
homepage_content.setText(note.getContent());
homepage_time.setText(note.getTime());
// 删除按钮添加监听
homepage_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.deleteNote(note.getId());
ToastUtil.toastShow(mContext, "成功删除事件:" + note.getTitle());
eventAdapter();
}
});
// 编辑按钮添加监听
homepage_edt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, EditActivity.class);
intent.putExtra("note", note);
startActivity(intent);
}
});
// 给一个事件对象添加监听
eventitem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, LookoverActivity.class);
intent.putExtra("note", note);
startActivity(intent);
}
});
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return noteList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return noteList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
}
适配的xml文件item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/homepage_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/homepage_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="事件标题"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/homepage_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="2dp"
android:text="删除" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="/" />
<TextView
android:id="@+id/homepage_edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="2dp"
android:text="编辑" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/homepage_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="我们是好朋友我们是好朋友我们是好朋友我们是好朋友我们是好朋友我们是好朋友" />
<TextView
android:id="@+id/homepage_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间" />
</LinearLayout>
</LinearLayout>
activity页面的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#919191"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="事件列表"
android:textSize="18sp" />
<TextView
android:id="@+id/homepage_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#E9E939"
android:clickable="true"
android:padding="8dp"
android:text="增加"
android:textSize="14sp" />
</RelativeLayout>
<ListView
android:id="@+id/eventList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" >
</ListView>
<TextView
android:id="@+id/homepage_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:background="#E9E939"
android:clickable="true"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="8dp"
android:text="搜索"
android:textSize="14sp" />
</LinearLayout>