//主页面
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecy;
private ListmList = new ArrayList<>();
private ListBeanDao mListBeanDao;
private String mUrl = "http://v.juhe.cn/weixin/query?key=a332c6b34264527ac142764eaed9364d&pno=2";
private MyAdapter mAdapter;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsonStr = (String) msg.obj;
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonStr, MyData.class);
mList = myData.getResult().getList();
mAdapter = new MyAdapter(mList);
mRecy.setAdapter(mAdapter);
mAdapter.setOnItem(new MyAdapter.OnItemClick() {
@Override
public void setOnItemClick(View v, int position) {
Toast.makeText(MainActivity.this, mList.get(position).getSource(), Toast.LENGTH_SHORT).show();
}
});
mAdapter.setOnLongItem(new MyAdapter.OnLongItemClick() {
@Override
public void setOnLongItemClick(View v, int position) {
ListBean bean = mList.get(position);
long insert = mListBeanDao.insert(bean);
if (insert > 0) {
Intent intent = new Intent(MainActivity.this, QueryActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "插入失败", Toast.LENGTH_SHORT).show();
}
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListBeanDao = MyApp.getMyApp().getmDaoSession().getListBeanDao();
initView();
getInfo();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.Recy);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecy.setLayoutManager(linearLayoutManager);
}
private void getInfo() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(mUrl).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message msg = new Message();
msg.obj = response.body().string();
mHandler.sendMessage(msg);
}
});
}