Android 在单击getJson时,如何在sqlite数据库中插入已解析的Json对象?
你可以用Android 在单击getJson时,如何在sqlite数据库中插入已解析的Json对象?,android,json,sqlite,Android,Json,Sqlite,你可以用 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
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;
}
}
也许您可以尝试将Json转换为Pojo。然后使用orm将其插入sqlite,或者通过将pojo传递给数据库处理程序来使用简单查询。
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);
}
}
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;
}
}
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();
}
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)");