一 Activity ---->> 用于选择的主界面
package com.zte.positionenglish.activity;
import java.util.ArrayList;
import java.util.List;
import roboguice.inject.InjectView;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.zte.android.app.activity.BaseActivity;
import cn.com.zte.android.widget.dialog.DialogManager;
import com.zte.positionenglish.R;
import com.zte.positionenglish.adapter.ExamSecondTitleAdapter;
import com.zte.positionenglish.adapter.ExamSecondTitleAdapter.SelectedPositionExamCallback;
import com.zte.positionenglish.adapter.FirstTitleAdapter;
import com.zte.positionenglish.biz.ApplyExamBiz;
import com.zte.positionenglish.db.ExamDBHelper;
import com.zte.positionenglish.db.ExamDatabaseManager;
import com.zte.positionenglish.entity.ExamPositionEntity;
import com.zte.positionenglish.entity.SecondTitlePositionInfoEntity;
import com.zte.positionenglish.model.Contanst;
import com.zte.positionenglish.util.DataApplication;
/***
* 选择具体【岗位/科目】报名考试 页面
*
* @author wtb
*
*/
public class ExamChooseActivity extends BaseActivity implements OnItemClickListener{
private static final String TAG = ExamChooseActivity.class.getSimpleName();
public static final String exam_position_selected = "exam_position_selected";
public static final String ExamPositionACTION = "cn.com.zte.action.applyexam.broadcast";
private Context mContext = ExamChooseActivity.this;
private String EID = ""; //考试岗位信息对象id
private String EPID = "";//考试岗位对象id
private ApplyExamBroadcastReciver applyExamBroadcastReciver;
@InjectView(R.id.choose_position_probar)
private ProgressBar choose_position_probar;
/**数据库处理相关*/
private ExamDatabaseManager examManager, examManagerGrid;
private ExamDBHelper examDBHelper,examDBHelperGrid;
private SQLiteDatabase database = null;
private SQLiteDatabase databaseGrid = null;
/**返回*/
@InjectView(R.id.iv_myImageView_back)
private ImageView iv_myImageView_back;
/**搜索框*/
@InjectView(R.id.myEditText_Filter)
private EditText myEditText_Filter;
/**要输入的搜索字符*/
private String englishTest;
/**按照条件搜索之后的暂时存放数据的集合*/
private List<ExamPositionEntity> searchListData = new ArrayList<ExamPositionEntity>();
private List<ExamPositionEntity> searchSecondList = new ArrayList<ExamPositionEntity>();
/**清空搜索框*/
@InjectView(R.id.myImageView_delete_search)
private ImageView myImageView_delete_search;
/**左边一级标题listview*/
@InjectView(R.id.lv_first_title)
private ListView lv_first_title;
/**左边一级标题数据集合*/
private List<ExamPositionEntity> listFirstTitleEntity = new ArrayList<ExamPositionEntity>();
/**二级标题数据集合*/
private List<ExamPositionEntity> _listSecondTitleEntity = new ArrayList<ExamPositionEntity>();
/**左边一级标题适配器*/
private FirstTitleAdapter firstAdapter = null;
/**右边二级标题listview*/
@InjectView(R.id.lv_right_listview)
private ListView lv_right_listview;
/**右边二级标题对象*/
private SecondTitlePositionInfoEntity secondEntity;
/**右边二级标题对象集合*/
private List<SecondTitlePositionInfoEntity> listSecondTitleEntity = new ArrayList<SecondTitlePositionInfoEntity>();
/**在二级listview中标题text或gridview的text对象*/
private ExamPositionEntity positionEntityItem;
/**在二级listview中标题text或gridview的text对象集合*/
private List<ExamPositionEntity> listExamPositionEntityItem = new ArrayList<ExamPositionEntity>();
/**右边二级标题适配器*/
private ExamSecondTitleAdapter secondAdapter = null;
/**确定 报名按钮*/
@InjectView(R.id.tv_apply_test)
private TextView tv_apply_test;
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Contanst.GET_SUCCESS:
Log.d(TAG, "---报名成功的信息是["+msg.obj+"]--");
ExamChooseActivity.this.finish();
break;
case Contanst.GET_FAILED:
Toast.makeText(mContext, "考试报名失败~", Toast.LENGTH_SHORT).show();
Log.d(TAG, "---报名失败原因是["+msg.obj+"]--");
choose_position_probar.setVisibility(View.GONE);
break;
}
};
};
@Override
protected void initContentView() {
super.initContentView();
setContentView(R.layout.activity_exam_choose);
}
@Override
protected void initViews() {
// TODO Auto-generated method stub
super.initViews();
// 初始化EditText
myEditText_Filter.setText(null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ExamPositionACTION);
applyExamBroadcastReciver = new ApplyExamBroadcastReciver();
ExamChooseActivity.this.registerReceiver(applyExamBroadcastReciver, intentFilter);
}
@Override
protected void initData() {
super.initData();
// 获取考试信息id
Intent intent = ((Activity) mContext).getIntent();
EID = intent.getBundleExtra("_ApplyExamPositionIDBundle").getString("_ApplyExamPositionInfoID");
Log.d(TAG + "----这个是从考试报名页跳过来的考试岗位ID: [ " +EID+" ]---", "");
secondAdapter = new ExamSecondTitleAdapter(mContext,R.layout.item_second_title_layout ,listSecondTitleEntity);
secondAdapter.setSelectedPositionExamCallback(new SelectedPositionExamCallback() {
@Override
public void SelectedPositionExam(int secondLocation, int thirdLocation) {
for (int i = 0; i < listSecondTitleEntity.size(); i++) {
for (int j = 0; j < listSecondTitleEntity.get(i).getListListData().size(); j++) {
listSecondTitleEntity.get(i).getListListData().get(j).setSelected(false);
}
}
listSecondTitleEntity.get(secondLocation).getListListData().get(thirdLocation).setSelected(true);
secondAdapter.clear();
secondAdapter.addAll(listSecondTitleEntity);
secondAdapter.notifyDataSetChanged();
}
});
lv_right_listview.setAdapter(secondAdapter);
//初始化左边一级二级标题
initTitles();
}
@Override
protected void initViewEvents() {
super.initViewEvents();
// 点击事件
setOnClicks();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(applyExamBroadcastReciver);
}
/**该页面所有点击事件*/
private void setOnClicks() {
/**返回*/
iv_myImageView_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
/** 搜索栏输入有变化监听事件 */
myEditText_Filter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
englishTest = s.toString();
Log.d(TAG, "你输入的搜索字符englishTest为:" + englishTest);
if (englishTest != null&&!englishTest.equals("")) {
String[] positionNameList = new String[]{"%" + englishTest + "%"};
examManager = ExamDatabaseManager.getInstance(mContext);
examDBHelper = new ExamDBHelper(mContext);
database = examDBHelper.getWritableDatabase();
searchListData = examManager.SearchOnConditionFirst(database, positionNameList);
Log.d(TAG, "搜索栏输入搜索条件--有变化监听事件");
if (searchListData!=null&&searchListData.size()>0) {
firstAdapter = new FirstTitleAdapter(mContext, searchListData);
lv_first_title.setAdapter(firstAdapter);
searchPosition(0);
firstAdapter.setSelectedPosition(0);
if (firstAdapter != null) {
firstAdapter.notifyDataSetChanged();
}
}else {
initTitles();
DialogManager.showToast(mContext, "没有该岗位请重新输入", Toast.LENGTH_SHORT);
}
if (englishTest.length() > 0 && myImageView_delete_search.getVisibility() != View.VISIBLE) {
myImageView_delete_search.setVisibility(View.VISIBLE);
}
} else {
initTitles();
if (myImageView_delete_search.getVisibility() != View.GONE) {
myImageView_delete_search.setVisibility(View.GONE);
}
}
}
});
/**清除搜索栏中所有字符*/
myImageView_delete_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myEditText_Filter.setText(null);
myImageView_delete_search.setVisibility(View.GONE);
}
});
lv_first_title.setOnItemClickListener(this);
lv_right_listview.setOnItemClickListener(this);
/*报名确定*/
tv_apply_test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if("".equals(EPID)){
Toast.makeText(mContext, "您还没选择岗位科目", Toast.LENGTH_SHORT).show();
return;
}
if(DataApplication.getInstance().getNetworkStatus() == 1){
// 携带数据请求服务端
new ApplyExamBiz(mContext, EID, EPID, mHandler).startNet();
Log.d(TAG, "---考试报名信息Id是【"+EID+"】->>--考试岗位Id是【"+EPID+"】-->>>---提交服务器--->>");
choose_position_probar.setVisibility(View.VISIBLE);
}else{
Log.d(TAG, "---没有网络 检查网络--->>");
Toast.makeText(mContext, "无网络 报名失败", Toast.LENGTH_SHORT).show();
return;
}
}
});
}
/**初始化左边一级标题*/
private void initTitles() {
//初始化数据[一级标题] 数据来源于数据库
examManager = ExamDatabaseManager.getInstance(mContext);
examDBHelper = new ExamDBHelper(mContext);
database = examDBHelper.getWritableDatabase();
listFirstTitleEntity = examManager.queryFirstTitle(database, "LevelF"); // 检索条件为 LevelF
Log.d(TAG, " --检索取出一级标题--");
if (listFirstTitleEntity.size()>0) {
listFirstTitleEntity.remove(0); // 第一个是有问题的。因此需要过滤调, 但是这个是权宜之计 以后需要在数据库里面修正而非这里
initFirsTitleAdapter(listFirstTitleEntity);
Log.d(TAG, "--字符item打印共:-->" + firstAdapter.getCount() + "个");
Log.d(TAG, "------------- 一级标题分割线 --------------");
for(int i=0; i<listFirstTitleEntity.size(); i++){
Log.d(TAG, "标题【"+listFirstTitleEntity.get(i).getLeveF()+"】");
}
Log.d(TAG, "------------- 一级标题分割线 --------------");
}
}
/**初始化一级标题adapter*/
private void initFirsTitleAdapter(List<ExamPositionEntity> listEntity) {
firstAdapter = new FirstTitleAdapter(mContext, listEntity);
lv_first_title.setAdapter(firstAdapter);
initSecondListView(listEntity.get(0).getLeveF()); // 默认二级标题也是显示第一个
firstAdapter.setSelectedPosition(0);
if (firstAdapter != null) {
firstAdapter.notifyDataSetChanged();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
switch (parent.getId()) {
case R.id.lv_first_title:
Log.d(TAG, "--listFirstTitle选中-->" + listFirstTitleEntity.get(position).getLeveF());
/**循环历遍第一级标题所有元素, 加载二级标题*/
if (englishTest!=null&&!englishTest.equals("")) {
for (int i = 0; i < searchListData.size(); i++) {
if (i==position) {
//如果是搜索情况下
if (englishTest!=null&&!englishTest.equals("")) {
searchPosition(i);
}
}
}
}else {//如果搜索框为空 普通情况下
for(int i=0; i<listFirstTitleEntity.size();i++){
if(i==position){
initSecondListView(listFirstTitleEntity.get(i).getLeveF());
}
}
}
firstAdapter.setSelectedPosition(position); // 一级标题选中的颜色
firstAdapter.notifyDataSetInvalidated();
break;
}
}
/**每一个[一级标题列表元素]就代表一个[二级标题列表]*/
private void initSecondListView( String first){
listSecondTitleEntity=new ArrayList<SecondTitlePositionInfoEntity>();
// FIXME
if (_listSecondTitleEntity.size()>0) {
/**遍历二级标题所有元素, 加载二级标题列表所有元素*/
for(int j=0; j<_listSecondTitleEntity.size(); j++){
//加载列表元素前先清空里面的内容
if(listSecondTitleEntity!=null || listSecondTitleEntity.size()>0){
listSecondTitleEntity.clear();
Log.d(TAG, "--清除二级列表中元素[listSecondTitleEntity]内容-->" + (listSecondTitleEntity.isEmpty()? "已清除" : "未清除"));
}
if(listExamPositionEntityItem!=null || listExamPositionEntityItem.size()>0){
listExamPositionEntityItem.clear();
Log.d(TAG, "--清除二级列表中gridview中的内容下的列表[listExamPositionEntityItem]内容-->" + (listExamPositionEntityItem.isEmpty()? "已清除" : "未清除"));
}
}
}
examManager = ExamDatabaseManager.getInstance(mContext);
examDBHelper = new ExamDBHelper(mContext);
database = examDBHelper.getWritableDatabase();
//获取到了二级标题集合 应该是每一个一级标题就要检索一次二级标题 【实现了】
_listSecondTitleEntity = examManager.querySecondTitle(database, "LevelS", first); // 检索条件为 LevelS enable为Y
/*---------------日志打印区域------------------*/
for(int j=0; j<_listSecondTitleEntity.size(); j++){
Log.i(TAG+"---二级标题取出是【" + _listSecondTitleEntity.get(j).getLeveS() +"】---Activity", ""); // 遍历二级标题
}
/*---------------日志打印区域------------------*/
for(int i=0; i<_listSecondTitleEntity.size(); i++){
positionEntityItem = _listSecondTitleEntity.get(i);
// 获取一个gridview的集合
examManagerGrid = ExamDatabaseManager.getInstance(mContext);
examDBHelperGrid = new ExamDBHelper(mContext);
databaseGrid = examDBHelper.getWritableDatabase();
listExamPositionEntityItem = examManagerGrid.queryPositionEntity(databaseGrid, "Name", _listSecondTitleEntity.get(i).getLeveS()); // 获取gridview集合
/*---------------日志打印区域------------------*/
for(int j=0; j<listExamPositionEntityItem.size(); j++){
Log.i(TAG+"---gridview内容 取出是【" + listExamPositionEntityItem.get(j).getName() +"】---Activity", ""); // 遍历二级标题
}
/*---------------日志打印区域------------------*/
//二级标题及内容text
listSecondTitleEntity.add(new SecondTitlePositionInfoEntity(positionEntityItem, listExamPositionEntityItem));
}
secondAdapter.clear();
secondAdapter.addAll(listSecondTitleEntity);
secondAdapter.notifyDataSetChanged();
}
class ApplyExamBroadcastReciver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ExamPositionACTION)){
EPID = intent.getBundleExtra("MyExamPositionIDBunlde").getString("IChooseExamPositionID");
// Toast.makeText(mContext, "岗位ID:" + MyExamPositionID, Toast.LENGTH_SHORT).show();
Log.i(TAG+"---岗位ID:【" + EPID +"】---", "");
}
}
}
/**
*
* @param pos 一级菜单点选的位置
* */
private void searchPosition(int pos){
listSecondTitleEntity=new ArrayList<SecondTitlePositionInfoEntity>();
examManager = ExamDatabaseManager.getInstance(mContext);
examDBHelper = new ExamDBHelper(mContext);
database = examDBHelper.getWritableDatabase();
_listSecondTitleEntity = examManager.SearchPositionSecond(database, "LevelS", searchListData.get(pos).getLeveF(), "%" + englishTest + "%"); // 检索条件为 LevelS enable为Y
for(int i=0; i<_listSecondTitleEntity.size(); i++){
positionEntityItem = _listSecondTitleEntity.get(i);
// 获取一个gridview的集合
examManagerGrid = ExamDatabaseManager.getInstance(mContext);
examDBHelperGrid = new ExamDBHelper(mContext);
databaseGrid = examDBHelper.getWritableDatabase();
listExamPositionEntityItem = examManagerGrid.SearchPositionEntity(databaseGrid, "Name", _listSecondTitleEntity.get(i).getLeveS(),"%" + englishTest + "%"); // 获取gridview集合
/*---------------日志打印区域------------------*/
for(int j=0; j<listExamPositionEntityItem.size(); j++){
Log.i(TAG+"---gridview内容 取出是【" + listExamPositionEntityItem.get(j).getName() +"】---Activity", ""); // 遍历二级标题
}
/*---------------日志打印区域------------------*/
//二级标题及内容text
listSecondTitleEntity.add(new SecondTitlePositionInfoEntity(positionEntityItem, listExamPositionEntityItem));
}
secondAdapter.clear();
secondAdapter.addAll(listSecondTitleEntity);
secondAdapter.notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<!-- 自定义Topbar start -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#00C2C4"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal"
android:text="岗位选择"
android:textColor="#ffffff"
android:textSize="22sp"
android:typeface="normal" />
<ImageView
android:id="@+id/iv_myImageView_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="17dip"
android:paddingRight="50dp"
android:src="@drawable/info_back" />
</RelativeLayout>
<!-- 自定义Topbar end -->
<!-- 搜索框 start -->
<LinearLayout
android:id="@+id/english_test_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#FFFFFFFF" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/position_filter" >
<EditText
android:id="@+id/myEditText_Filter"
style="@style/positiontextsize16"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/position_search_edittext"
android:drawableLeft="@drawable/search"
android:hint="search"
android:imeOptions="actionSend"
android:maxLength="100"
android:paddingLeft="10dip"
android:singleLine="true"
android:textColor="@color/black" />
<ImageView
android:id="@+id/myImageView_delete_search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="@drawable/login_delete"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#FFFFFFFF" />
</LinearLayout>
<!-- 搜索框 end -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<!-- 岗位英语课程选择 start -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<!-- 左侧一级标题 start -->
<RelativeLayout
android:id="@+id/rel_listview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@+id/tv_apply_test"
android:layout_marginTop="5dip"
android:background="#EEF0F3" >
<ListView
android:id="@+id/lv_first_title"
android:layout_width="70dip"
android:layout_height="fill_parent"
android:divider="@null"
android:scrollbars="@null" />
</RelativeLayout>
<!-- 左侧一级标题 end -->
<!-- 右侧详细选择 start 本想用一个scrollview中再嵌套一个listview -->
<ListView
android:id="@+id/lv_right_listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@+id/tv_apply_test"
android:layout_toRightOf="@+id/rel_listview"
android:background="#FFFFFF"
android:divider="@null"
android:scrollbars="@null" />
<!-- 右侧详细选择 end -->
<!-- 报名按钮 -->
<TextView
android:id="@+id/tv_apply_test"
android:layout_width="match_parent"
android:layout_height="35dip"
android:layout_alignParentBottom="true"
android:background="#00C2C4"
android:gravity="center"
android:text="报名"
android:textColor="#FFFFFF"
android:textSize="17sp" />
</RelativeLayout>
<!-- 岗位英语课程选择 end -->
<ProgressBar
android:id="@+id/choose_position_probar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>