从标签中获取片段中的adpter位置

时间:2015-07-14 13:07:02

标签: java android android-fragments

我在位置0有一个静态标签,它从服务器返回数据。我希望通过位置1,2,3等服务器的位置返回数据。我将发布代码,但我无法确定此代码中的错误。

片段xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="app.amplitudenet.com.materialapp.Fragmentos.AtendimentoHistorico">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/IDProcolo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Numero do Protocolo"
        android:layout_marginLeft="12dp"
        android:textSize="24dp"
        android:layout_marginTop="12dp"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Histórico: "
        android:id="@+id/textoHistorico"
        android:textStyle="bold"
        android:layout_below="@+id/IDProcolo1"
        android:layout_alignBottom="@+id/Historico"
        android:layout_alignLeft="@+id/IDProcolo1"
        android:layout_alignStart="@+id/IDProcolo1" />

    <TextView
        android:id="@+id/Historico"
        android:text="Número do Histórico"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666"
        android:layout_below="@+id/IDProcolo1"
        android:layout_toRightOf="@+id/textoHistorico"
        android:layout_toEndOf="@+id/textoHistorico" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Responsável: "
        android:layout_marginTop="5dp"
        android:id="@+id/textoDataCadastro"
        android:textStyle="bold"
        android:layout_below="@+id/Historico"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="8dp"
        />

    <TextView
        android:id="@+id/FuncionarioHistorico"
        android:text="Thiago Belão"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666"
        android:layout_alignTop="@+id/textoDataCadastro"
        android:layout_toRightOf="@+id/textoDataCadastro"
        android:layout_toEndOf="@+id/textoDataCadastro" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data de Solução: "
        android:layout_marginTop="5dp"
        android:id="@+id/textoDataSolucaoHistorico"
        android:textStyle="bold"
        android:layout_below="@+id/textoDataCadastro"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="8dp"
        />

    <TextView
        android:id="@+id/DataSolucaoHistorico"
        android:text="19/11/2015"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666"
        android:layout_alignTop="@+id/textoDataSolucaoHistorico"
        android:layout_toRightOf="@+id/textoDataSolucaoHistorico"
        android:layout_toEndOf="@+id/textoDataSolucaoHistorico" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Etapa Finalizada : "
        android:layout_marginTop="5dp"
        android:id="@+id/textoEtapa"
        android:textStyle="bold"
        android:layout_below="@+id/textoDataSolucaoHistorico"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="8dp"
        />

    <TextView
        android:id="@+id/Etapa"
        android:text="Sim / Não"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666"
        android:layout_alignTop="@+id/textoEtapa"
        android:layout_toRightOf="@+id/textoEtapa"
        android:layout_toEndOf="@+id/textoEtapa" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Solicitação: "
        android:layout_marginTop="5dp"
        android:layout_marginLeft="12dp"
        android:id="@+id/textoSolicitacaoHistorico"
        android:layout_below="@+id/textoEtapa"
        android:textStyle="bold"
        />

    <TextView
        android:id="@+id/textoHistoricoAtendimento"
        android:text="Texto da Solicitação"
        android:layout_width="fill_parent"
        android:layout_marginTop="8dp"
        android:layout_height="fill_parent"
        android:textColor="#666"
        android:textSize="14dp"
        android:layout_marginLeft="12dp"
        android:layout_below="@+id/textoSolicitacaoHistorico"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/IntPosicao"
        android:text="1"
        android:visibility="gone" />


</RelativeLayout>

片段code.java

public class AtendiementoOcorrencia extends ActionBarActivity {
private Toolbar toolbar;
ViewPager pager;
AtendimentoTabsAdpter adapter;
SlidingTabLayout tabs;
ArrayList<String>Titles;

public static final  int CONNECTION_TIME = 1000 *15;
public static final  String SERVIDOR = "http://www.creativeriopreto.com.br/app/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_atendiemento_ocorrencia);
    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Bundle id = getIntent().getExtras();
    String get = id.getString("id");
    String total = id.getString("total");
    String codigo =  id.getString("codigo");
    int abas =  Integer.parseInt(total);
    Titles = Abas(abas+1);
    int numerodeabas = abas + 1;
    adapter = new AtendimentoTabsAdpter(getSupportFragmentManager(),Titles, numerodeabas);
    pager = (ViewPager) findViewById(R.id.pagerAtendimento);
    pager.setAdapter(adapter);
    tabs = (SlidingTabLayout) findViewById(R.id.tabsAtendimento);
    tabs.setDistributeEvenly(true);
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.selector);
        }

    });
    tabs.setViewPager(pager);
    tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            TextView texto = (TextView) findViewById(R.id.IntPosicao);
            texto.setText(String.valueOf(position));
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


}
public ArrayList<String> Abas(int numeroAbas) {
    int i;
    ArrayList<String> titulos  = new ArrayList<>();
    titulos.add("Detalhes");
    for (i = 1; i < numeroAbas; i++)
    {
        titulos.add("Hist. Nº" + i);
    }
    return titulos;

}

}

activity.java

public class AtendimentoHistorico extends Fragment {
Historico item;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View tela =  inflater.inflate(R.layout.fragment_atendimento_historico, container, false);
    Bundle id = getActivity().getIntent().getExtras();
    String codigo =  id.getString("codigo");
    int codid = Integer.parseInt(codigo);
    TextView posicao = (TextView) tela.findViewById(R.id.IntPosicao);
    String coisocoisado = posicao.getText().toString();
    int pos;
    pos = Integer.parseInt(coisocoisado);
    item =  new Historico(codid,pos);
    Autenticado(item);
    return tela;
}
public void Autenticado(Historico historico) {
    ServerRequests server = new ServerRequests(getActivity());
    server.getDadosHistoricoBackground(historico, new GetHistoricoCallBack() {
        @Override
        public void done(Historico returnedHistorico) {
            if (returnedHistorico == null)
            {
                Erro();
            }
            else
            {
                Bundle id = getActivity().getIntent().getExtras();
                String get = id.getString("id");
                TextView momo = (TextView) getActivity().findViewById(R.id.IDProcolo1);
                momo.setText("Protocolo Nº "+get);
                TextView momo1 = (TextView) getActivity().findViewById(R.id.FuncionarioHistorico);
                if (returnedHistorico.Setor == "null")
                {
                    momo1.setText(""+returnedHistorico.Funcionario);
                }
                else
                {
                    momo1.setText(""+returnedHistorico.Setor);
                }
                TextView momo2 = (TextView) getActivity().findViewById(R.id.DataSolucaoHistorico);
                momo2.setText(""+returnedHistorico.Data);
                TextView momo3 = (TextView) getActivity().findViewById(R.id.Etapa);
                momo3.setText(""+returnedHistorico.Etapa);
                TextView momo4 = (TextView) getActivity().findViewById(R.id.textoHistoricoAtendimento);
                momo4.setText(""+returnedHistorico.Solucao);
                TextView momo5 = (TextView) getActivity().findViewById(R.id.Historico);
                momo5.setText(""+returnedHistorico.Sequencia);
                TextView momo6 = (TextView) getActivity().findViewById(R.id.txtFinalizado);
                momo6.setText(""+returnedHistorico.Finalizado);
            }

        }
    });
}


private void Erro() {
    AlertDialog.Builder alerta = new AlertDialog.Builder(getActivity());
    alerta.setMessage("Erro ao Carregar Histórico de Atendimento");
    alerta.setPositiveButton("OK", null);
    alerta.show();
}

}

adpter代码

public class AtendimentoTabsAdpter extends FragmentStatePagerAdapter {
ArrayList<String> Titles; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();


// Build a Constructor and assign the passed Values to appropriate values in the class
public AtendimentoTabsAdpter(FragmentManager fm,ArrayList<String> mTitles, int mNumbOfTabsumb) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        AtendimentoDetalhado tab1 = new AtendimentoDetalhado();
        return tab1;
    }

     else          // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        AtendimentoHistorico tab2 = new AtendimentoHistorico();
        return tab2;
    }



}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {

    return Titles.get(position);
}

// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return NumbOfTabs;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    registeredFragments.remove(position);
    super.destroyItem(container, position, object);
}

public Fragment getRegisteredFragment(int position) {
    return registeredFragments.get(position);
}

1 个答案:

答案 0 :(得分:0)

我更改了adpater并制作了一个列表,更新了我的rest api中的方法,现在我有两个选项卡和我想要的所有数据