Widespread Augmented Reality

Widespread Augmented Reality
Click on the image to get the Android Augmented Reality Heads up Display

Monday, December 2, 2013

Android ArrayList, HashMap and Clickable ListView

I am coding a delete screen for the Android W.A.R. app. Get it here at Google Play before I break it.

Meanwhile, in order to list the geotags that can be deleted, I will need a list of geotags with "id" as the key.

So first, I use an Async task to make the HTTP call to my server and select geotags by "id". I will not show that proprietary code here, but I can show the Async's onPostExecute method where the following occurs:

  • Receive a table with two columns: id and title
  • Populate a HashMap with the columns named: "id" and "title"
  • Assign the HashMap values to an ArrayList
  • Send the ArrayList to a clickable ListView that uses android.R.layout.simple_list_item_2
  • Convert the ArrayList row back to a HashMap to retrieve "Id".
  • There must be a more elegant way to do this, but you get what you pay for. I do this for fun and for free.

    Just the bare bones...
    @Override
    protected void onPostExecute(String[][] result)
    {
    final ArrayList> alist = new ArrayList>();
    int size = result.length;
    for (int i = 0; i < size; i++) {
      HashMap cols = new HashMap();
      cols.put("id", result[i][0]);
      cols.put("title",result[i][1]);
      alist.add(cols);
    }
    // release result table
    result = null;
    //Identify the column names
    String[] from = { "id", "title" };
    //Send corresponding values to fields in android.R.layout.simple_list_item_2
    int[] to = { android.R.id.text1, android.R.id.text2 };
    ListView delview = (ListView) findViewById(R.id.list3);
    //Set up the adapter to hold the two column table
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), alist,
          android.R.layout.simple_list_item_2, from, to);
    delview.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //The Click Listener on each row
    delview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position1, long id) {
        Map mid = alist.get(position1);
        String sid = mid.get("id");
        Toast.makeText(getBaseContext(), "Clicked on tag Id: "+sid,
        Toast.LENGTH_SHORT).show();
    });
    return;
    }

    No comments:

    Post a Comment