我尝试将MainActivity listview.itemclick
转到SecondActivity
,其中也是listview。但是,我点击的MainActivity listview
个项目中的任何项目,它始终在listview
中打开相同的SecondActivity
。当我点击listview
处的listview.itemclick
MainActivity
点击
上的MainActivity列表视图listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent;
Teams entry = (Teams) parent.getItemAtPosition(position);
intent = new Intent(TeamMenu.this, TeamSelectMenu.class);
intent.putExtra("team_id", entry.getTeamName());
startActivity(intent);
}
当我试图在SecondActivity
上抓住它时:
Intent intent = getIntent();
intent.getStringExtra("team_id");
它仍然一直打开listview
。
sharedPreferences
我保存了SecondActivity
private void savePlayerListToSharedpreference(ArrayList<Teams> playerList) {
String jsonPlayer = gson.toJson(playerList);
sharedPreference.savePlayersList(jsonPlayer);
}
我在sharedPreferences
SecondActivity
private void getPlayerListFromSharedPreference() {
String jsonPlayer = sharedPreference.getPlayersList();
Type type = new TypeToken<List<Teams>>(){}.getType();
playerList = gson.fromJson(jsonPlayer, type);
if (playerList == null) {
playerList = new ArrayList<>();
}
}
我在listview
SecondActivity
public void onGettingDataListener() {
if (playerList.size() == 0) {
Toast.makeText(TeamSelectMenu.this, "No data in sharedPreferences", Toast.LENGTH_SHORT).show();
} else {
getPlayerListFromSharedPreference();
PlayersListViewAdapter adapter = new PlayersListViewAdapter(TeamSelectMenu.this, R.layout.team_list, playerList);
listView.setAdapter(adapter);
}
}
SharedPreferences
public class TeamSharedPreferences {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
// Context
private Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "pref";
private static final String TEAMS = "teams";
private static final String PLAYERS = "players";
public TeamSharedPreferences(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void saveTeamsList(String scoreString) {
editor.putString(TEAMS, scoreString);
editor.commit();
}
public String getTeamsList() {
return pref.getString(TEAMS, "");
}
public void savePlayersList(String playerString) {
editor.putString(PLAYERS, playerString);
editor.commit();
}
public String getPlayersList() {
return pref.getString(PLAYERS, "");
}
}
public class TeamListViewAdapter extends ArrayAdapter<Teams> {
private Activity activity;
public TeamListViewAdapter(Activity activity, int resource, List<Teams> teams) {
super(activity, resource, teams);
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.team_list, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
Teams teams = getItem(position);
holder.teamName.setText(teams.getTeamName());
return convertView;
}
private static class ViewHolder {
private TextView teamName;
//private TextView playerNames;
public ViewHolder(View v) {
teamName = (TextView) v.findViewById(R.id.teamList);
}
}}
ArrayAdapter
strictNullChecks