Results 1 to 5 of 5
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default is any way pass value to previous activity by sharedpreferences

    Dear Friends!

    First of all I Thanks for your solution on my previous bug..,

    I have a problem while pass value from Dialog into my previous activity by preference (without calling the intent)

    if any action to happen while activity start we will call " protected void onCreate(Bundle savedInstanceState) { } " method. But i wants to call an action when back to my activity (without using intent)..,

    i have the custom list view
    1. to open the custom list list view by onclick button_1
    2. select the list of items
    3. back to my main activity by click button_2


    i can view the list of item using Toast. But i unable to carry the value by using the previous activity


    The Code of my custom listview

    Java Code:
    package com.raj.coldpoint;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import com.raj.mylist.AlertDialogManager;
    import com.raj.mylist.CallReptAdapter;
    import com.raj.mylist.CallReptModel;
    import com.raj.mylist.ConnectionDetector;
    import com.raj.mylist.JSONParser;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.content.SharedPreferences;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class SerRendList extends ListActivity implements OnClickListener {
    
    	ConnectionDetector cd;
    
    	AlertDialogManager alert = new AlertDialogManager();
    
    	private ProgressDialog pDialog;
    
    	JSONParser jsonParser = new JSONParser();
    
    	ArrayList<HashMap<String, String>> tracksList;
    
    	private List<CallReptModel> ckmod;
    
    	private CallReptAdapter adaptr = null;
    	Button sltChk;
    	CheckBox cb;
    	TextView abmsg;
    	String abr, msg;
    	private static final String Tag_url = "http://androidcrm.urspaly.com/abbreviations.php";
    	private static final String Tag_AbbVarCode = "AbbVarCode";
    	private static final String Tag_AbbVarMsg = "AbbVarMsg";
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_ser_rend_list);
    		cb=(CheckBox) findViewById(R.id.checkBox1);
    		sltChk = (Button) findViewById(R.id.chechedlist);
    		abmsg = (TextView) findViewById(R.id.textView2);
    		sltChk.setOnClickListener(this);
    		cd = new ConnectionDetector(getApplicationContext());
    		if (!cd.isConnectingToInternet()) {
    			alert.showAlertDialog(SerRendList.this,
    					"Internet Connection Error",
    					"Please connect to working Internet connection", false);
    			return;
    		}
    		tracksList = new ArrayList<HashMap<String, String>>();
    		ckmod = new ArrayList<CallReptModel>();
    
    		new LoadSrChkList().execute();
    	}
    
    	private void fillData(String messsage, String ckbr, boolean isSelected) {
    		final CallReptModel model = new CallReptModel();
    		model.setMesssage(messsage);
    		Log.d("messsage", messsage);
    		model.setSelectred(isSelected);
    		model.setCkabr(ckbr);
    		Log.d("ckbr", ckbr);
    		ckmod.add(model);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.ser_rend_list, menu);
    		return true;
    	}
    
    	class LoadSrChkList extends AsyncTask<String, String, String> {
    
    		@Override
    		protected void onPreExecute() {
    			// TODO Auto-generated method stub
    			super.onPreExecute();
    			pDialog = new ProgressDialog(SerRendList.this);
    			pDialog.setMessage("Please Wait...");
    			pDialog.setIndeterminate(false);
    			pDialog.setCancelable(false);
    			pDialog.show();
    		}
    
    		@Override
    		protected String doInBackground(String... arg0) {
    			// TODO Auto-generated method stub
    			List<NameValuePair> params = new ArrayList<NameValuePair>();
    			String json = jsonParser.makeHttpRequest(Tag_url, "GET", params);
    			Log.d("Chk List", json);
    			try {
    				JSONArray jObj = new JSONArray(json);
    				if (jObj != null) {
    					for (int i = 0; i < jObj.length(); i++) {
    						JSONObject c = jObj.getJSONObject(i);
    						String abrr = c.getString(Tag_AbbVarCode);
    						String msgg = c.getString(Tag_AbbVarMsg);
    						HashMap<String, String> map = new HashMap<String, String>();
    						map.put(Tag_AbbVarCode, abrr);
    						map.put(Tag_AbbVarMsg, msgg);
    						tracksList.add(map);
    						fillData(msgg, abrr, false);
    					}
    				}
    			} catch (JSONException d) {
    				d.printStackTrace();
    			}
    
    			return null;
    		}
    
    		@Override
    		protected void onPostExecute(String result) {
    			// TODO Auto-generated method stub
    			pDialog.dismiss();
    			runOnUiThread(new Runnable() {
    				public void run() {
    					adaptr = new CallReptAdapter(SerRendList.this,R.layout.serrenchkbx, ckmod);
    					setListAdapter(adaptr);
    				}
    			});
    		}
    	}
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		StringBuffer responseText = new StringBuffer();
    		responseText.append("You Selec The following Items...\n");
    
    		ArrayList<CallReptModel> stateList = (ArrayList<CallReptModel>) adaptr.crmodel;
    
    		for (int i = 0; i < stateList.size(); i++) {
    			CallReptModel state = stateList.get(i);
    
    			if (state.isSelectred()) {
    				responseText.append("\n" + state.getMesssage());
    			}
    		}
    
    		Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG)
    				.show();
    		String rl=responseText.toString();
    		SharedPreferences sharf=PreferenceManager.getDefaultSharedPreferences(this);
    		SharedPreferences.Editor et=sharf.edit();
    		et.putString("slst", rl);
    		finish();
    	}
    }

    The part of code on my main Activity

    Java Code:
    EditText srend;
    Java Code:
    srend = (EditText) findViewById(R.id.r_serrend);
    Java Code:
    srend.addTextChangedListener(ServiceReport.this);
    Java Code:
    @Override
    	public void onTextChanged(CharSequence s, int start, int before, int count) {
    		// TODO Auto-generated method stub
    		Is_Valid_Service_Render(srend);
    		SharedPreferences sharp=PreferenceManager.getDefaultSharedPreferences(this);
    		String ckval=sharp.getString("slst","");
    		if(!ckval.equalsIgnoreCase(""))
    		  {
    			ckval = ckval+srend.getText();
    		    srend.setText(ckval);
    		  }
    		Is_Valid_Remarks(remk);
    	}
    is any way pass value to previous activity by sharedpreferences-custom.png

    where / on which i have to place the sharedpreference to get & past my value on that EditText..,


    Can Any one Guide me please..,

    I am looking for your solution..,

    Thank You!

    Have A Happy Day..,
    Attached Thumbnails Attached Thumbnails is any way pass value to previous activity by sharedpreferences-custom.png  
    Last edited by raj.mscking@gmail.com; 05-15-2013 at 03:27 PM. Reason: to reduce the image load time
    - Raj

  2. #2
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default Re: is any way pass value to previous activity by sharedpreferences

    I got the Answer..,


    i am handle this shared-preferences by using life cycle of activity

    My button on click on Dialog screen after selecting the checked items
    Java Code:
    @Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		StringBuffer responseText = new StringBuffer();
    		responseText.append("You Selec The following Items...\n");
    
    		ArrayList<CallReptModel> stateList = (ArrayList<CallReptModel>) adaptr.crmodel;
    
    		for (int i = 0; i < stateList.size(); i++) {
    			CallReptModel state = stateList.get(i);
    
    			if (state.isSelectred()) {
    				responseText.append("\n" + state.getMesssage());
    			}
    		}
    
    		Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG)
    				.show();
    		String rl=responseText.toString();
    		SharedPreferences sharf=getSharedPreferences(PREF_NAME,Activity.MODE_PRIVATE);
    		SharedPreferences.Editor et=sharf.edit();
    		et.putString("slst", rl);
                    et.commit();
    		finish();
    	}
    My main screen activity code

    Java Code:
    @Override
    	protected void onResume() {
    		// TODO Auto-generated method stub
    		super.onResume();
    
    		SharedPreferences sharf=getSharedPreferences(SerRendList.PREF_NAME,Activity.MODE_PRIVATE);
    		SharedPreferences.Editor et=sharf.edit();
    		String ckval=sharf.getString("slst","");
    		if(!ckval.equalsIgnoreCase(""))
    		  {
    			ckval = srend.getText()+ckval;
    			Log.d("Respones..,", ckval);
    		    srend.setText(ckval);
    		  }
    	}
    Thanks friends who ever try to give solution for my problems..,

    Have A Happy Day..,
    Last edited by raj.mscking@gmail.com; 05-16-2013 at 12:51 PM.
    - Raj

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: is any way pass value to previous activity by sharedpreferences

    Is there a reason you can't get a result in the previous activity using onActivityResult? Activity | Android Developers
    Get in the habit of using standard Java naming conventions!

  4. #4
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default Re: is any way pass value to previous activity by sharedpreferences

    Ok sir!
    hereafter i will fallow as you said..,
    Thank you for your valuable advice..,
    Have A Happy Day..,
    - Raj

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default Re: is any way pass value to previous activity by sharedpreferences

    Dear All!

    I solve the above problem by using startActivityForResult() method instead of Preferences in activity..,

    I thanks for help me to solve the problem..,

    I Thanks A Lot..,

    Have A Happy Day..,
    - Raj

Similar Threads

  1. Activity Manager 0.4.2
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-27-2013, 02:31 AM
  2. Activity Manager 0.4.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 04-04-2012, 05:57 PM
  3. Replies: 0
    Last Post: 10-01-2011, 04:14 AM
  4. Replies: 1
    Last Post: 09-12-2011, 02:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •