第二个活动无法启动适配器类。

huangapple go评论81阅读模式
英文:

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 &#39;protected&#39;
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&lt;NBA_Adapter.NBAViewHolder&gt; 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(&quot;id&quot;, 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 &#39;protected&#39;
Log.v(&quot;cs100&quot;, &quot;I exist!&quot;);
team_id = getIntent().getIntExtra(&quot;id&quot;,0);
Log.v(&quot;cs100&quot;, &quot;&quot; + 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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.example.nba&quot;&gt;
&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.SecondMain&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

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&lt;Player_Adapter.PlayerViewHolder&gt; implements Filterable {
private int team_id;
public class myclass extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
team_id = getIntent().getIntExtra(&quot;id&quot;,0);
recyclerView.setAdapter(adapter);
Log.v(&quot;Player&quot;,&quot;&quot;+ team_id); //Not displaying statement which indicates that &quot;Player_Adapter&quot; 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 &quot;fullName&quot;
intent.putExtra(&quot;id&quot;, 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.

huangapple
  • 本文由 发表于 2020年9月6日 14:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定