我如何在每个listview点击共享偏好中创建新的SharedPreferences?

时间:2018-06-05 15:21:36

标签: java android android-sharedpreferences

我尝试将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

0 个答案:

没有答案