简体   繁体   English

android Activity意向BaseAdapter

[英]android Activity intent to a BaseAdapter

I want to know if it's possible to send several informations by intent from an Activity to a BaseAdapter. 我想知道是否有可能通过意图将多个信息从Activity发送到BaseAdapter。

Indeed, I have an application android, and I want to send the mail of the user to an BaseAdapter that contains a listView. 确实,我有一个应用程序android,并且我想将用户的邮件发送到包含listView的BaseAdapter。

So is it possible ? 那有可能吗?

I try like this but I have an error : (I send in the onPostExecute() ) 我尝试这样,但出现错误:(我发送了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 : 和我的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 米奇74

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 结果必须是: Mickey74

Thank at all for your help. 谢谢您的帮助。

Mickey74 米奇74

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM