Я застрял в ситуации, когда я использую пользовательский счетчик, в котором я хочу переключатель и текст.В этом я хочу, чтобы была выбрана только одна радиокнопка, но при переходе на другую радиокнопку остается выбранной последняя радиокнопка.
Вот мой основной XML-файл
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
>
<Spinner
android:id="@+id/spinner_lang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</LinearLayout>
Файл элементадля Custom SPinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="2"
android:padding="10dp"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/radio_"
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_lang_box"
android:layout_weight="1.5"
android:layout_width="0dp"
android:text="Hindi"
android:textSize="21sp"
android:layout_height="wrap_content" />
</LinearLayout>
Ну, это мой файл MainActivity, в котором я установил адаптер.
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String[] country = { "Select Language","French", "Hindi", "Japanese" };
private Spinner spinnerLanguage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.spinner_lang);
spin.setOnItemSelectedListener(this);
CustomArrayAdapter customAdapter=new CustomArrayAdapter(getApplicationContext(),country);
spin.setAdapter(customAdapter);
//Creating the ArrayAdapter instance having the country list
/*ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spinnerLanguage.setAdapter(aa);*/
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
if (pos == 1) {
Locale myLocale = new Locale("fr");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
} else if (pos == 2) {
Toast.makeText(this, "this is from 2", Toast.LENGTH_SHORT).show();
Locale myLocale = new Locale("hi");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
} else if (pos == 3) {
Toast.makeText(this, "this is from 3", Toast.LENGTH_SHORT).show();
Locale myLocale = new Locale("ja");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
И это мой файл адаптера .. пакет com.rightarrows.language_change;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by tapes on 5/21/2018.
*/
public class CustomArrayAdapter extends BaseAdapter {
Context context;
String[] countryNames;
LayoutInflater inflter;
public CustomArrayAdapter(Context applicationContext, String[] countryNames) {
this.context = applicationContext;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return countryNames.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.dialog_item_list, null);
TextView names = (TextView) view.findViewById(R.id.text_lang_box);
names.setText(countryNames[i]);
return view;
}
}