如何从数组中替换片段

时间:2014-01-10 10:05:21

标签: java android arrays android-fragments

我不明白..我正在尝试使用transaction.replace使用arrayAdapter将一个片段替换为另一个片段。数组中的值是com.example.package.fragment-地址。但我无法弄清楚如何实际替换片段。

如果我尝试将以下代码用于以下行:

FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_container, Fragment.instantiate(this, getResources().getStringArray(R.array.adressLista[position])));
        transaction.commit(); 

我得到“表达式的类型必须是数组类型,但它解析为int”。但是,如果我使用相同的R.array替换TextView中的文本,如下所示:

TextView infoText = (TextView) getActivity().findViewById(R.id.fragmentTextView);
        infoText.setText(getResources().getStringArray(R.array.adressLista)[position]);

然后显示的文字是com.example。地址。如何更换碎片?

代码: VisaFragment(错误发生在第11行从底部):

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class VisaFragment extends Fragment {

    static final String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState){ 

        if (savedInstanceState != null){
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        return inflater.inflate(R.layout.om_sida, container, false);    
    }

    @Override
    public void onStart(){
        super.onStart();
System.out.println("VisaFragment onStart");     

        Bundle argument = getArguments();
        if (argument != null){
            visaFragmentArgument(argument.getInt(ARG_POSITION));
        }else if (mCurrentPosition != -1){
            visaFragmentArgument(mCurrentPosition);
        }
    }

    public void visaFragmentArgument(int position) {
System.out.println("VisaFragment VisaFragmentArgument");

        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_container, Fragment.instantiate(this, getResources().getStringArray(R.array.adressLista[position])));
        transaction.commit();
        }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

MenyFragment:

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class MenyFragment extends ListFragment{

        onMenyKnappClickListener mCallback;
        public interface onMenyKnappClickListener {
            public void onMenyKnappVald (int position);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

    System.out.println("MenyFragment onCreate");
            int layout = android.R.layout.simple_list_item_1;
            setListAdapter (new ArrayAdapter<String>(getActivity(), layout, getResources().getStringArray(R.array.menyLista)));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
    System.out.println("MenyFragment onListItemClick");     
            mCallback.onMenyKnappVald(position);
            getListView().setItemChecked(position, true);
        }

        @Override
        public void onStart() {
            super.onStart();
            System.out.println("MenyFragment onStart");
            if (getFragmentManager().findFragmentById(R.id.fragment_container_stor) !=null){
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                }
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    System.out.println("MenyFragment onAttach");
            try {
                mCallback =(onMenyKnappClickListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " Måste implementera onMenyKnappClickListener");
            }
        }
}

MainActivity:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity implements MenyFragment.onMenyKnappClickListener{

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
System.out.println("MainActivity onCreate");
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragment_container_liten) != null){
            if (savedInstanceState != null){
                return;
            }

            MenyFragment menyFragment = new MenyFragment();
            menyFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, menyFragment).commit();
        }
    }

    public void onMenyKnappVald (int position){
System.out.println("MainActivity onMenyKnappVald");

    VisaFragment visaFrag = (VisaFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container);

    if (visaFrag != null) {
        visaFrag.visaFragmentArgument(position);

    }else{
        VisaFragment nyttFragment = new VisaFragment();
        Bundle argument = new Bundle ();

        argument.putInt(VisaFragment.ARG_POSITION, position);
        nyttFragment.setArguments(argument);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, nyttFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


    }

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    android:orientation="horizontal"    
    android:background="@color/menyBGColor"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.example.exempelfragmentsgrund.MenyFragment"
              android:id="@+id/meny_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.exempelfragmentsgrund.VisaFragment"
              android:id="@+id/fragment_container"
              android:layout_weight="3"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

你看到你有

getResources().getStringArray(R.array.adressLista[position]) 

在第一个然后在TextView的东西中这个

getResources().getStringArray(R.array.adressLista)[position]

因此,您只需将[position]方法移出getStringArray

getResources().getStringArray(R.array.adressLista[position])给出错误,因为getStringArray接受int参数,并且您尝试使用R.array.adressLista,就像它是一个数组,但它是整数,它引用了values.xml中的数组。

答案 1 :(得分:0)

Fragment.instantiate(this, getResources().getStringArray(R.array.adressLista[position])));

getResources().getStringArray()返回int [],您的上述代码段将返回int。 setText有两个变体,因此它可以工作:setText(int) setText(String)