英文:
Second Activity Won't Start Adapter class
问题
以下是您提供的代码的翻译部分:
// 主活动
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) {
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 适配器
public class NBA_Adapter extends RecyclerView.Adapter<NBA_Adapter.NBAViewHolder> implements Filterable {
public static class NBAViewHolder extends RecyclerView.ViewHolder {
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);
}
});
}
}
}
// 第二个主活动
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) {
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 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>
请注意,这只是您提供代码的翻译部分。如果您需要更多的帮助或解释,请随时提问。
英文:
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. To do this, I have constructed my main activity, which will launch the "NBA_Adapter" in the OnCreate method as shown below:
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:
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). Since "MainActivity" is almost identical to "SecondMain" I assumed that similar to how "MainActivity" launches "NBA_Adapter" so will "SecondMain" launch "Player_Adapter". Player_Adapter is almost identical to "NBA_Adapter" but it is not launching at all. This is why I suspect that the problem is how I described "SecondMain" in the Android Manifest. 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:
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();
}
}
答案1
得分: 0
在类 myclass
内部,您肯定已经设置了适配器,但看起来您遗漏了将 LayoutManager
设置为 RecyclerView
的代码。
recyclerView.setLayoutManager(new LinearLayoutManager(this/*在这里添加上下文*/));
另一个建议是,您应该将活动单独保留,而不是将其嵌套在适配器内部。
英文:
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
.
recyclerView.setLayoutManager(new LinearLayoutManager(this/*Context here*/));
Another suggestion is that you should keep activity separately and not nest it inside a adapter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论