在我的应用程序中我有两种语言(英语,阿拉伯语)。如果用户选择任何语言应用程序更改该语言。但我的应用程序内容是从json api.how检索数据来更改它?
package com.blog.navdrawer;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.blog.navdrawer.JSONParser;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Locale;
import static android.content.Context.MODE_PRIVATE;
import static com.blog.navdrawer.R.string.language;
/**
* Created by ewall-07 on 3/12/16.
*/
public class DemoFragone extends Fragment {
private JSONParser jsonparser = new JSONParser();
private TextView tv,tv1;
String a,b,c;
private JSONObject jsonobject = null;
String languagesave;
private Locale myLocale;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// View v = inflater.inflate(R.layout.fragment_fragone,container,false);
View v = inflater.inflate(R.layout.fragment_fragone,container,false);
tv = (TextView) v.findViewById(R.id.text_tv);
tv1 = (TextView) v.findViewById(R.id.text_tv1);
new retrievedata().execute();
language();
/*
//it help us to change the font style...
Typeface type = Typeface.createFromAsset(getActivity().getAssets(),"DroidSerif-Regular.ttf");
TextView tv1 = (TextView) v.findViewById(R.id.text_tv1);
tv1.setTypeface(type);*/
return v;
}
private void language() {
String langPref = "Language";
SharedPreferences sharedpreferences = this.getActivity().getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.language),languagesave);
editor.commit();
}
class retrievedata extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
if(languagesave == "en") {
jsonobject = jsonparser.makeHttpRequest("http://www.json-generator.com/api/json/get/cegNjwgBTS?indent=2");
Log.d("app data", jsonobject.toString());
try {
a = jsonobject.getString("name");
b = jsonobject.getString("data");
c = jsonobject.getString("image_url");
} catch (JSONException e) {
e.printStackTrace();
}
}
else if(languagesave == "ar"){
jsonobject = jsonparser.makeHttpRequest("http://www.json-generator.com/api/json/get/cegNjwgBTS?indent=2");
Log.d("app data", jsonobject.toString());
try {
a = jsonobject.getString("name_ar");
b = jsonobject.getString("data_ar");
c = jsonobject.getString("image_url");
} catch (JSONException e) {
e.printStackTrace();
}
}
return a;
}
protected void onPostExecute(String a){
tv.setText(a);
tv1.setText(b);
ImageView image = (ImageView) getActivity().findViewById(R.id.image_one);
Picasso.with(getActivity()).load(c).error(R.drawable.error).placeholder(R.drawable.progress_aniamtion).noFade().into(image);
}
}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (myLocale != null){
newConfig.locale = myLocale;
Locale.setDefault(myLocale);
getActivity().getResources().updateConfiguration(newConfig, getActivity().getResources().getDisplayMetrics());
}
} }
MainActivity :
package com.blog.navdrawer.activity;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.blog.navdrawer.R;
import com.blog.navdrawer.adapter.CountryLanguageAdapter;
public class CountryLanguageActivity extends AppCompatActivity implements OnClickListener {
private Button mSaveButton;
private ListView mCountryList, mLanguageList;
private ArrayAdapter<String> mLanguageAdapter, mCountryAdaptor;
String languagesave;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addActionbar();
setContentView(R.layout.activity_country_language);
initialization();
}
private void addActionbar() {
//noinspection ConstantConditions
getSupportActionBar().setCustomView(R.layout.actionbar);
TextView TvTitle = (TextView) findViewById(getResources()
.getIdentifier("action_bar_title", "id", getPackageName()));
}
private void initialization() {
//ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f79f04")));
//bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//bar.setCustomView(R.layout.actionbar);
mCountryList = (ListView) findViewById(R.id.list_country);
mLanguageList = (ListView) findViewById(R.id.list_language);
mSaveButton = (Button) findViewById(R.id.button_save_continue);
String[] country = { "Saudi Arabia - Riyadh", "Kuwait", "Qatar", "Bahrain" };
String[] language = { "English", "Arabic"};
int[] countryIcon = { R.drawable.ic_saudi, R.drawable.ic_kuwait , R.drawable.ic_qatar, R.drawable.ic_bahrain};
int[] languageIcon = { R.drawable.ic_english, R.drawable.ic_arabic };
int countryList = 1;
int languageList = 2;
String[] languagesave = language;
//Create a customer adapter for the language list
mLanguageAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, country);
mCountryList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mCountryList.setAdapter(new CountryLanguageAdapter(CountryLanguageActivity.this, country, countryIcon, countryList));
//Create a customer adapter for the country list
mCountryAdaptor = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, language);
mLanguageList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mLanguageList.setAdapter(new CountryLanguageAdapter(CountryLanguageActivity.this, language, languageIcon, languageList));
ViewTreeObserver listVTO = mCountryList.getViewTreeObserver();
listVTO.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mCountryList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
resizeListView(mCountryList);
}
});
ViewTreeObserver listVTO1 = mLanguageList.getViewTreeObserver();
listVTO1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mLanguageList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
resizeListView(mLanguageList);
}
});
mSaveButton.setOnClickListener(this);
SharedPreferences sharedpreferences = CountryLanguageActivity.this.getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.language),languagesave);
editor.commit();
}
//onclick even for the save and continue button
public void onClick(View v ) {
//ViewDialog alert = new ViewDialog();
//alert.showDialog(ListViewMultipleSelectionActivity.this, "Please select the country");
Intent intent = new Intent(CountryLanguageActivity.this,LeftRightNavigationActivity.class);
SharedPreferences sharedpreferences = CountryLanguageActivity.this.getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.language),languagesave);
editor.commit();
startActivity(intent);
}
// Dynamically set the height for the items in list view
private void resizeListView(ListView listView) {
ListAdapter adapter = listView.getAdapter();
int count = adapter.getCount();
int itemsHeight = 0;
// Your views have the same layout, so all of them have
// the same height
View oneChild = listView.getChildAt(0);
if( oneChild == null)
return;
itemsHeight = oneChild.getHeight();
// Resize your list view
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)listView.getLayoutParams();
params.height = itemsHeight * count;
listView.setLayoutParams(params);
}
}
答案 0 :(得分:0)
查看我的代码:
Private void changeLanguage (final int position) {
final SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
if (position == 1) {
final Locale myLocale = new Locale("ar");
final Resources res = getResources();
final DisplayMetrics dm = res.getDisplayMetrics();
final Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
editor.putString("KEY","ar");
} else {
final Locale myLocale = new Locale("en");
final Resources res = getResources();
final DisplayMetrics dm = res.getDisplayMetrics();
final Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
editor.putString("KEY","en");
}
editor.apply();
}
将以下代码放在Activity on-site
中final SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
languagesave = sharedPreferences.getString("KEY", "en");
我已更新帖子。
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
changeLanguage(position);
final Intent refresh = new Intent(MainActivity.this, MainActivity.class);
startActivity(refresh);
finish();
}
});