使用 Intent 从适配器启动另一个类?
Posted
技术标签:
【中文标题】使用 Intent 从适配器启动另一个类?【英文标题】:Start Another Class From adapter Using Intent? 【发布时间】:2019-10-08 18:57:42 【问题描述】:我想使用 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();
这是包含查看器的我的活动..
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)
);
【问题讨论】:
让我们continue this discussion in chat。 【参考方案1】:你不能使用intent
打开一个fragment类。
您可以做的是在MainFragment
中创建一个selectTab
函数,并从gotInvoiceClass
函数中调用它。
主片段
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
类中,你可以从MainFragment
得到fname
。
发票
MainFragment activity = (MainFragment) getActivity();
String name = activity.getMyData();
EdtName.setText(name);
【讨论】:
我的问题由此解决;感谢您的帮助:)以上是关于使用 Intent 从适配器启动另一个类?的主要内容,如果未能解决你的问题,请参考以下文章