Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #1478 第六次实验报告 #2614

Merged
merged 3 commits into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Com1614080901217 extends AppCompatActivity {
private List<String> listContent;
private List<BianQian> bianQianList;
private SQLiteDatabase writableDatabase;
private BianQian bianQian;
private ArrayAdapter<String> adapter;


@Override
Expand All @@ -27,16 +36,17 @@ protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1614080901217);
//initDB();
//initDB();
initData();
initDataByJson();
ListView listView = (ListView) findViewById(R.id.bianqian_list);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listContent);
adapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listContent);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(Com1614080901217.this, ContentActivity.class);
intent.putExtra("content",bianQianList.get(i).getContent());
intent.putExtra("content", bianQianList.get(i).getContent());
startActivity(intent);

}
Expand All @@ -45,6 +55,36 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}

private void initDataByJson() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("https://raw.githubusercontent.com/MoxinHuoHuo/android-labs-2018/master/com1614080901217/jsonFile.json").build();
try {
Response execute = okHttpClient.newCall(request).execute();
String jsonData = execute.body().string();
Gson gson = new Gson();
List<BianQian> newData = gson.fromJson(jsonData, new TypeToken<List<BianQian>>() {
}.getType());
bianQianList.addAll(newData);
List<String> strings = initTitle(newData);
listContent.addAll(strings);
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});

} catch (IOException e) {
e.printStackTrace();
}

}
}).start();
}


private void initDB() {
DatabaseHelper databaseHelper = new DatabaseHelper(this);
Expand All @@ -61,7 +101,7 @@ private void initDB() {

private void initData() {
bianQianList = new ArrayList<>();
listContent = new ArrayList<>();
listContent = new ArrayList<>();
DatabaseHelper databaseHelper = new DatabaseHelper(this);
writableDatabase = databaseHelper.getWritableDatabase();
Cursor cursor = writableDatabase.query("bianqian", null, null, null, null, null, null);
Expand All @@ -82,4 +122,12 @@ private void initData() {
}
}

List<String> initTitle(List<BianQian> newData
) {
List<String> newTitle = new ArrayList<>();
for (int i = 0; i < newData.size(); i++) {
newTitle.add(newData.get(i).getTitle());
}
return newTitle;
}
}
150 changes: 150 additions & 0 deletions com1614080901217/jsonFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
[{
"id": 0,
"title": "来至网络请求的第的0条标题",
"content": "来至网络请求的第的0条内容"
},
{
"id": 1,
"title": "来至网络请求的第的1条标题",
"content": "来至网络请求的第的1条内容"
},
{
"id": 2,
"title": "来至网络请求的第的2条标题",
"content": "来至网络请求的第的2条内容"
},
{
"id": 3,
"title": "来至网络请求的第的3条标题",
"content": "来至网络请求的第的3条内容"
},
{
"id": 4,
"title": "来至网络请求的第的4条标题",
"content": "来至网络请求的第的4条内容"
},
{
"id": 5,
"title": "来至网络请求的第的5条标题",
"content": "来至网络请求的第的5条内容"
},
{
"id": 6,
"title": "来至网络请求的第的6条标题",
"content": "来至网络请求的第的6条内容"
},
{
"id": 7,
"title": "来至网络请求的第的7条标题",
"content": "来至网络请求的第的7条内容"
},
{
"id": 8,
"title": "来至网络请求的第的8条标题",
"content": "来至网络请求的第的8条内容"
},
{
"id": 9,
"title": "来至网络请求的第的9条标题",
"content": "来至网络请求的第的9条内容"
},
{
"id": 10,
"title": "来至网络请求的第的10条标题",
"content": "来至网络请求的第的10条内容"
},
{
"id": 11,
"title": "来至网络请求的第的11条标题",
"content": "来至网络请求的第的11条内容"
},
{
"id": 12,
"title": "来至网络请求的第的12条标题",
"content": "来至网络请求的第的12条内容"
},
{
"id": 13,
"title": "来至网络请求的第的13条标题",
"content": "来至网络请求的第的13条内容"
},
{
"id": 14,
"title": "来至网络请求的第的14条标题",
"content": "来至网络请求的第的14条内容"
},
{
"id": 15,
"title": "来至网络请求的第的15条标题",
"content": "来至网络请求的第的15条内容"
},
{
"id": 16,
"title": "来至网络请求的第的16条标题",
"content": "来至网络请求的第的16条内容"
},
{
"id": 17,
"title": "来至网络请求的第的17条标题",
"content": "来至网络请求的第的17条内容"
},
{
"id": 18,
"title": "来至网络请求的第的18条标题",
"content": "来至网络请求的第的18条内容"
},
{
"id": 19,
"title": "来至网络请求的第的19条标题",
"content": "来至网络请求的第的19条内容"
},
{
"id": 20,
"title": "来至网络请求的第的20条标题",
"content": "来至网络请求的第的20条内容"
},
{
"id": 21,
"title": "来至网络请求的第的21条标题",
"content": "来至网络请求的第的21条内容"
},
{
"id": 22,
"title": "来至网络请求的第的22条标题",
"content": "来至网络请求的第的22条内容"
},
{
"id": 23,
"title": "来至网络请求的第的23条标题",
"content": "来至网络请求的第的23条内容"
},
{
"id": 24,
"title": "来至网络请求的第的24条标题",
"content": "来至网络请求的第的24条内容"
},
{
"id": 25,
"title": "来至网络请求的第的25条标题",
"content": "来至网络请求的第的25条内容"
},
{
"id": 26,
"title": "来至网络请求的第的26条标题",
"content": "来至网络请求的第的26条内容"
},
{
"id": 27,
"title": "来至网络请求的第的27条标题",
"content": "来至网络请求的第的27条内容"
},
{
"id": 28,
"title": "来至网络请求的第的28条标题",
"content": "来至网络请求的第的28条内容"
},
{
"id": 29,
"title": "来至网络请求的第的29条标题",
"content": "来至网络请求的第的29条内容"
}]
Loading