I have an activity in which I have to parse XML and populate Spinner using parsed data.
I ended up parsing XML. This is the method:
void parse_ExamList()
{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlContent);
NodeList nl = doc.getElementsByTagName(KEY_EXAMSET);
for ( int i = 0; i < nl.getLength();i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_SETID, parser.getValue(e, KEY_SETID));
map.put(KEY_SETNAME, parser.getValue(e, KEY_SETNAME));
menuItems.add(map);
}
}
If you notice that you can see that KEY_SETID and KEY_SETNAME are for arraylist. I have to fill in the counter KEY_SETNAME , and KEY_SETID will not be displayed on the counter. But if the element is clicked, then the identifier corresponding to the name must be obtained for sending to the server.
I have a way to populate a counter, for example:
public void addItemsOnExamListSpinner()
{
List<String> list = new ArrayList<String>();
list.add("Speed Test 150(min) PO Set-01");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
exam_list_spinner.setAdapter(dataAdapter);
}
How do I populate a counter using an ArrayList, which is obtained when parsing XML?
This is the full activity :
public class SpeedTestExamNameActivity extends Activity {
Spinner exam_list_spinner;
Button detailsBtn;
TextView showUser;
String full_name;
String responseBody;
String xmlContent=null;
static final String KEY_EXAMSET = "ExamSet";
static final String KEY_SETID = "SetId";
static final String KEY_SETNAME = "SetName";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed_test_exam_name);
isOnline_downloadList();
full_name=getFromPreference("user_name");
showUser=(TextView)findViewById(R.id.speed_username_textView);
showUser.setText("Welcome, "+full_name);
exam_list_spinner = (Spinner) findViewById(R.id.speed_examlist_spinner);
addItemsOnExamListSpinner();
detailsBtn = (Button) findViewById(R.id.speed_exam_details_button);
detailsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SpeedTestExamNameActivity.this,
"Exam List Spinner: "+ String.valueOf(exam_list_spinner.getSelectedItem()),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SpeedTestExamNameActivity.this, SpeedTestActivity.class);
SpeedTestExamNameActivity.this.startActivity(intent);
}
});
}
public String getFromPreference(String variable_name)
{
String preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name,"");
return preference_return;
}
public boolean isOnline_downloadList() {
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
new MyAsyncTask().execute(getFromPreference("student_code"));
return true;
}
AlertDialog.Builder Internet_Alert = new AlertDialog.Builder(SpeedTestExamNameActivity.this);
Internet_Alert.setCancelable(false);
Internet_Alert.setTitle("Attention!");
Internet_Alert.setMessage("This application requires internet connectivity, no internet connection detected");
Internet_Alert.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
onQuitPressed();
}
});
Internet_Alert.create().show();
return false;
}
public void onQuitPressed() {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
xmlContent=responseBody;
parse_ExamList();
}
protected void onProgressUpdate(Integer... progress){
}
public void postData(String student_code) {
HttpClient httpclient = new DefaultHttpClient();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(SpeedTestExamNameActivity.this);
final String url_first = preferences.getString("URLFirstPart","");
HttpPost httppost = new HttpPost(url_first+"ExamList");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("StudentCode", student_code));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
Log.d("result", responseBody);
}
catch (Throwable t ) {
Log.d("Error Time of Login",t+"");
}
}
}
void parse_ExamList()
{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlContent);
NodeList nl = doc.getElementsByTagName(KEY_EXAMSET);
for ( int i = 0; i < nl.getLength();i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_SETID, parser.getValue(e, KEY_SETID));
map.put(KEY_SETNAME, parser.getValue(e, KEY_SETNAME));
menuItems.add(map);
}
}
public void addItemsOnExamListSpinner()
{
List<String> list = new ArrayList<String>();
list.add("Speed Test 150(min) PO Set-01");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
exam_list_spinner.setAdapter(dataAdapter);
}
}
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SpeedTestExamNameActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/speed_choose_exam"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/speed_examlist_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
<Button
android:id="@+id/speed_exam_details_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_below="@+id/speed_examlist_spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="@string/details" />
<TextView
android:id="@+id/speed_username_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Name Title"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
, Spinner Arraylist, XML-?