I have a problem that is a little hard to explain. I am building an android application for trip planning. I have a main activity that contains the map item and some other buttons and an AsyncTask class where the communication with the server is done and the path is displayed on the map. I am trying to make it work offline by saving the response from the server in sqllite database.
I have an Activity called MainActivity. There, I have a method for menu items
public boolean onOptionsItemSelected(final MenuItem pItem) {
switch (pItem.getItemId()) {
case R.id.recent_trips:
startActivity(new Intent(this, recentTripsActivity.class));
break;
default:
break;
}
return false;
}
}
Here is the current method for calling the AsyncTask class
btnPlanTrip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tripReq = new TripRequest(MainActivity.this,activityHandler,db);
tripReq.execute(request);
}
});
}
When I click on the recent_trips from the menu button, recentTripsActivity opens. Basically, in this class I retrieve some data from mysqllite database and present it on a listView through an adapter. There, I have a method view.setOnClickListener in which when a list item is clicked I take a certain value (res=listItem.getofflineResult();). I would like to use that value on Main Activity (see below for more details). Here is the code.
public class recentTripsActivity extends Activity implements Serializable {
private ImageButton stopSearch;
private AutoCompleteTextView searchField;
SQLiteDatabase db;
OfflineTripRequest tripReq;
String res;
//db = openOrCreateDatabase("AndroidDatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
private ListView listview;
private ArrayList mListItem;
ItemBO listItem;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recent_trips);
//open database connection
db = openOrCreateDatabase("AndroidDatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
int i=0;
String [] str={"id","description","offlineResult"};
listview = (ListView) findViewById(R.id.lview_trips);
mListItem = new ArrayList<ItemBO>();
Cursor cursor = db.query("tbl_offlinePathResult",str, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
i++;
ItemBO listItem = new ItemBO(i,cursor.getString(1),cursor.getString(2));
mListItem.add(listItem);
cursor.moveToNext();
if(i==10){
break;
}
}
// Make sure to close the cursor
cursor.close();
i++;
listview.setAdapter(new ListAdapter(recentTripsActivity.this, R.id.lview_trips,mListItem));
db.close();
}
//***ListAdapter***
class ListAdapter extends ArrayAdapter { //--CloneChangeRequired
private ArrayList mList; //--CloneChangeRequired
private Context mContext;
@SuppressWarnings("unchecked")
public ListAdapter(Context context, int textViewResourceId,
ArrayList list) { //--CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
this.mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
try {
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item_saved_paths, null); //--CloneChangeRequired(list_item)
}
final ItemBO listItem = (ItemBO) mList.get(position); //--CloneChangeRequired
if (listItem != null) {
// setting list_item views
((TextView) view.findViewById(R.id.tv_name))
.setText(listItem.getDescription());
view.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) { //--clickOnListItem
res=listItem.getofflineResult();
}
});
}
} catch (Exception e) {
Log.i(recentTripsActivity.ListAdapter.class.toString(), e.getMessage());
}
return view;
}
}
}
I would like to pass the data mentioned above(res=listItem.getofflineResult() to MainActivity. After doing that I would like to call an AsyncTask class and pass that data to that class. How can I do all this by clicking on the listItem? As I said before the map is on MainActivity but the click will happen from a different Activity. I hope I made my self clear enough, If you have any questions please ask me
I generally use a singleton method when passing data in an android app. If you are unfamiliar, it is basically a static class that always returns the same instance. This allows you to create multiple 'instances' of the class that all share the same data.
Well you can pass the res (String?) in many ways, the simple is throw preferences:
SharedPreferences settings = context.getSharedPreferences(APPNAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("res", res);
editor.commit();
and then in MainActiviy get:
SharedPreferences settings = context.getSharedPreferences(APPNAME, 0);
res = settings.getString("res","");
You can also save in the local database, or like "tkcsam" says using a static and abstract variable.
public abstract Global{
public static String res;
}
and you can acess in any Activity like :
Global.res = "A"
First, you need to override onListItemClick in your list. You will then need to put the needed information into a bundle and end the activity. This bundle gets "returned" to the calling activity in that activity's onActivityReuslt method (which you will need to override). See:
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.