I want to know if it's possible to send several informations by intent from an Activity to a BaseAdapter.
Indeed, I have an application android, and I want to send the mail of the user to an BaseAdapter that contains a listView.
So is it possible ?
I try like this but I have an error : (I send in the onPostExecute() )
My Activity :
public class ParticipateActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapterTournament adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String NOMTOURNAMENT = "nomTournament";
static String NBPLAYERSMAX = "nbPlayersMax";
static String MAIL = "mail";
// To know the main user
public String mail;
// IDs for menu items
private static final int MAIN_MENU = Menu.FIRST;
//php server
private static final String LISTTOURN_URL = "http://10.0.0.35/BD_Projet/liste_tournament.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.activity_participate);
Intent i = getIntent();
// Get the result of mail
mail = i.getStringExtra("mail");
System.out.println(mail);
// Execute DownloadJSON AsyncTask
new DownloadLT().execute();
}
// DownloadJSON AsyncTask
private class DownloadLT extends AsyncTask<Void, Void, Void> {
// Allow to display an circle Bar
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(ParticipateActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON ListTournaments Michaël KOZA");
// Set progressdialog message
mProgressDialog.setMessage("Loading for tournaments...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
List<NameValuePair> args = new ArrayList<NameValuePair>();
args.add(new BasicNameValuePair("mail", mail));
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address (We return just tournaments where there are still places to play)
jsonobject = JSONParser.makeHttpRequest(LISTTOURN_URL, "POST", args);
// checking log for json response
Log.d("Register attempt", jsonobject.toString());
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("ListTournaments");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("nomTournament", jsonobject.getString("nomTournament"));
map.put("nbPlayersMax", jsonobject.getString("nbPlayersMax"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapterTournament(ParticipateActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
//intent
Intent i = new Intent(ParticipateActivity.this,ListViewAdapterTournament.class);
i.putExtra("mail", mail);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
And my ListView :
public class ListViewAdapterTournament extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public String mail, nomT;
public ListViewAdapterTournament(Context context, ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
}
/* Permet de savoir le nombre d'item à afficher dans le liste view */
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView nomTournament;
TextView nbPlayersMax;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.activity_list_view_adapter_tournament, parent, false);
// Get the position
resultp = data.get(position);
//receive the mail
Intent i = getIntent(); //error
// Get the result of mail
mail = i.getStringExtra("mail");
// Locate the TextViews in listview_item.xml
nomTournament = (TextView) itemView.findViewById(R.id.nomTournament);
nbPlayersMax = (TextView) itemView.findViewById(R.id.nbPlayersMax);
// Capture position and set results to the TextViews
nomTournament.setText(resultp.get(ParticipateActivity.NOMTOURNAMENT));
nbPlayersMax.setText(resultp.get(ParticipateActivity.NBPLAYERSMAX));
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(context, ChooseTeam.class);
mail = resultp.get(ParticipateActivity.MAIL);
System.out.println(mail);
intent.putExtra("mail", mail);
intent.putExtra("nomTournament", nomT);
// Click on item, on enregistre le joueur dans le tournament et on lui demande de choisir team
context.startActivity(intent);
}
});
return itemView;
}
}
Thank you very much for your help
Mickey74
To send an informations at another activity without open the other activity, you have to pass by the Shared Preferences.
To send the informations you have to do that :
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Mickey74");
prefsEditor.commit();
And to use this informations in another activity to late :
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
System.out.println(prefName);
The result must to be : Mickey74
Thank at all for your help.
Mickey74
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.