使用sharedpreferences在另一个活动中使用edittext在android小部件中设置textview [英] Set textview inside android widget from edittext in another activity using sharedpreferences

查看:82
本文介绍了使用sharedpreferences在另一个活动中使用edittext在android小部件中设置textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I wrote a program to my friend to calculate the fuel litres and prices

It's a simple layout that let him enter all the counters of the fuel machines and the prices and then within a button click all the results will show in a specific textviews

MainActivity Class

    public class MainActivity extends Activity
    {
	Button result;
	 EditText b951,b952,b953,b954,b955,b956,b981,b982,b983,b984,b985,b986,red1,green1;
	EditText b9511,b9522,b9533,b9544,b9555,b9566,b9811,b9822,b9833,b9844,b9855,b9866,red2,green2,oil;
	EditText sum95,sum98,sumred,sumgreen;
	EditText total,t95,t98,tred,tgreen;
	EditText p95,p98,pred,pgreen;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		result = (Button)findViewById(R.id.btn1);
		b951 = (EditText)findViewById(R.id.b951);
		b952 = (EditText)findViewById(R.id.b952);
		b953 = (EditText)findViewById(R.id.b953);
		b954 = (EditText)findViewById(R.id.b954);
		b955 = (EditText)findViewById(R.id.b955);
		b956 = (EditText)findViewById(R.id.b956);
		b981 = (EditText)findViewById(R.id.b981);
		b982 = (EditText)findViewById(R.id.b982);
		b983 = (EditText)findViewById(R.id.b983);
		b984 = (EditText)findViewById(R.id.b984);
		b985 = (EditText)findViewById(R.id.b985);
		b986 = (EditText)findViewById(R.id.b986);
		red1 = (EditText)findViewById(R.id.red1);
		green1 = (EditText)findViewById(R.id.green1);
		b9511 = (EditText)findViewById(R.id.b9511);
		b9522 = (EditText)findViewById(R.id.b9522);
		b9533 = (EditText)findViewById(R.id.b9533);
		b9544 = (EditText)findViewById(R.id.b9544);
		b9555 = (EditText)findViewById(R.id.b9555);
		b9566 = (EditText)findViewById(R.id.b9566);
		b9811 = (EditText)findViewById(R.id.b9811);
		b9822 = (EditText)findViewById(R.id.b9822);
		b9833 = (EditText)findViewById(R.id.b9833);
		b9844 = (EditText)findViewById(R.id.b9844);
		b9855 = (EditText)findViewById(R.id.b9855);
		b9866 = (EditText)findViewById(R.id.b9866);
		red2 = (EditText)findViewById(R.id.red2);
		green2 = (EditText)findViewById(R.id.green2);
		oil = (EditText)findViewById(R.id.oil);
		sum95 = (EditText)findViewById(R.id.sum95);
		sum98 = (EditText)findViewById(R.id.sum98);
		sumred = (EditText)findViewById(R.id.sumred);
		sumgreen = (EditText)findViewById(R.id.sumgreen);
		p95 = (EditText)findViewById(R.id.p95);
		p98 = (EditText)findViewById(R.id.p98);
		pred = (EditText)findViewById(R.id.pred);
		pgreen = (EditText)findViewById(R.id.pgreen);
		total = (EditText)findViewById(R.id.total);
		t95 = (EditText)findViewById(R.id.t95);
		t98 = (EditText)findViewById(R.id.t98);
		tred = (EditText)findViewById(R.id.tred);
		tgreen = (EditText)findViewById(R.id.tgreen);
		LoadPreferences();
	}
		
	public void clear (View v){
		b951.setText("");
		b9511.setText("");
		b952.setText("");
		b9522.setText("");
		b953.setText("");
		b9533.setText("");
		b954.setText("");
		b9544.setText("");
		b955.setText("");
		b9555.setText("");
		b956.setText("");
		b9566.setText("");

		b981.setText("");
		b9811.setText("");
		b982.setText("");
		b9822.setText("");
		b983.setText("");
		b9833.setText("");
		b984.setText("");
		b9844.setText("");
		b985.setText("");
		b9855.setText("");
		b986.setText("");
		b9866.setText("");

		red1.setText("");
		red2.setText("");
		green1.setText("");
		green2.setText("");
		
		oil.setText("");

		p95.setText("");
		p98.setText("");
		pred.setText("");
		pgreen.setText("");
		
		sum95.setText("");
		sum98.setText("");
		sumred.setText("");
		sumgreen.setText("");
		
		t95.setText("");
		t98.setText("");
		tred.setText("");
		tgreen.setText("");
		
		total.setText("");
	}
	
	public void close (View v){
		SavePreferences("p2b951", b951.getText().toString());
		SavePreferences("p2b9511", b9511.getText().toString());
		SavePreferences("p2b952", b952.getText().toString());
		SavePreferences("p2b9522", b9522.getText().toString());
		SavePreferences("p2b953", b953.getText().toString());
		SavePreferences("p2b9533", b9533.getText().toString());
		SavePreferences("p2b954", b954.getText().toString());
		SavePreferences("p2b9544", b9544.getText().toString());
		SavePreferences("p2b955", b955.getText().toString());
		SavePreferences("p2b9555", b9555.getText().toString());
		SavePreferences("p2b956", b956.getText().toString());
		SavePreferences("p2b9566", b9566.getText().toString());

		SavePreferences("p2b981", b981.getText().toString());
		SavePreferences("p2b9811", b9811.getText().toString());
		SavePreferences("p2b982", b982.getText().toString());
		SavePreferences("p2b9822", b9822.getText().toString());
		SavePreferences("p2b983", b983.getText().toString());
		SavePreferences("p2b9833", b9833.getText().toString());
		SavePreferences("p2b984", b984.getText().toString());
		SavePreferences("p2b9844", b9844.getText().toString());
		SavePreferences("p2b985", b985.getText().toString());
		SavePreferences("p2b9855", b9855.getText().toString());
		SavePreferences("p2b986", b986.getText().toString());
		SavePreferences("p2b9866", b9866.getText().toString());
		
		SavePreferences("p2red1", red1.getText().toString());
		SavePreferences("p2red2", red2.getText().toString());
		SavePreferences("p2green1", green1.getText().toString());
		SavePreferences("p2green2", green2.getText().toString());
		
		SavePreferences("p2oil", oil.getText().toString());
		
		SavePreferences("p2p95", p95.getText().toString());
		SavePreferences("p2p98", p98.getText().toString());
		SavePreferences("p2pred", pred.getText().toString());
		SavePreferences("p2pgreen", pgreen.getText().toString());
		
		finish();
		
	}
	
	
	private void SavePreferences(String key, String value){
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	private void LoadPreferences(){
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		String p1b951 = sharedPreferences.getString("p2b951", "");
		String p1b9511 = sharedPreferences.getString("p2b9511", "");
		String p1b952 = sharedPreferences.getString("p2b952", "");
		String p1b9522 = sharedPreferences.getString("p2b9522", "");
		String p1b953 = sharedPreferences.getString("p2b953", "");
		String p1b9533 = sharedPreferences.getString("p2b9533", "");
		String p1b954 = sharedPreferences.getString("p2b954", "");
		String p1b9544 = sharedPreferences.getString("p2b9544", "");
		String p1b955 = sharedPreferences.getString("p2b955", "");
		String p1b9555 = sharedPreferences.getString("p2b9555", "");
		String p1b956 = sharedPreferences.getString("p2b956", "");
		String p1b9566 = sharedPreferences.getString("p2b9566", "");
		
		String p1b981 = sharedPreferences.getString("p2b981", "");
		String p1b9811 = sharedPreferences.getString("p2b9811", "");
		String p1b982 = sharedPreferences.getString("p2b982", "");
		String p1b9822 = sharedPreferences.getString("p2b9822", "");
		String p1b983 = sharedPreferences.getString("p2b983", "");
		String p1b9833 = sharedPreferences.getString("p2b9833", "");
		String p1b984 = sharedPreferences.getString("p2b984", "");
		String p1b9844 = sharedPreferences.getString("p2b9844", "");
		String p1b985 = sharedPreferences.getString("p2b985", "");
		String p1b9855 = sharedPreferences.getString("p2b9855", "");
		String p1b986 = sharedPreferences.getString("p2b986", "");
		String p1b9866 = sharedPreferences.getString("p2b9866", "");
		
		String p1red1 = sharedPreferences.getString("p2red1", "");
		String p1red2 = sharedPreferences.getString("p2red2", "");
		String p1green1 = sharedPreferences.getString("p2green1", "");
		String p1green2 = sharedPreferences.getString("p2green2", "");
		
		String p1oil = sharedPreferences.getString("p2oil", "");
		
		String p1p95 = sharedPreferences.getString("p2p95", "");
		String p1p98 = sharedPreferences.getString("p2p98", "");
		String p1pred = sharedPreferences.getString("p2pred", "");
		String p1pgreen = sharedPreferences.getString("p2pgreen", "");
		
		b951.setText(p1b951);
		b9511.setText(p1b9511);
		b952.setText(p1b952);
		b9522.setText(p1b9522);
		b953.setText(p1b953);
		b9533.setText(p1b9533);
		b954.setText(p1b954);
		b9544.setText(p1b9544);
		b955.setText(p1b955);
		b9555.setText(p1b9555);
		b956.setText(p1b956);
		b9566.setText(p1b9566);
		
		b981.setText(p1b981);
		b9811.setText(p1b9811);
		b982.setText(p1b982);
		b9822.setText(p1b9822);
		b983.setText(p1b983);
		b9833.setText(p1b9833);
		b984.setText(p1b984);
		b9844.setText(p1b9844);
		b985.setText(p1b985);
		b9855.setText(p1b9855);
		b986.setText(p1b986);
		b9866.setText(p1b9866);
		
		red1.setText(p1red1);
		red2.setText(p1red2);
		green1.setText(p1green1);
		green2.setText(p1green2);
		
		oil.setText(p1oil);
		
		p95.setText(p1p95);
		p98.setText(p1p98);
		pred.setText(p1pred);
		pgreen.setText(p1pgreen);
	}
	
		public void result (View v){
			
			try
		{
				
				int ib951 = Integer.parseInt(b951.getText().toString());
				int ib952 = Integer.parseInt(b952.getText().toString());
				int ib953 = Integer.parseInt(b953.getText().toString());
				int ib954 = Integer.parseInt(b954.getText().toString());
				int ib955 = Integer.parseInt(b955.getText().toString());
				int ib956 = Integer.parseInt(b956.getText().toString());
				int ib981 = Integer.parseInt(b981.getText().toString());
				int ib982 = Integer.parseInt(b982.getText().toString());
				int ib983 = Integer.parseInt(b983.getText().toString());
				int ib984 = Integer.parseInt(b984.getText().toString());
				int ib985 = Integer.parseInt(b985.getText().toString());
				int ib986 = Integer.parseInt(b986.getText().toString());
				int ib9511 = Integer.parseInt(b9511.getText().toString());
				int ib9522 = Integer.parseInt(b9522.getText().toString());
				int ib9533 = Integer.parseInt(b9533.getText().toString());
				int ib9544 = Integer.parseInt(b9544.getText().toString());
				int ib9555 = Integer.parseInt(b9555.getText().toString());
				int ib9566 = Integer.parseInt(b9566.getText().toString());
				int ib9811 = Integer.parseInt(b9811.getText().toString());
				int ib9822 = Integer.parseInt(b9822.getText().toString());
				int ib9833 = Integer.parseInt(b9833.getText().toString());
				int ib9844 = Integer.parseInt(b9844.getText().toString());
				int ib9855 = Integer.parseInt(b9855.getText().toString());
				int ib9866 = Integer.parseInt(b9866.getText().toString());
				int ired1 = Integer.parseInt(red1.getText().toString());
				int igreen1 = Integer.parseInt(green1.getText().toString());
				int ired2 = Integer.parseInt(red2.getText().toString());
				int igreen2 = Integer.parseInt(green2.getText().toString());
				
				
				int iisum95 = ((ib9511-ib951)+(ib9522-ib952)+(ib9533-ib953)+(ib9544-ib954)+(ib9555-ib955)+(ib9566-ib956));
				sum95.setText(String.valueOf(iisum95));
				
				int iisum98 = ((ib9811-ib981)+(ib9822-ib982)+(ib9833-ib983)+(ib9844-ib984)+(ib9855-ib985)+(ib9866-ib986));
				sum98.setText(String.valueOf(iisum98));
				
				int iisumred = (ired2-ired1);
				sumred.setText(String.valueOf(iisumred));
				
				int iisumgreen = (igreen2-igreen1);
				sumgreen.setText(String.valueOf(iisumgreen));
				
				int isum95 = Integer.parseInt(sum95.getText().toString());
				int isum98 = Integer.parseInt(sum98.getText().toString());
				int isumred = Integer.parseInt(sumred.getText().toString());
				int isumgreen = Integer.parseInt(sumgreen.getText().toString());
				int ip95 = Integer.parseInt(p95.getText().toString());
				int ip98 = Integer.parseInt(p98.getText().toString());
				int ipred = Integer.parseInt(pred.getText().toString());
				int ipgreen = Integer.parseInt(pgreen.getText().toString());
				
				int pp95 = (isum95*ip95);
				t95.setText(String.valueOf(pp95));
				
				int pp98 = (isum98*ip98);
				t98.setText(String.valueOf(pp98));
				
				int ppred = (isumred*ipred);
				tred.setText(String.valueOf(ppred));
				
				int ppgreen = (isumgreen*ipgreen);
				tgreen.setText(String.valueOf(ppgreen));
				int it95 = Integer.parseInt(t95.getText().toString());
				int it98 = Integer.parseInt(t98.getText().toString());
				int itred = Integer.parseInt(tred.getText().toString());
				int itgreen = Integer.parseInt(tgreen.getText().toString());
				
				int itoil = Integer.parseInt(oil.getText().toString());
				
				int ttotal = (it95+it98+itred+itgreen+itoil);
				total.setText(String.valueOf(ttotal));
		}
			catch (Exception e)
			{
				e.printStackTrace();
		}

    }
		@Override
		public void onBackPressed()
		{
			// TODO: Implement this method
	}
	
    }

Widget Class


    public class Widget extends AppWidgetProvider
    {
	TextView b95;
 	

	public void onReceive(Context context, Intent intent)
	{

		//LoadPreferences();
		String action = intent.getAction();
		if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
		{

			RemoteViews views = new RemoteViews(context.getPackageName(),
												R.layout.widget);


			AppWidgetManager
				.getInstance(context)
				.updateAppWidget(
				intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
				views);
		}

	}
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
	{
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		SharedPreferences p1b95 = context.getSharedPreferences("p1p95", 0);
		int p2b95 = p1b95.getInt("p1p95", 0);
		views.setTextViewText(R.id.b95, String.valueOf(p2b95));
		appWidgetManager.updateAppWidget(appWidgetIds, views);
		
		
		}
		}



My widget is to show the fuel prices on the homescreen


Everything is ok with the app but my problem is that I cant show the prices in the textviews they are empty

I just want to show price for one type of fuel in the widget

Please can someone help me and let me know what's wrong with my code

推荐答案

这篇关于使用sharedpreferences在另一个活动中使用edittext在android小部件中设置textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆