前辈请教个问题,怎在自定义的dialog中使用getSharedPreferences啊?

rexuechaoren 发布于 2013/12/20 15:46
阅读 952
收藏 0

@迷途d书童 你好,想跟你请教个问题:

前辈请教个问题,怎在自定义的dialog中使用getSharedPreferences啊?

public class MyDialog extends Dialog{

	Context context;
    public MyDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }
    public MyDialog(Context context, int theme){
        super(context, theme);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog);
		final EditText phone_num = (EditText) this.findViewById(R.id.phone_num);
		final SharedPreferences sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		final String username = sp.getString("username", "");
       
		Button btn_ok = (Button) this.findViewById(R.id.dialog_button_ok);
		btn_ok.setOnClickListener(new android.view.View.OnClickListener() {
				public void onClick(View view) {
					//Toast.makeText(context, "hello", 0).show();//显示一个提示
		//MyDialog.this.dismiss();
		System.out.println("121212121212"+phone_num.getText());
		try {
			String result = dopost(phone_num.getText().toString(),"");
			System.out.println(result);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        
        Button btn_cancel = (Button) this.findViewById(R.id.dialog_button_cancel);
        btn_cancel.setOnClickListener(new android.view.View.OnClickListener() {
			public void onClick(View view) {
				//Toast.makeText(context, "hello", 0).show();//显示一个提示
				MyDialog.this.dismiss();
				System.out.println("cancel");
			}
		});
        
    }

	private String dopost(String phone_num,String result) throws JSONException{
	     //封装数据
		 Map<String, String> parmas = new HashMap<String, String>();
		 System.out.println(phone_num+"#####################");
		 parmas.put("username", phone_num);
		 DefaultHttpClient client = new DefaultHttpClient();//http客户端
		 HttpPost httpPost = new HttpPost("http://www.huayuanbuy.com/prize.php?action=app_table");
		 ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		 if(parmas != null){
		     Set<String> keys = parmas.keySet();
		     for(Iterator<String> i = keys.iterator(); i.hasNext();) {
		          String key = (String)i.next();
		          pairs.add(new BasicNameValuePair(key, parmas.get(key)));
		     }
		}
	  try {
		   UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "utf-8");
		 /*
		  *  将POST数据放入HTTP请求
		  */
		 httpPost.setEntity(p_entity);
		 /*
		  *  发出实际的HTTP POST请求
		   */
		         HttpResponse response = client.execute(httpPost);
		         HttpEntity entity = response.getEntity();
		         InputStream content = entity.getContent();
		   String returnConnection = convertStreamToString(content);
		        
				//TextView show = null;//自定义
		//show.setText(returnConnection);
		   JSONObject jsonObject = new JSONObject(returnConnection);
		   result = jsonObject.getString("result");
		      
		  } catch (IllegalStateException e) {
		   e.printStackTrace();
		  } catch (IOException e) {
		   e.printStackTrace();
		  }
		  return result;
	 }
	
	  private String convertStreamToString(InputStream is) {
		   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		         StringBuilder sb = new StringBuilder();
		         String line = null;
		         try {
		              while ((line = reader.readLine()) != null) {
		                   sb.append(line);
		              }
		         } catch (IOException e) {
		              e.printStackTrace();
		         } finally {
		              try {
		                   is.close();
		              } catch (IOException e) {
		                   e.printStackTrace();
		              }
		         }
		         return sb.toString();
		 }
    

}



以下是问题补充:

@rexuechaoren:ps:第19行的getSharedPreferences 无法使用,让重新定义 (2013/12/20 15:47)
加载中
0
Sherard
Sherard

 final SharedPreferences sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);

改成

 final SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);

0
rexuechaoren
rexuechaoren
多谢大神前辈相助,太帅气了,完美解决!
返回顶部
顶部