Я печатаю некоторые строковые данные в listView, где я получаю массив из массива string из strings.xml. Для того, чтобы сделать вас более ясным Даю пример:getResources(). GetStringArray (R.array.Passing_own_variable);
strings.xml
<string-array name="vehicle">
<item>car</item>
<item>bur</item>
<item>van</item>
</string-array>
<string-array name="food">
<item>cake</item>
<item>donuts</item>
<item>Ice-cream</item>
</string-array>
<string-array name="fruits">
<item>apple</item>
<item>banana</item>
<item>grapes</item>
</string-array>
Now, Something In NavigationItem like:
vehicle
food
fruits
тогда, когда я выбираю автомобиль я начинаю fargment, как показано ниже, и
case R.id.vehicle:
bundle.putString("key_variable","vehicle");
msgListActivtiy loveweb=new msgListActivtiy();
loveweb.setArguments(bundle);
fragmentTransaction=fragmentManager.beginTransaction().replace(R.id.mainContainer,loveweb);
fragmentTransaction.commit();
break;
case R.id.foods:
bundle.putString("key_variable","food");
//othercodes
break;
case R.id.fruits:
bundle.putString("key_variable","fruits");
//othercodes
break;
Теперь, это открывает a. msgListActivity
Что-то Likethis
public class msgListActivtiy extends Fragment {
String localUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle=this.getArguments();
localUrl=bundle.getString("key_variable","defaultvalue");
View rootView=inflater.inflate(R.layout.lv,container,false);
ListView listView= (ListView) rootView.findViewById(R.id.listview);
//Now, below instead of giving car I need to set the localvariable that I
//have declared above.
//Something I want to pass like
//String[] res="getActivity().getResources().getStringArray(R.array."+localvariable+");"; I tried this and give me error like found string instead of String[]
//how can we achieve that? If that can't be done then how do we handle these kinds of situations??
String[] res=getActivity().getResources().getStringArray(R.array.need_variable_from_bundle_above);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,res);
listView.setAdapter(adapter);
return rootView;
@ Exigente05 Я сказал в комментариях код. Сам вопрос - вопрос. Как мы можем передать own_variable в R.array? –