【发布时间】:2015-03-24 18:22:45
【问题描述】:
我想知道是否可以通过 Intent 从 Activity 向 BaseAdapter 发送多个信息。
确实,我有一个应用程序 android,我想将用户的邮件发送到包含 listView 的 BaseAdapter。
那么有可能吗?
我尝试这样但我有一个错误:(我发送 onPostExecute() )
我的活动:
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();
}
}
}
还有我的 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;
}
}
非常感谢您的帮助
米奇74
【问题讨论】:
-
你不应该使用意图。只需更新连接到列表视图的适配器。 Adapter 并不是作为一个 Activity 运行的,而是一个列表的构建块。
-
我可以在一个活动中发送信息而不去这个活动?
-
当您已经在包含设置了此适配器的列表视图的活动中时不会。您只需更新此适配器并调用 adapter.notifyDataSetChanged()
-
是的,但是当我在一个活动中按意图发送信息但不立即使用 startActivity() 继续这个活动时,有可能吗?因为如果我不继续活动,系统会记得我之前发送的信息吗?
-
我不关注。你可以将信息传递给一个活动,然后开始这个活动,是的。但是您不能在适配器上使用 Intent。您应该使适配器变量可以在您拥有更新适配器的信息的地方访问。就是这样。
标签: android listview android-intent baseadapter