Thursday, 22 August 2013

viewpager calls the next view background code

viewpager calls the next view background code

I have a viewpager of 4 screens, where each screen calls a webservice and
displays data. Lets say I'm on the first screen - what happens is it calls
its own webservice and displays the data, and then it calls the webservice
of the next screen, so that when I scroll to the second screen it displays
the correct data, and calls the web service of the third screen.
How do I make it call the correct web service for each screen and stop?
Here is my code sample:
public class MyPagerAdapter extends PagerAdapter {
ListView ListContainer;
// ArrayList<String> grades = new ArrayList<String>();
ArrayList<String> names = new ArrayList<String>();
ViewPager myViewPager;
DBAdapter databaseAdapter;
String connectionCode;
String student_id;
int index;
// State number of pages
public int getCount() {
return 4;
}
public int getNameIndex() {
return index;
}
public void setStudentIndex(int index) {
this.index = index;
}
// Set each screen's content
@Override
public Object instantiateItem(View container, final int position) {
final Context context = container.getContext();
LinearLayout layout = new LinearLayout(context);
ListContainer = new ListView(context);
ListContainer.setId(12345);
// Add elements
ActionBar actionbar = getActionBar();
actionbar.setTitle("connection");
// myViewPager = (ViewPager) findViewById(R.id.Grades);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(p);
ListContainer.setLayoutParams(p);
switch (position) {
case 0:
actionbar.setTitle("connections list");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
context, android.R.layout.simple_list_item_1, names);
ListContainer.setAdapter(adapter);
ListContainer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Name = names.get(arg2);
Cursor cursor = databaseAdapter.getStudentByName(Name);
try {
cursor.moveToFirst();
student_id = cursor.getString(cursor
.getColumnIndexOrThrow("student_id"));
StudentId = student_id;
//setStudentIndex(arg2);
index=arg2;
myViewPager.setCurrentItem(1, false);
} catch (Exception e) {
String x = e.toString();
Toast.makeText(context, e.toString(),
Toast.LENGTH_LONG).show();
}
}
});
break;
case 1:
actionbar.setTitle(Name + "'s Grades");
ArrayAdapter<String> Gradesadapter = new ArrayAdapter<String>(
context, android.R.layout.simple_list_item_1, grades);
ListContainer.setAdapter(Gradesadapter);
ListContainer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String month = grades.get(arg2);
if (month.equals("January"))
month = "1";
else if (month.equals("February"))
month = "2";
else if (month.equals("March"))
month = "3";
else if (month.equals("April"))
month = "4";
else if (month.equals("May"))
month = "5";
else if (month.equals("June"))
month = "6";
else if (month.equals("July"))
month = "7";
else if (month.equals("August"))
month = "8";
else if (month.equals("September"))
month = "9";
else if (month.equals("October"))
month = "10";
else if (month.equals("November"))
month = "11";
else if (month.equals("December"))
month = "12";
Intent intent = new Intent(context,
GradesListActivity.class);
intent.putExtra("month", month);
intent.putExtra("student_id", StudentId);
try {
startActivity(intent);
} catch (Exception e) {
}
}
});
break;
case 2:
actionbar.setTitle(Name + "'s messages");
try {
getThereports();
ListContainer = populateReportView(ListContainer);
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG)
.show();
}
ListContainer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(Home.this,
ReportActivity.class);
intent.putExtra("feedback_message", reports.get(arg2)
.getFeedback_message());
startActivity(intent);
}
});
break;
case 3:
actionbar.setTitle(Name + "'s school");
try {
schools = new ArrayList<school>();
getTheSchool();
ListContainer = populateSchoolView(ListContainer);
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG)
.show();
}
break;
default:break;
}
layout.addView(ListContainer);
((ViewPager) myViewPager).addView(layout, 0); // This is the line I
// added
return layout;
}

No comments:

Post a Comment