package com.b509.activity.patient;
import java.util.ArrayList;
import java.util.List;
import com.b509.tools.CornerListView;
import com.b509.R;
import com.b509.activity.patient.mydoctors.service.OtherPersonService;
import com.b509.activity.patient.mydoctors.service.Person;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class IllnessNoteActivity extends Activity {
CornerListView note;
List persons;
List illness_list;
List illness_time;
List illness_id;
private OnItemClickListener itemClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.illnessnote);
illness_list = new ArrayList();
illness_time = new ArrayList();
illness_id = new ArrayList();
note = (CornerListView) findViewById(R.id.lv_note);
persons = updatalist();
System.out.println(persons.size() + "??????????????????????");
for (int i = 0; i < persons.size(); i++) {
String time = persons.get(i).getTime();
System.out.println(time);
String illness = persons.get(i).getIllness();
System.out.println(illness);
Integer id = persons.get(i).getId();
System.out.println(id);
illness_list.add(illness);
illness_time.add(time);
illness_id.add(id);
}
initListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "新建记录");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bubdle = new Bundle();
bubdle.putInt("flag", 1);
intent.putExtras(bubdle);
intent.setClass(IllnessNoteActivity.this, EditNoteActivity.class);
startActivity(intent);
return super.onOptionsItemSelected(item);
}
private void initListView() {
itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
System.out.println("-----------------------");
final int a = arg2;
AlertDialog.Builder builder = new AlertDialog.Builder(
IllnessNoteActivity.this);
builder.setTitle("选项");
final String[] messages = { "查看", "删除", };
builder.setItems(messages,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
switch (which) {
case 0:
Intent intent = new Intent();
int item_all = illness_list.size();
Bundle bubdle = new Bundle();
bubdle.putInt("all_nub", item_all);
bubdle.putInt("item_", a);
bubdle.putInt("flag", 2);
intent.putExtras(bubdle);
intent.setClass(IllnessNoteActivity.this,
EditNoteActivity.class);
startActivity(intent);
break;
case 1:
int item_all2 = illness_list.size();
int id = item_all2 - a;
OtherPersonService personService = new OtherPersonService(
IllnessNoteActivity.this);
personService.delete(id);
illness_list.remove(a);
illness_time.remove(a);
// 重新加载listView
BaseAdapter ba = new BaseAdapter() {
public int getCount() {
return illness_list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position,
View convertView,
ViewGroup parent) {
LinearLayout ll = new LinearLayout(
IllnessNoteActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(5, 5, 5, 5);
TextView tv = new TextView(
IllnessNoteActivity.this);
tv.setText(illness_list
.get(position));
tv.setTextSize(24);
tv.setPadding(5, 5, 5, 5);
tv.setGravity(Gravity.LEFT);
ll.addView(tv);
TextView tv2 = new TextView(
IllnessNoteActivity.this);
// tv.setText(getResources().getText(data[position]));
tv2.setText(illness_time
.get(position));
tv2.setTextSize(24);
tv2.setPadding(5, 5, 5, 5);
tv2.setGravity(Gravity.RIGHT);
ll.addView(tv2);
return ll;
}
};
note.removeAllViewsInLayout();
ba.notifyDataSetChanged();
note.setAdapter(ba);
note.setOnItemClickListener(itemClickListener);
break;
}
}
});
builder.show();
}
};
BaseAdapter ba = new BaseAdapter() {
public int getCount() {
return illness_list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(IllnessNoteActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(5, 5, 5, 5);
TextView tv = new TextView(IllnessNoteActivity.this);
tv.setText(illness_list.get(position));
tv.setTextSize(24);
tv.setPadding(5, 5, 5, 5);
tv.setGravity(Gravity.LEFT);
ll.addView(tv);
TextView tv2 = new TextView(IllnessNoteActivity.this);
tv2.setText(illness_time.get(position));
tv2.setTextSize(24);
tv2.setPadding(5, 5, 5, 5);
tv2.setGravity(Gravity.RIGHT);
ll.addView(tv2);
return ll;
}
};
note.setAdapter(ba);
note.setOnItemClickListener(itemClickListener);
}
public List updatalist() {
OtherPersonService personService = new OtherPersonService(
IllnessNoteActivity.this);
List lists = personService.getScrollData(0, 1);
System.out.println(lists + "??????????????????????????");
return lists;
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return super.onKeyDown(keycode, event);
}
}
person 是操作sqllite类 令一篇文章会贴代码
通过点击listview相应sqllite中的相应数据,关键是怎么得到相应的id 通过点击事件获得sqllite的id 这一步非常重要!
因为点击onclick中有一个args2参数 这个参数是从零开始依次向下排 因为大多数都是最新的在第一个,以此类推...
可以得到arg2和id 的转化关系 arg2+id=数据总数 ,把arg2和数据总数传到另一个activity 就可以计算出id 就可以取出相应的
数据!这一步非常重要!