package paddy.contactSystem;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
public class QueryActivity extends ExpandableListActivity
{
private DatabaseHelper staticDBHelper = null;
private int staticGroupInt = 0;
private Button queryButton = null;
private EditText queryEdit = null;
private static String staticInputName = null;
//判断是不是第一次启动此方法
private static boolean firstStartFlag = true;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.query);
queryButton = (Button)findViewById(R.id.queryButtonId);
queryEdit = (EditText)findViewById(R.id.queryEditId);
//是第一次启动就执行里面的方法
if(firstStartFlag)
{
//从数据库里读取数据,然后显示
firstDisplayInfo();
queryButton.setOnClickListener(new QueryButtonListener());
firstStartFlag = false;
}
}
@Override
protected void onResume()
{
super.onResume();
if(!firstStartFlag)
{
firstDisplayInfo();
queryButton.setOnClickListener(new QueryButtonListener());
}
}
public void firstDisplayInfo()
{
//生成一个DatabaseHelper对象,用于对数据库进行操作,参数一:当前这个activity,参数二:数据库的名字
DatabaseHelper dbHelper = new DatabaseHelper(this, "paddy_database");
//将这个DatabaseHelper这个对象的引用赋值给一个全局变量,方便此类的其它方法调用
this.staticDBHelper = dbHelper;
//得到一个Cursor,里面包含了个人信息的名字,它作为唯一的一个参数,用它来去寻找它的其它信息,如phoneNum,qqNum
Cursor groupCursor = dbHelper.getParentCursor();
//生成一个用于布局的一个类对象
ExpandableListAdapter msctAdapter = new MySimpleCursorTreeAdapter(
this,
groupCursor,
R.layout.group,
new String[]{"name"},
new int[]{R.id.groupTo},
R.layout.child,
new String[]{"name"},
new int[]{R.id.childTo});
setListAdapter(msctAdapter);
}
//此方法是用于通过查询是调用 的,参数是用户输入的要查询的名字
public void queryDisplayInfo(String inputName)
{
DatabaseHelper dbHelper = new DatabaseHelper(this, "paddy_database", inputName);
this.staticDBHelper = dbHelper;
Cursor groupCursor = dbHelper.getParentCursor();
ExpandableListAdapter msctAdapter = new MySimpleCursorTreeAdapter(
this,
groupCursor,
R.layout.group,
new String[]{"name"},
new int[]{R.id.groupTo},
R.layout.child,
new String[]{"name"},
new int[]{R.id.childTo});
setListAdapter(msctAdapter);
}
class QueryButtonListener implements OnClickListener
{
@Override
public void onClick(View v)
{
String inputNameStr = queryEdit.getText().toString();
staticInputName = inputNameStr;
queryDisplayInfo(inputNameStr);
}
}
//此方法是用户点击子菜单是调用的,参数三:表示的是第几的个人的,参数四:那个人的哪一个信息
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
{
//得到数据库里所以人的电话号码,存放在一个ArrayList里面
ArrayList<String> phoneNumList = getPhoneNumList();
//得到数据里面所以人的姓名
ArrayList<String> realNameList = getNameList();
Intent pSmsIntent = new Intent();
pSmsIntent.setClass(this, ProcessDataActivity.class);
// pSmsIntent.setClass(this, PhoneAndSms.class);
//把电话和姓名传过去,电话用于打电话,姓名用于删除和编辑个人信息是的一个参照对象,即要通过姓名去数据库查找他的信息,然后进行编辑
pSmsIntent.putExtra("phoneNum", phoneNumList.get(groupPosition));
pSmsIntent.putExtra("realName", realNameList.get(groupPosition));
//此方法用于调用另外一个Activity,即打电话,发短信那个界面
startActivity(pSmsIntent);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
//此方法用于得到姓名,存放在一个ArrayList中
public ArrayList<String> getNameList()
{
Cursor realNameCursor = staticDBHelper.getParentCursor();
ArrayList<String> realNameList = new ArrayList<String>();
while(realNameCursor.moveToNext())
{
String realNameStr = realNameCursor.getString(realNameCursor.getColumnIndex("name"));
realNameList.add(realNameStr);
}
return realNameList;
}
//此方法用于得到电话号码,存放在一个ArrayList中
public ArrayList<String> getPhoneNumList()
{
Cursor phoneNumCursor = staticDBHelper.getPhoneNumCursor();
ArrayList<String> phoneNumList = new ArrayList<String>();
while(phoneNumCursor.moveToNext())
{
String phoneNumStr = phoneNumCursor.getString(phoneNumCursor.getColumnIndex("name"));
phoneNumList.add(phoneNumStr);
}
return phoneNumList;
}
//继承了抽象类SimpleCursorTreeAdapter,这种布局方式就是要靠这个类
class MySimpleCursorTreeAdapter extends SimpleCursorTreeAdapter
{
public MySimpleCursorTreeAdapter(Context context,
Cursor cursor,
int groupLayout,
String[] groupFrom,
int[] groupTo,
int childLayout,
String[] childFrom,
int[] childTo)
{
super(context,
cursor,
groupLayout,
groupFrom,
groupTo,
childLayout,
childFrom,
childTo);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
Cursor childCursor = (Cursor) getChild(groupPosition, childPosition);
return childCursor.getLong(1);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor)
{
String uid = groupCursor.getString(groupCursor.getColumnIndex("name"));
return staticDBHelper.getChildCursor(uid);
}
@Override
public long getGroupId(int groupPosition)
{
Cursor groupCursor = getGroup(groupPosition);
return groupCursor.getLong(staticGroupInt);
}
}
}
- 1
- 2
- 3
前往页