//**MainActivity**
-----------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
String json_string;
public ProgressBar progressBar;
public TextView textView;
Toolbar toolbar;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getJson(View view){
new MainActivity.BackgroundTask(this).execute();
}
public class BackgroundTask extends AsyncTask<Void,Void,String> {
String json_url;
String JSON_STRING;
Context context;
public BackgroundTask(Context context) {
this.context = context;
}
@Override
public void onPreExecute(){
progressDialog=new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Loading.....Please Wait");
progressDialog.show();
json_url="http://demo5629995.mockable.io/Employee1";
}
@Override
protected String doInBackground(Void... params) {
try{
URL url=new URL(json_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while ((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Void values){
super.onProgressUpdate(values);
}
public void onPostExecute(String result){
progressDialog.dismiss();
TextView textView= (TextView) findViewById(R.id.tvjson);
textView.setText(result);
json_string=result;
}
}
public void parseJson(View view){
if(json_string==null){
Toast.makeText(this,"get Json",Toast.LENGTH_LONG).show();
}
else {
Intent intent=new Intent(this,JsonDisplayActivity.class);
intent.putExtra("json_data",json_string);
startActivity(intent);
}
}
}
// JsonDisplayActivity _______________________________________________________________
public class JsonDisplayActivity extends Activity {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
EmployeeAdapter employeeAdapter;
ListView listView;
List<employee> empList = new ArrayList<>();
DatabaseHandler databaseHandler=new DatabaseHandler(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaylistview);
listView= (ListView) findViewById(R.id.listview);
json_string=getIntent().getExtras().getString("json_data");
try {
jsonObject=new JSONObject(json_string);
jsonArray=jsonObject.getJSONArray("emp");
int count=0;
String name,email,url;
while (count<jsonArray.length()){
JSONObject jo=jsonArray.optJSONObject(count);
name=jo.getString("Name");
email=jo.getString("Email");
url=jo.getString("Url");
employee emp=new employee(name,email,url);
// employeeAdapter.add(emp);
empList.add(emp);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
employeeAdapter=new EmployeeAdapter(this,R.layout.activity_row,empList);
listView.setAdapter(employeeAdapter);
}
}
// EmployeeAdapter _____________________________________________________________________
public class EmployeeAdapter extends ArrayAdapter {
List list;
public EmployeeAdapter(Context context, int resource,List<employee> employeeList) {
super(context, resource);
list=employeeList;
}
public void add(employee object){
super.add(object);
list.add(object);
}
public int getCount(){
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row=convertView;
EmployeeHolder employeeholder;
if(row==null){
LayoutInflater layoutInflater= (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.activity_row,parent,false);
employeeholder=new EmployeeHolder();
employeeholder.tv_name= (TextView) row.findViewById(R.id.tvname);
employeeholder.tv_email= (TextView) row.findViewById(R.id.tvemail);
employeeholder.tv_url= (TextView) row.findViewById(R.id.tvUrl);
row.setTag(employeeholder);
}
else {
employeeholder=(EmployeeHolder)row.getTag();
}
// employee emp= (employee) this.getItem(position);
employee emp= (employee) list.get(position);
employeeholder.tv_name.setText(emp.getName());
employeeholder.tv_email.setText(emp.getEmail());
employeeholder.tv_url.setText(emp.getUrl());
return row;
}
static class EmployeeHolder{
TextView tv_name,tv_email,tv_url;
}
}
// 员工 __________________________________________________________
public class employee {
String name;
String email;
String url;
public employee(String name, String email,String url){
this.setName(name);
this.setEmail(email);
this.setUrl(url);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
答案 0 :(得分:0)
您可以使用
ArrayList<HashMap<String, String>> DETAILS = new ArrayList<HashMap<String, String>>();
try {
JSONArray data = new JSONArray(/*YOUR JSON DATA*/);
DETAILS.clear();
HashMap<String, String> map;
int i = 0;
//Users.clear();
for(i = 0; i < data.length(); i++){
HashMap<String, String> hashmap = new HashMap<String, String>();
JSONObject c = data.getJSONObject(i);
hashmap.put("NAME", c.getString("NAME"));
hashmap.put("EMAIL", c.getString("EMAIL"));
hashmap.put("URL", c.getString("URL"));
DETAILS.add(hashmap);
}
if (i >= data.length())
{
DatabaseHelper DH = new DatabaseHelper(getApplicationContext());
DH.addIntoUsers(DETAILS);
DH.close();
Log.d("USERS:", ""+DETAILS);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在您的DatabaseHelper类中,您应该声明函数
public void addIntoCases(ArrayList<?> arraylist)
{
SQLiteDatabase sqlitedatabase = getWritableDatabase();
sqlitedatabase.delete("EMPLOYEE_TBL", null, null);
ContentValues contentvalues = new ContentValues();
int i = 0;
for(i=0;i<arraylist.size();i++){
contentvalues.put("NAME", (String)((HashMap<?, ?>)arraylist.get(i)).get("NAME"));
contentvalues.put("EMAIL", (String)((HashMap)arraylist.get(i)).get("EMAIL"));
contentvalues.put("URL", (String)((HashMap<?, ?>)arraylist.get(i)).get("URL"));
sqlitedatabase.insert("EMPLOYEE_TBL", null, contentvalues);
}
return;
}
你的桌子应该像:
sqlitedatabase.execSQL("CREATE TABLE IF NOT EXISTS EMPLOYEE_TBL(_id INTEGER PRIMARY KEY,NAME TEXT,EMAIL TEXT,URL TEXT)");