second活动不会启动适配器类

vd2z7a6w  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(444)

我正在尝试创建一个nba球队的循环视图列表,当单击每个球队时,它将显示一个特定球队中nba球员的循环视图列表。为此,我构建了我的主活动,它将在oncreate方法中启动“nba\u 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\u适配器:

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);
                }
            });
        }

    }

}

第二条主线:

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清单:

<?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>

现在我可以启动“secondmain”类了,这可以从log.v语句打印中看出(这意味着此时显示的是团队,而不是团队中的玩家)。由于“mainactivity”和“secondmain”几乎相同,我假设“mainactivity”启动“nba\u adapter”的方式类似,“secondmain”也会启动“player\u adapter”。球员适配器几乎是相同的“nba适配器”,但它并没有推出在所有。这就是为什么我怀疑问题是我在android清单中如何描述“secondmain”。我只包括了每节课的相关部分。任何关于这个问题的提示或链接都将不胜感激,谢谢!
编辑:包含以下播放器适配器类:

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();
    }
}
56lgkhnf

56lgkhnf1#

在班里 myclass 您肯定是在设置适配器,但似乎缺少设置适配器的代码 LayoutManagerRecyclerView .

recyclerView.setLayoutManager(new LinearLayoutManager(this/*Context here*/));

另一个建议是,应该单独保存活动,而不是将其嵌套在适配器中。

相关问题