今天,是一个值得纪念的日子,是我小菜菜,手写的第一份博客,也不知道写点什么,那么,就写一个前段日子在公司新改的需求吧,其实挺简单的,我们的需求的呢,是这样的,搜索的内容呢 只需要在下面展示5个就可以,然后历史记录下面还要有一个搜索推荐,数据保存到本地 那么 我使用的是数据库,so,那么 我来看看 我是如何实现的,当然,方法大家可能都见过,只是一时之间想不起来而已了,好吧 ,说的太多了,我们来看代码
先看:
布局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:background="#ffffff"
android:orientation="vertical"
tools:context=".activity.game.SearchGameActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@color/wankr_stand_title"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="29dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/bg_white">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="29dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@null"
android:imeOptions="actionSearch"
android:singleLine="true"
android:textColor="#333333"
android:textCursorDrawable="@null"
android:textSize="13sp" />
<ImageView
android:id="@+id/search_iv_cancel"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:src="@drawable/search_cancle" />
RelativeLayout>
<TextView
android:id="@+id/search_tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="10dp"
android:layout_weight="0"
android:text="取消"
android:textColor="#ffffff"
android:textSize="12sp" />
LinearLayout>
<ListView
android:id="@+id/search_result_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:listSelector="#00000000"
android:scrollbars="none">
ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/game_disable"
android:visibility="gone">
<ImageView
android:id="@+id/iv_nodata"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:background="@color/game_disable"
android:src="@drawable/v1_2_img_data_empty"
android:visibility="gone" />
<ImageView
android:id="@+id/img_txt_no_data"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_nodata"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@drawable/v1_2_txt_data_empty"
android:visibility="gone" />
RelativeLayout>
LinearLayout>
headview的布局,历史记录和搜索推荐是一起的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/his_lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/histotry_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="13dp"
android:layout_weight="9"
android:text="历史记录"
android:textColor="#999999"
android:textSize="15sp" />
<ImageView
android:id="@+id/pic_deleate"
android:layout_width="40dp"
android:layout_height="39dp"
android:padding="12dp"
android:src="@drawable/delate" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="21dp"
android:orientation="horizontal">
<TextView
android:id="@+id/his_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_weight="1"
android:background="@drawable/histotry_bt"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="iphone6"
android:textSize="13sp"
android:maxLines="2"
android:visibility="invisible" />
<TextView
android:id="@+id/his_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:background="@drawable/histotry_bt"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="iphone6"
android:textSize="13sp"
android:layout_weight="1"
android:maxLines="2"
android:visibility="invisible" />
<TextView
android:id="@+id/his_three"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_weight="1"
android:background="@drawable/histotry_bt"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="iphone6"
android:textSize="13sp"
android:maxLines="2"
android:visibility="invisible" />
<TextView
android:id="@+id/his_four"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_weight="1"
android:background="@drawable/histotry_bt"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="iPhone 7"
android:textSize="13sp"
android:maxLines="2"
android:visibility="invisible" />
<TextView
android:id="@+id/his_five"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="1"
android:background="@drawable/histotry_bt"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="iphone6s"
android:textSize="13sp"
android:maxLines="2"
android:visibility="invisible" />
LinearLayout>
LinearLayout>
<TextView
android:id="@+id/tv_re"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="搜索推荐"
android:textColor="#999999"
android:textSize="13sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tv_re"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#dbdbdb" />
LinearLayout>
当然 到这里了 我们还需要把adapter的item布局也码出来,这样就省事很多了,布局也算都码完了,你也可以写adapter的时候在写这个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_iv_show_head"
android:layout_width="55dp"
android:layout_height="55dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp">
<TextView
android:id="@+id/item_game_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="tofa战争"
android:textColor="#363636"
android:textSize="15sp" />
<TextView
android:id="@+id/item_game_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_game_start"
android:layout_alignLeft="@+id/item_game_name"
android:layout_marginTop="2dp"
android:includeFontPadding="false"
android:text="210M"
android:textColor="#666666"
android:textSize="13sp" />
<TextView
android:id="@+id/item_game_start"
style="@style/red_button"
android:layout_width="112dp"
android:layout_height="33dp"
android:layout_alignParentRight="true"
android:includeFontPadding="false"
android:text="开始游戏" />
<TextView
android:id="@+id/item_game_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/item_game_start"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="3"
android:text="asdfasdfasdfasdfasdfasdfasdfasdasdfasdfasdfadsfasdfasdfasdfasfdasdfasdfasdfasdfasdfsdfasfdasdfasfd"
android:textColor="#777777"
android:textSize="13sp" />
<LinearLayout
android:id="@+id/item_game_show_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/item_game_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_game_iv_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/bg_failed_square_128" />
<ImageView
android:id="@+id/item_game_iv_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/bg_failed_square_128" />
LinearLayout>
<LinearLayout
android:id="@+id/item_game_active"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/item_game_show_pic"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
LinearLayout>
RelativeLayout>
LinearLayout>
LinearLayout>
好了,开胃小菜都上完了,我们来看看主菜吧~~
定义一个SearchGameActivity类,至于继承谁这就看你们自己公司有没有基类了,这里的操作是实现搜索,获取搜索游戏推荐的数据,更新UI界面 显示历史数据等,来详细看代码
SearchGameActivity:
public class SearchGameActivity extends BaseActivity implements View.OnClickListener {
private EditText ev_serarch;
private ListView lv;
private TextView tv_cancel;
private ImageView iv_cancel;
private List rGameMode;
private ImageView iv_no_data;
private ImageView iv_no_title;
private List searchRecordsList;
private List temp;
private RecordsDao recordsDao;
private LinearLayout histext;
private TextView his_tv, tv_record1, tv_record2, tv_record3, tv_record4, tv_record5;
private ImageView delete_iv;
@Override
public void initView() {
//这是headview的布局
View view =LayoutInflater.from(SearchGameActivity.this).inflate(R.layout.head_view_recommed, null);
histext = ((LinearLayout) view.findViewById(R.id.his_lin));
his_tv = (TextView) view.findViewById(R.id.histotry_search);
tv_record1 = (TextView) view.findViewById(R.id.his_one);
tv_record2 = (TextView) view.findViewById(R.id.his_two);
tv_record3 = (TextView) view.findViewById(R.id.his_three);
tv_record4 = (TextView) view.findViewById(R.id.his_four);
tv_record5 = (TextView) view.findViewById(R.id.his_five);
delete_iv = ((ImageView) view.findViewById(R.id.pic_deleate));
ev_serarch = ((EditText) findViewById(R.id.search_edit_text));
lv = ((ListView) findViewById(R.id.search_result_game));
tv_cancel = ((TextView) findViewById(R.id.search_tv_cancel));
iv_cancel = ((ImageView) findViewById(R.id.search_iv_cancel));
iv_no_data = ((ImageView) findViewById(R.id.iv_nodata));
iv_no_title = ((ImageView) findViewById(R.id.img_txt_no_data));
lv.addHeaderView(view);
}
@Override
public void setListener() {
tv_cancel.setOnClickListener(this);
iv_cancel.setOnClickListener(this);
final EditText.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//先隐藏键盘
if (ev_serarch.getText().toString().length() > 0) {
String record = ev_serarch.getText().toString();
//判断数据库中是否存在该记录
if (!recordsDao.isHasRecord(record)) {
temp.add(record);
recordsDao.addRecords(record);
}
//讲搜索记录保存在数据库中
reversedList();
checkRecordsSize();
//點擊之後跳轉搜索結果的頁面
showResultSerach(ev_serarch.getText().toString());
updateUI();//回到历史记录的UI界面
} else {
showToast("搜索内容不能为空");
}
}
return false;
}
};
ev_serarch.setOnEditorActionListener(editorActionListener);
ev_serarch.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) {
}
});
tv_record1.setOnClickListener(this);
his_tv.setOnClickListener(this);
tv_record2.setOnClickListener(this);
tv_record3.setOnClickListener(this);
tv_record4.setOnClickListener(this);
tv_record5.setOnClickListener(this);
delete_iv.setOnClickListener(this);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Log.e("onItemClick", "position:" + position + "id:" + id);
if (id >= 0) {
showResultSerach(rGameMode.get(position - 1).getgName());
}
}
});
}
//当没有匹配的搜索数据的时候不显示历史记录
private void checkRecordsSize() {
if (searchRecordsList.size() == 0) {
histext.setVisibility(View.GONE);
} else {
histext.setVisibility(View.VISIBLE);
}
// tv_record1.setText(searchRecordsList.get(0));
}
//搜索游戏的结果的方法
public void showResultSerach(String searchStr) {
Intent intent = new Intent(this, SearchGameResultActivity.class);
intent.putExtra("searchStr", searchStr);
startActivity(intent);
}
@Override
public void initData() {
//获取游戏推荐
getGameRecommed();
recordsDao = new RecordsDao(this);
searchRecordsList = new ArrayList<>();
temp = new ArrayList<>();
temp.addAll(recordsDao.getRecordList());
reversedList();
//第一次进入判断数据库中是否有历史记录,没有则不显示
checkRecordsSize();
updateUI();
}
//更新UI界面 显示历史数据
private void updateUI() {
tv_record1.setVisibility(View.INVISIBLE);
tv_record2.setVisibility(View.INVISIBLE);
tv_record3.setVisibility(View.INVISIBLE);
tv_record4.setVisibility(View.INVISIBLE);
tv_record5.setVisibility(View.INVISIBLE);
try {
tv_record1.setText(searchRecordsList.get(0));
tv_record1.setVisibility(View.VISIBLE);
tv_record2.setText(searchRecordsList.get(1));
tv_record2.setVisibility(View.VISIBLE);
tv_record3.setText(searchRecordsList.get(2));
tv_record3.setVisibility(View.VISIBLE);
tv_record4.setText(searchRecordsList.get(3));
tv_record4.setVisibility(View.VISIBLE);
tv_record5.setText(searchRecordsList.get(4));
tv_record5.setVisibility(View.VISIBLE);
} catch (Exception e) {
}
}
@Override
public int getLayout() {
return R.layout.activity_search_game;
}
@Override
public String setTitle() {
return null;
}
//获取搜索游戏推荐的数据
public void getGameRecommed() {
getByLiangLiangBase(Constant.GET_SEARCH_RECOMMED_GAME, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String searchResult = new String(responseBody);
Log.e("searchResult------>", searchResult);
SearchGameRecommed gameRecommed = new Gson().fromJson(searchResult, new TypeToken() {
}.getType());
if (gameRecommed.getCode() == 1) {
//获取推荐的游戏
rGameMode = gameRecommed.getrGame();
if (rGameMode.size() > 0) {
ev_serarch.setHint(rGameMode.get(0).getgName());
//适配器
SearchGameRecommedAdapter adapter = new SearchGameRecommedAdapter(SearchGameActivity.this, spUtil, rGameMode);
lv.setAdapter(adapter);
} else {
lv.setVisibility(View.GONE);
}
} else {
// showToast("请检查网络");
lv.setVisibility(View.GONE);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// showToast("请检查网络");
iv_no_data.setVisibility(View.VISIBLE);
iv_no_title.setVisibility(View.VISIBLE);
lv.setVisibility(View.GONE);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.search_tv_cancel:
this.finish();
break;
case R.id.search_iv_cancel:
ev_serarch.setHint("");
ev_serarch.setText("");
break;
//清楚历史记录
case R.id.pic_deleate:
//删除数据的方法
temp.clear();
reversedList();
recordsDao.deleteAllRecords();
histext.setVisibility(View.GONE);
break;
case R.id.his_one:
//點擊之後跳轉搜索結果的頁面
showResultSerach(tv_record1.getText().toString());
updateUI();
break;
case R.id.his_two:
//點擊之後跳轉搜索結果的頁面
showResultSerach(tv_record2.getText().toString());
updateUI();
break;
case R.id.his_three:
//點擊之後跳轉搜索結果的頁面
showResultSerach(tv_record3.getText().toString());
updateUI();
break;
case R.id.his_four:
//點擊之後跳轉搜索結果的頁面
showResultSerach(tv_record4.getText().toString());
updateUI();
break;
case R.id.his_five:
//點擊之後跳轉搜索結果的頁面
showResultSerach(tv_record5.getText().toString());
updateUI();
break;
}
}
//颠倒list顺序,用户输入的信息会从上依次往下显示
private void reversedList() {
searchRecordsList.clear();
for (int i = temp.size() - 1; i >= 0; i--) {
searchRecordsList.add(temp.get(i));
}
}
现在在写一个 RecordSQLiteOpenHelper
这里面是搜索帮助类
public class RecordSQLiteOpenHelper extends SQLiteOpenHelper {
private final static String DB_NAME = "temp.db";
private final static int DB_VERSION = 1;
public RecordSQLiteOpenHelper(Context context) {
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlStr = "CREATE TABLE IF NOT EXISTS records (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);";
db.execSQL(sqlStr);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
数据库操作类 里面有增删改查的方法,模糊查询
public class RecordsDao {
RecordSQLiteOpenHelper recordSQLiteOpenHelper;
SQLiteDatabase recordsDb;
public RecordsDao(Context context) {
recordSQLiteOpenHelper = new RecordSQLiteOpenHelper(context);
}
//添加搜索记录
public void addRecords(String record) {
if (!isHasRecord(record)) {
recordsDb = recordSQLiteOpenHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", record);
//添加
recordsDb.insert("records", null, values);
//关闭
recordsDb.close();
}
}
//判断是否含有该搜索记录
public boolean isHasRecord(String record) {
boolean isHasRecord = false;
recordsDb = recordSQLiteOpenHelper.getReadableDatabase();
Cursor cursor = recordsDb.query("records", null, null, null, null, null, null);
while (cursor.moveToNext()) {
if (record.equals(cursor.getString(cursor.getColumnIndexOrThrow("name")))) {
isHasRecord = true;
}
}
//关闭数据库
recordsDb.close();
return isHasRecord;
}
//获取全部搜索记录
public List getRecordList() {
List recordList = new ArrayList<>();
recordsDb = recordSQLiteOpenHelper.getReadableDatabase();
Cursor cursor = recordsDb.query("records", null, null, null, null, null, null);
while (cursor.moveToNext()) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
recordList.add(name);
}
//关闭数据库
recordsDb.close();
}
return recordList;
}
//模糊查询
public List querySimlarRecords(String record){
String queryStr = "select * from records where name like '%\" + record + \"%' order by name ";
List simlarRecords = new ArrayList<>();
Cursor cursor = recordSQLiteOpenHelper.getReadableDatabase().rawQuery(queryStr,null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
simlarRecords.add(name);
}
return simlarRecords;
}
//qing清空搜索记录
public void deleteAllRecords(){
recordsDb = recordSQLiteOpenHelper.getWritableDatabase();
recordsDb.execSQL("delete from records");
recordsDb.close();
}
}
搜索结果页面SearchGameResultActivity
/**
* 搜索结果页面
* 这个类里面 有很多你们用不到的方法 例如下载 安装 自行整理
*
*/
public class SearchGameResultActivity extends BaseActivity {
private PullToRefreshListView plv;
private String searchStr;
private SearchGameResultAdapter adapter;
private List gameDatas;
private DownLoadMainStateReceiver receiver;
private int page = 1;
@Override
public void initView() {
plv = ((PullToRefreshListView) findViewById(R.id.plv_game_result));
plv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//从下面开始刷新
}
@Override
public void setListener() {
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
//下拉刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
page = 1;
getSearchGameResult();
}
//上啦加载
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
page++;
getSearchGameResult();
}
});
}
@Override
public void initData() {
gameDatas = new ArrayList<>();
//注册广播
initReceiver();
//获取搜索游戏的数据
initIntent();
getSearchGameResult();
}
/**
* 注册广播
*/
public void initReceiver() {
receiver = new DownLoadMainStateReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("downloadstate");
registerReceiver(receiver, intentFilter);
}
@Override
public int getLayout() {
return R.layout.activity_search_game_result;
}
@Override
public String setTitle() {
return null;
}
public void initIntent() {
Intent intent = getIntent();
searchStr = intent.getStringExtra("searchStr");
title = ((TextView) findViewById(R.id.title));
title.setText(searchStr);
// if (searchStr.length() >= 12) {
// String sub = searchStr.substring(0, 10);
// title.setText(sub + "...");
// } else
// title.setText(searchStr);
}
public void getSearchGameResult() {
getByLiangLiangBase(Constant.SEARCH_GAME_ALL + searchStr + "?agent=" + GameApplication.getChannel(mContext), null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
hideLoading(plv);
plv.onRefreshComplete();
String str = new String(responseBody);
GameSearchResult gameSearchResult = new Gson().fromJson(str, new TypeToken() {
}.getType());
gameDatas = gameSearchResult.getGames();
Log.e("str", str);
adapter = new SearchGameResultAdapter(SearchGameResultActivity.this, spUtil, new OnGameItemClickListenser() {
@Override
public void onGameItemClick(HomeResultBean.MainGame game) {
//游戏详情页点击
postDetail(game, false);
}
@Override
public void onDownLoadClick(int position) {
//下载点击 这个和搜索和历史记录就没有关系了
postDetail(gameDatas.get(adapter.getTruePosition(position)), true);
}
}, null, null);
plv.setAdapter(adapter);
adapter.setDatas(gameDatas);
if (gameSearchResult.getCode() != 1) {
showNoDataView(plv, 4);
showToast("该渠道没有游戏");
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//showNoNetToast();
hideLoading(plv);
showNoDataView(plv, 0);
}
});
}
class DownLoadMainStateReceiver extends BroadcastReceiver {
private int position;
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra("state", 0);
String packageName = intent.getStringExtra("packagename");
position = getPositionByPackageName(packageName);
logE("主页", "position:" + position + "state:" + state);
if (position != -1)
if (state == 0) {
// 系统发送的安装成功广播
gameDatas.get(position).setState(
DownFileUtil.INSTALL_SUCCESS);
} else {
// service发送的下载状态广播
if (state != DownFileUtil.NETDOWNDOING_MESGWHAT) {
gameDatas.get(position).setState(state);
}
}
adapter.setDatas(gameDatas);
}
}
private int getPositionByPackageName(String packageName) {
for (int i = 0; i < gameDatas.size(); i++) {
HomeResultBean.MainGame game = gameDatas.get(i);
if (packageName.equals(game.getPackage_name())) {
return i;
}
}
return -1;
}
@Override
public void onDestroy() {
super.onDestroy();
unReReceiver();
}
/**
* 注销广播
*/
public void unReReceiver() {
if (receiver != null) {
unregisterReceiver(receiver);
}
}
//获取游戏详情
private void postDetail(final HomeResultBean.MainGame bean, final boolean isDownLoad) {
showLoading();
String url = Constant.GAME_DETAIL + bean.getGame_id();
//这是我自己封装的get方法,你们可以用client.
getByLiangLiangBase(url, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
String jsonString = new String(responseBody);
Log.e("onSuccess", jsonString);
SpecialGame sGame = new Gson().fromJson(jsonString,
new TypeToken() {
}.getType());
if (sGame.getCode() == 1) {
Intent intent = new Intent();
if (sGame.getType() == 1)
intent.setClass(mContext, GameSpecialInfoActivity.class);
else
intent.setClass(mContext, GameNormalInfoActivity.class);
intent.putExtra("bean", bean);
intent.putExtra("detailbean", sGame);
intent.putExtra("isDownload", isDownLoad);
startActivity(intent);
} else {
showToast(sGame.getMsg());
}
hideLoading();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
hideLoading();
}
});
}
@Override
public void onNoDataClick() {
//重新获取数据
getSearchGameResult();
}
}
鉴于adapter很简单 而我得adapter操作很多和搜索都没有关系 就不往上贴了 自己写吧,好吧 就写到这里,希望对你们有点作用,好评好评