我在资产文件夹中有sqlite文件,并将其复制到sqlite数据库中,在安装应用程序时,它会显示给用户。 现在我想从服务器json文件插入新数据。我从服务器获取了json数据并成功完成了解析部分,但它没有插入到现有数据库中。 用于解析json数据并在列表中显示的代码。代码中没有任何错误。但是我无法将其插入到现有的sqlite数据库中。
public void get_data(String data)
{
try {
JSONArray data_array=new JSONArray(data);
for (int i = 0 ; i < data_array.length() ; i++)
{
JSONObject obj=new JSONObject(data_array.get(i).toString());
Message add=new Message();
add.category = obj.getString("category");
add.status = obj.getString("status");
add.imagelink=obj.getString("imglink");
objlist.add(add);
db1.addSMS(add);
}
new ListAdapter(CatagoryActivity.this, objlist).notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
public ArrayList<Message> getMessages() {
ArrayList<Message> objlist_msg = new ArrayList<Message>();
Message obj;
adp.open();
Cursor c = adp.Catagorylist();
c.moveToFirst();
/*
* Toast.makeText(CatagoryActivity.this, "" + c.getCount(),
* Toast.LENGTH_LONG) .show();
*/
for (int i = 0; i < c.getCount(); i++) {
obj = new Message();
String msg = c
.getString(c.getColumnIndex(MessagesAdapter.CATEGORY));
String imagelink = c.getString(c
.getColumnIndex(MessagesAdapter.Imagelink));
obj.setImagelink(imagelink);
Cursor c1 = adp.getcount(msg);
c1.moveToFirst();
int count = c1.getCount();
obj.setCategory(msg);
obj.setCount(String.valueOf(count));
objlist_msg.add(obj);
c.moveToNext();
}
adp.close();
return objlist_msg;
}
public class ListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Message> objlist;
public ListAdapter(Context context, ArrayList<Message> objlist) {
this.context = context;
this.objlist = objlist;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.catagory_list_raw, null);
holder.txt = (TextView) convertView.findViewById(R.id.text);
holder.count = (TextView) convertView.findViewById(R.id.count);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.txt.setText(objlist.get(position).getCategory().toString());
return convertView;
}
@Override
public int getCount() {
return objlist.size();
}
@Override
public Object getItem(int position) {
return objlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
class ViewHolder{
TextView txt,count;
}
//This function in Database.java file
public void addSMS(Message sms) {
SQLiteDatabase db123 = this.getWritableDatabase();
try{
ContentValues values = new ContentValues();
values.put(MessagesAdapter.CATEGORY, sms.getCategory());
values.put(MessagesAdapter.Status, sms.getStatus());
values.put(MessagesAdapter.Imagelink,sms.getImagelink());
db123.insert(MessagesAdapter.TBL_CATEGORY, null, values);
//Log.e("TAG","JSON DATA INSERTED");
db123.close();
}catch (Exception e){
Log.e("problem",e+"");
}
}