mardi 23 février 2016

Android - fetch data from php and mysql and save it to SQLite data after this fetch the data to listview from SQLite Database

hello all > here i have listview in android its fetching the data from PHP and MYSQL >> now i need to Fetch this data and Save it in SQLite Database first then populate the list view from SQLite Database onCreate will put my code now i really need a way to do this

SupportActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.activity_support); 

    initView();  
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.support, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.refresh) {

        initView();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");   
    db = new SQLiteHandler(getApplicationContext());
    HashMap<String, String> detail = db.getUserDetails();
    String empid = detail.get("empid");
    String url = "http://ift.tt/1PXImGU"+empid;
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);   
}
@Override
public void onFetchComplete(List<Application> data) {
    System.out.println("data is " + data);
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);     
    adapter.notifyDataSetChanged();
}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
}

FetchDataTask.java

public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;

public FetchDataTask(FetchDataListener listener) {
    this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
    if(params == null) return null;

    // get url from params
    String url = params[0];

    try {
        // create http connection
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        // connect
        HttpResponse response = client.execute(httpget);

        // get response
        HttpEntity entity = response.getEntity();

        if(entity == null) {
            msg = "No response from server";
            return null;        
        }

        // get response content and convert it to json string
        InputStream is = entity.getContent();
        return streamToString(is);
    }
    catch(IOException e){
        msg = "No Network Connection";
    }

    return null;
}

@Override
protected void onPostExecute(String sJson) {
    if(sJson == null) {
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        

    try {
        // convert json string to json array
        JSONArray aJson = new JSONArray(sJson);

        // create apps list
        List<Application> apps = new ArrayList<Application>();

        for(int i=0; i<aJson.length(); i++) {
            JSONObject json = aJson.getJSONObject(i);
            Application app = new Application();
            app.setnames(json.getString("names"));
            app.setTitle(json.getString("app_title"));
            app.setlat1(json.getString("app_lat1"));
            app.setlog1(json.getString("app_log1"));   
            app.setclientid(json.getString("app_clientid"));
            app.setempid(json.getString("empid"));
            app.setviscode(json.getString("app_viscode"));
            app.setclienttype(json.getString("app_clienttype"));
            app.setTel1(json.getString("app_Tel1"));
            app.setmobile1(json.getString("app_mobile1"));
            app.setaddr1(json.getString("app_addr1"));
            app.setccost(json.getString("app_ccost"));
            app.setTimeStamp(json.getString("date"));
            app.setVisitT(json.getString("VisitT"));
            app.setVisitR(json.getString("VisitR")); 
            app.setIcon(json.getString("icon"));

            // add the app to apps list
            apps.add(app);
        }

        //notify the activity that fetch data has been complete
        if(listener != null) listener.onFetchComplete(apps);
    } catch (JSONException e) {
        msg = "Invalid response";
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        
}

/**
 * This function will convert response stream into json string
 * @param is respons string
 * @return json string
 * @throws IOException
 */
public String streamToString(final InputStream is) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder(); 
    String line = null;

    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } 
    catch (IOException e) {
        throw e;
    } 
    finally {           
        try {
            is.close();
        } 
        catch (IOException e) {
            throw e;
        }
    }

    return sb.toString();
}}

ApplicationAdtpter.java

public class ApplicationAdapter extends ArrayAdapter<Application>{
private List<Application> items;

public ApplicationAdapter(Context context, List<Application> items) {
    super(context, R.layout.app_custom_list, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.app_custom_list, null);            
    }

    Application app = items.get(position);


  if(app != null) {
        ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
        TextView names = (TextView)v.findViewById(R.id.tv100);
        TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
        TextView lat1 = (TextView)v.findViewById(R.id.lat1);
        TextView log1 = (TextView)v.findViewById(R.id.log1);
  //      LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
        TextView timestamp = (TextView)v.findViewById(R.id.timestamp);
        TextView clientid = (TextView)v.findViewById(R.id.ccode);
        TextView empid = (TextView)v.findViewById(R.id.ecode);
        TextView clienttype = (TextView)v.findViewById(R.id.tvvv44);
        TextView viscode = (TextView)v.findViewById(R.id.vcode);
        TextView Tel1 = (TextView)v.findViewById(R.id.tvtel);
        TextView mobile1 = (TextView)v.findViewById(R.id.tvmob);
        TextView addr1 = (TextView)v.findViewById(R.id.tvaddr);
        TextView ccost = (TextView)v.findViewById(R.id.ccost);
        TextView VisitT = (TextView)v.findViewById(R.id.tv102);
        TextView VisitR = (TextView)v.findViewById(R.id.tv103);

        if(icon != null) {
            Resources res = getContext().getResources();
            String sIcon = "com.benitkibabu.sap:drawable/" + app.getIcon();
            icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
        }

        if(names != null){ names.setText(app.getnames());}
        if(titleText != null){ titleText.setText(app.getTitle());}
        if(lat1 != null){ lat1.setText(app.getlat1());}
        if(log1 != null){ log1.setText(app.getlog1());}
        if(clientid != null){ clientid.setText(app.getclientid());}
        if(empid != null){ empid.setText(app.getempid());}
        if(viscode != null){ viscode.setText(app.getviscode());}
        if(clienttype != null){ clienttype.setText(app.getclienttype());}
        if(Tel1 != null){ Tel1.setText(app.getTel1());}
        if(mobile1 != null){ mobile1.setText(app.getmobile1());}
        if(addr1 != null){ addr1.setText(app.getaddr1());}
        if(ccost != null){ ccost.setText(app.getccost());}
        if(VisitT != null){ VisitT.setText(app.getVisitT());}
        if(VisitR != null){ VisitR.setText(app.getVisitR());}



        // Converting timestamp into x ago format
        // Converting timestamp into x ago format
        long unixSeconds = 
                Long.parseLong(app.getTimeStamp());
        Date date = new Date(unixSeconds*1000L); // *1000 is to convert seconds to milliseconds
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // the format of your date
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-4")); // give a timezone reference for formating (see comment at the bottom
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate);
        timestamp.setText(formattedDate);

    return v;
}}

Application.java

public class Application {
     private String names;
     private String title;
     private String lat1;
     private String log1;
     private String clientid;
     private String empid;
     private String viscode;
     private String clienttype;
     private String Tel1;
     private String mobile1;
     private String addr1;
     private String ccost;
     private String timeStamp;
     private String VisitT;
     private String icon;
     private String VisitR;


public String getnames() {
   return names;
}
public void setnames(String names) {
   this.names = names;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getlat1() {
    return lat1;
}
public void setlat1(String lat1) {
    this.lat1 = lat1;
}public String getlog1() {
    return log1;
}
public void setlog1(String log1) {
    this.log1 = log1;
}
public String getclientid() {
    return clientid;
}
public void setclientid(String clientid) {
    this.clientid = clientid;
}
public String getempid() {
    return empid;
}
public void setempid(String empid) {
    this.empid = empid;
}
public String getviscode() {
    return viscode;
}
public void setviscode(String viscode) {
    this.viscode = viscode;
}
public String getclienttype() {
    return clienttype;
}
public void setclienttype(String clienttype) {
    this.clienttype = clienttype;
}
public String getTel1() {
    return Tel1;
}
public void setTel1(String Tel1) {
    this.Tel1 = Tel1;
}
public String getmobile1() {
    return mobile1;
}
public void setmobile1(String mobile1) {
    this.mobile1 = mobile1;
}
public String getaddr1() {
    return addr1;
}
public void setaddr1(String addr1) {
    this.addr1 = addr1;
}
public String getccost() {
    return ccost;
}
public void setccost(String ccost) {
    this.ccost = ccost;
}
public String getTimeStamp() {
    return timeStamp;
}
public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}
public String getVisitT() {
    return VisitT;
}
public void setVisitT(String VisitT) {
    this.VisitT = VisitT;
}
public String getVisitR() {
    return VisitR;
}
public void setVisitR(String VisitR) {
    this.VisitR = VisitR;
}
public String getIcon() {
    return icon;
}
public void setIcon(String icon) {
    this.icon = icon;
}}

Aucun commentaire:

Enregistrer un commentaire