[英]Second Activity Won't Start Adapter class
I am trying to create a recycle view list of NBA teams and when each team is clicked it will display a recycler view list of the NBA players in that particular team.我正在尝试创建 NBA 球队的回收视图列表,当单击每个球队时,它将显示该特定球队的 NBA 球员的回收视图列表。 To do this, I have constructed my main activity, which will launch the "NBA_Adapter" in the OnCreate method as shown below:为此,我构建了我的主要活动,它将在 OnCreate 方法中启动“NBA_Adapter”,如下所示:
package com.example.nba;
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
state state = new state();
private NBA_Adapter adapter;
private RecyclerView recyclerView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {//originally 'protected'
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
adapter = new NBA_Adapter(getApplicationContext());
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
}
}
NBA_Adapter: NBA_适配器:
package com.example.nba;
public class NBA_Adapter extends RecyclerView.Adapter<NBA_Adapter.NBAViewHolder> implements Filterable {
public static class NBAViewHolder extends RecyclerView.ViewHolder { //constructer for recyclerview adapter
public LinearLayout containerView;
public TextView textView;
NBAViewHolder(View view) {
super(view);
containerView = view.findViewById(R.id.nba_row);
textView = view.findViewById(R.id.nba_row_text_view);
containerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TEAMS current = (TEAMS) containerView.getTag();
Intent intent = new Intent(v.getContext(), SecondMain.class);
intent.putExtra("id", current.getId());
v.getContext().startActivity(intent);
}
});
}
}
}
Second Main:第二主线:
public class SecondMain extends AppCompatActivity implements SearchView.OnQueryTextListener {
private int team_id;
private Player_Adapter rapter;
private RecyclerView mrecyclerView;
private RecyclerView.LayoutManager layoutManager;
@Override
public void onCreate(Bundle savedInstanceState) {//originally 'protected'
Log.v("cs100", "I exist!");
team_id = getIntent().getIntExtra("id",0);
Log.v("cs100", "" + team_id);
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
mrecyclerView = findViewById(R.id.mrecycler_view);
rapter = new Player_Adapter(getApplicationContext());
mrecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
state.setstate(false);
mrecyclerView.setAdapter(rapter);
}
}
Android Manifest:安卓清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nba">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondMain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now I am able to launch the "SecondMain" class as evident of a Log.v statement printing(Meaning at this point the teams but not the players in the teams are displaying).现在我可以启动“SecondMain”类了,就像打印 Log.v 语句一样(意思是在这一点上显示的是球队而不是球队中的球员)。 Since "MainActivity" is almost identical to "SecondMain" I assumed that similar to how "MainActivity" launches "NBA_Adapter" so will "SecondMain" launch "Player_Adapter".由于“MainActivity”几乎与“SecondMain”相同,我假设类似于“MainActivity”启动“NBA_Adapter”的方式,“SecondMain”也会启动“Player_Adapter”。 Player_Adapter is almost identical to "NBA_Adapter" but it is not launching at all. Player_Adapter 几乎与“NBA_Adapter”相同,但它根本没有启动。 This is why I suspect that the problem is how I described "SecondMain" in the Android Manifest.这就是为什么我怀疑问题在于我如何在 Android 清单中描述“SecondMain”。 I only included the relevant parts for each class.我只包含了每个类的相关部分。 Any tips or links on this issue is appreciated, thanks!感谢有关此问题的任何提示或链接,谢谢!
EDIT: Included Player_Adapter class below:编辑:包括以下 Player_Adapter 类:
public class Player_Adapter extends RecyclerView.Adapter<Player_Adapter.PlayerViewHolder> implements Filterable {
private int team_id;
public class myclass extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
team_id = getIntent().getIntExtra("id",0);
recyclerView.setAdapter(adapter);
Log.v("Player",""+ team_id); //Not displaying statement which indicates that "Player_Adapter" is not being launched
}
}
public static class PlayerViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView textView;
PlayerViewHolder(View view) {
super(view);
containerView = view.findViewById(R.id.Player_List_row);
textView = view.findViewById(R.id.Player_List_row_text_view);
containerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Players current = (Players) containerView.getTag();
Intent intent = new Intent(v.getContext(), Compare_Stats.class);
//we get the "fullName"
intent.putExtra("id", current.getPlayer_id());
v.getContext().startActivity(intent);
}
});
}
}
@NonNull
@Override
public PlayerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_list, parent, false);
return new PlayerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PlayerViewHolder holder, int position) {
Players current = PlayersList.get(position);
holder.textView.setText(current.FullName());
holder.containerView.setTag(current);
}
@Override
public int getItemCount() {
return PlayersList.size();
}
}
Inside the class myclass
you are definitely setting the adapter , but it looks like you are missing the code that sets the LayoutManager
to RecyclerView
.在myclass
类中,您肯定是在设置适配器,但看起来您缺少将LayoutManager
设置为RecyclerView
的代码。
recyclerView.setLayoutManager(new LinearLayoutManager(this/*Context here*/));
Another suggestion is that you should keep activity separately and not nest it inside a adapter.另一个建议是您应该单独保留活动,而不是将其嵌套在适配器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.