Android Development Notes - achieved through SharePreferences Adapter pass data to the Activity

When I realized imitation Jingdong commodity classification function, you need to click on the right product subcategories (RightAdapter) jump to the product search list (SortSearchActivity), in order to achieve this, you need to solve two problems:

1. Goods subclass disposed in listening RightAdapter, the interface implemented by the Intent jump;

2. After the jump the value of the clicked item (here "oppo") passed to the appropriate Activity (here SortSearchActivity), and implement search listings displayed.

Specific implementation results as shown below (click oppo):
Here Insert Picture Description
Here Insert Picture Description

1. How to solve the interface problem jump, achieved through Context:

Intent intent = new Intent(mContext, SortSearchActivity.class);
mContext.startActivity(intent);

2. How to solve the issue by value, is achieved by SharePreferences:

RightAdpter (here kindItemBean.getName () data transfer is required):

SharedPreferences sp = mContext.getSharedPreferences("data", MODE_PRIVATE);
//获取编辑器
SharedPreferences.Editor editor = sp.edit(); 
//存入String型数据
editor.putString("goodsname", kindItemBean.getName()); 
Log.i(TAG, "goodsname:" + sp);
//提交修改,否则不生效
editor.commit();     

SortSearchActivity:

//获取分类界面点击的物品,展示出搜索结果
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
//第二个参数为缺省值,如果不存在该key,返回缺省值
String goodsname = sp.getString("goodsname", "");
Log.i(TAG, "goodsname:" + goodsname);
etSearch.setHint(goodsname); //etSearch为搜索框
goodsKeyWord = goodsname;    //goodsKeyWord为搜索关键字
Published 20 original articles · won praise 26 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_38233258/article/details/105194806