使用意图从适配器启动另一个类?

时间:2019-05-22 10:01:57

标签: android sqlite android-layout android-intent recycler-adapter

我想使用Intent并想移到另一个viewpager类,而我正在使用Intent来做到这一点

    private void gotInvoiceClass(String fname) {
       Intent goToUpdate = new Intent (mContext,Invoices.class);
       goToUpdate.putExtrka("FIRSTNAME", fname);
       mContext.startActivity(goToUpdate);
  }

我知道上面的方法是用来从类转移到活动的;为什么在从viewpager类使用意图到另一个viewpager类的情况下无法使用此方法。 这是我完整的视图寻呼机回收器适配器类

public class ClientRecyclerAdapter extends RecyclerView.Adapter<ClientRecyclerAdapter.ClientViewHolder> {

private List<GetSetClientsDetail> listOrders;
Context mContext;
RecyclerView mRecyclerView;
View itemView;

public ClientRecyclerAdapter(List<GetSetClientsDetail> listOrders, RecyclerView recyclerView) {
    this.listOrders =  listOrders;
    mRecyclerView = recyclerView;
}

@Override
public ClientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    mContext = parent.getContext();
    itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.items_clients_table, parent, false);

    return new ClientViewHolder(itemView);
}


public class ClientViewHolder extends RecyclerView.ViewHolder {

    public TextView lblName;
    public TextView lblRoute;
    public TextView lblType;
    public TextView lblCName;
    public TextView lblContact;
    RelativeLayout clientitemslayout;


    public ClientViewHolder(View view) {
        super(view);


        lblName =  view.findViewById(R.id.lblBName);
        lblRoute =  view.findViewById(R.id.lblRoute);
        lblType =  view.findViewById(R.id.lblType);
        lblCName = view.findViewById(R.id.lblCName);
        lblContact =  view.findViewById(R.id.lblContact);
        clientitemslayout = view.findViewById(R.id.listclientsitems);
    }
}

@Override
public void onBindViewHolder(ClientViewHolder holder, final int position) {

    holder.lblName.setText(listOrders.get(position).getFNAME());
    holder.lblRoute.setText(listOrders.get(position).getROUTE());
    holder.lblType.setText(listOrders.get(position).CNIC);
    holder.lblCName.setText(listOrders.get(position).getLNAME());
    holder.lblContact.setText(listOrders.get(position).getCONTACT());
    holder.clientitemslayout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            displayingAlertDialog(position);
            return false;
        }
    });

}

@Override
public int getItemCount() {
    Log.v(ClientRecyclerAdapter.class.getSimpleName(), "" + listOrders.size());
    return listOrders.size();
}

private void displayingAlertDialog(final int position) {
    final GetSetClientsDetail getSetClientsDetail= new GetSetClientsDetail();
    AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
    builder.setTitle("Choose Option");
    builder.setMessage("Add Client Invoice?");
    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            gotInvoiceClass(listOrders.get(position).getFNAME());
            dialog.cancel();

        }
    });
    builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //go to Remove Item
            DatabaseHelper dbHelper = new DatabaseHelper(mContext);
           dbHelper.deletePersonRecord(listOrders.get(position).getFNAME(), mContext);
            listOrders.remove( position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, listOrders.size());
            notifyDataSetChanged();
            dialog.cancel();

        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert11 = builder.create();
    alert11.show();
}


private void gotInvoiceClass(String fname) {
    Intent goToUpdate = new Intent (mContext,Invoices.class);
    goToUpdate.putExtra("FIRSTNAME", fname);
    mContext.startActivity(goToUpdate);
}
}

我想做的是,当我单击在适配器类中实现的对话框时,移到另一个视图寻呼机类。

谢谢

这是我的错误日志

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.dspak.erpmini/com.dynamicsolution.pakistan.Invoices}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
    at android.app.Activity.startActivityForResult(Activity.java:3351)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
    at android.app.Activity.startActivityForResult(Activity.java:3312)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
    at android.app.Activity.startActivity(Activity.java:3522)
    at android.app.Activity.startActivity(Activity.java:3490)
    at com.dynamicsolution.pakistan.ClientRecyclerAdapter.gotupdateuserActivity(ClientRecyclerAdapter.java:133)
    at com.dynamicsolution.pakistan.ClientRecyclerAdapter.access$200(ClientRecyclerAdapter.java:21)
    at com.dynamicsolution.pakistan.ClientRecyclerAdapter$2.onClick(ClientRecyclerAdapter.java:99)

这是我的客户班

    public class Clients extends Fragment {
    RecyclerView recyclerViewClients;
    Button btnAll;
    SearchView searchViewclient;
    ClientRecyclerAdapter clientRecyclerAdapter;
    List<GetSetClientsDetail> listclients;
    DatabaseHelper databaseHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.clients, container, false);

        btnAll=view.findViewById(R.id.searchallclients);
        recyclerViewClients=view.findViewById(R.id.recyclerviewallclients);
        searchViewclient=view.findViewById(R.id.searchclient);
        listclients = new ArrayList<>();

        clientRecyclerAdapter = new ClientRecyclerAdapter(listclients,recyclerViewClients);
        recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
        recyclerViewClients.setItemAnimator(new DefaultItemAnimator()); //multi copy paste!
        recyclerViewClients.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerViewClients.setHasFixedSize(true);
        recyclerViewClients.setAdapter(clientRecyclerAdapter);

        databaseHelper = new DatabaseHelper(getActivity());

        searchViewclient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchSQliteClientData();
            }
        });

        btnAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getClientFromSqlite();
            }
        });

        return view;
    }

    @SuppressLint("StaticFieldLeak")
    private void SearchSQliteClientData() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                listclients.clear();
                listclients.addAll(databaseHelper.SearchClientsData("A"));
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                clientRecyclerAdapter.notifyDataSetChanged();
            }
        }.execute();
    }


    @SuppressLint("StaticFieldLeak")
    private void getClientFromSqlite() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                listclients.clear();
                listclients.addAll(databaseHelper.getAllClientsData());
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                clientRecyclerAdapter.notifyDataSetChanged();
            }
        }.execute();
    }
}

这是我包含viewpagers的活动。

 public class MainFragment extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("Dynamic ERPMini");

        TabLayout tabLayout = findViewById(R.id.tab_layout);


        tabLayout.addTab(tabLayout.newTab().setText("Clients"));
        tabLayout.addTab(tabLayout.newTab().setText("Products"));
        tabLayout.addTab(tabLayout.newTab().setText("Invoices"));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = findViewById(R.id.pager);
        final PagerAdapter adapter = new PagersAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

不能使用intent打开片段类。

您可以做的是在selectTab中创建一个MainFragment函数,然后从gotInvoiceClass函数中调用它。

MainFragment

public void selectTab(int position, String fname) {
        this.fname = fname;
        viewPager.setCurrentItem(position);
    }

    public String getMyData() {
        return fname;
    }

修改gotInvoiceClass如下:

private void gotInvoiceClass(String fname) {
        MainFragment main = (MainFragment) mContext;
        main.selectTab(2, fname);
    }

Invoices类中,您可以从fname获得MainFragment

发票

MainFragment activity = (MainFragment) getActivity();
String name = activity.getMyData();
EdtName.setText(name);