0

I know similar questions have been asked, but I haven't been able to find a solution that works for me. I've added the code I am using to call the JSON feed and display it in a ListAdapter. I need to get my fields "ShowDate" and "ShowStart" to display in a readable format, not UNIX.

public class Schedule extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected TextView textView;
protected ImageView imageView;
protected ArrayList<HashMap<String, String>> myList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule);

    View layout = findViewById(R.id.gradiant);

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, new int[] {
                    0xFF95B9C7, 0xFF06357A });
    gd.setCornerRadius(2f);

    layout.setBackgroundDrawable(gd);

    myList = new ArrayList<HashMap<String, String>>();

    JSONObject jsonObjSend = new JSONObject();
    JSONObject json = JSONfunctions
            .getJSONfromURL("MyAPIURL");


        JSONArray current = json.getJSONArray("d");

        for (int i = 0; i < current.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = current.getJSONObject(i);

            map.put("id", String.valueOf(i));
            map.put("showid", "" + Html.fromHtml(e.getString("ShowID")));
            map.put("name", "" + Html.fromHtml(e.getString("Title")));
            map.put("showvenue", "" + e.getString("ShowVenue"));
            map.put("subtutle", "" + e.getString("SubTitle"));
            map.put("venueid", "" + e.getString("VenueID"));
            map.put("showdate", "" + e.getString("ShowDate"));
            map.put("showstart", "" + e.getString("ShowStart"));
            map.put("showend", "" + e.getString("ShowEnd"));
            map.put("image250", "" + e.getString("Image250"));
            map.put("aboutartist", "" + Html.fromHtml(e.getString("AboutArtist")));


            myList.add(map);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());

    }



    ListAdapter adapter = new SimpleAdapter(this, myList,R.layout.line_item, 
            new String[] { "name", "showdate","showstart", "showvenue", "image250" }, 
            new int[] { R.id.title, R.id.showdate,R.id.showstart,R.id.showvenue, R.id.list_image });


    setListAdapter(adapter);



    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> hashMap = myList.get(position);
            // hashMap.put("map", hashMap);
            Intent intent = new Intent(getApplicationContext(),
                    ArtistDetails.class);
            intent.putExtra("map", hashMap);
            startActivity(intent);

        }

    });
}

Any suggestions or advice will be greatly appreciated. I've spent way too much time on this for something I assume is fairly simple. Thanks!

8
  • So is your problem with the JSON, the connection, or converting a Unix timestamp into a readable date? If it is the last one, just use a Date constructor that takes in a timestamp, and pass it to a SimpleDateFormat to format it the way you want. Otherwise, please narrow down what the problem is. Commented Mar 9, 2012 at 17:57
  • It is the converting Unix to a readable date. Would the Date constructor go inside the onCreate? Commented Mar 9, 2012 at 18:09
  • Date d = new Date(timestamp); String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(d); You can find what each letter means in the format by following the link in one of the posted answers. Commented Mar 9, 2012 at 18:14
  • I appreciate the help so far. Thank you all. hopefully last question before this works. I am getting an error on (timestamp). Do i need to define(protect) that in some way? Or is that where I should be adding my "showdate"? Commented Mar 9, 2012 at 18:45
  • timestamp is whatever variable you have defined that is storing the timestamp. ex: long timestamp = 3928385743; //Except that number would be from your JSON feed. Commented Mar 9, 2012 at 18:49

4 Answers 4

2

Thanks for all the replies. Turns out that this is all I need it to do. It may just be a quick fix, but it works. I needed to put it inside my JSONArray.

String showDate = e.getString("ShowDate");
long epoch = Long.parseLong( showDate );
Date showDatePresent = new Date( epoch * 1000 );
SimpleDateFormat sdf = new SimpleDateFormat("E, MMMM d");
String dateOfShow = sdf.format(showDatePresent);
map.put("showdate", "" + dateOfShow);
Sign up to request clarification or add additional context in comments.

Comments

0

Checked out the SimpleDateFormat object.

Comments

0

You can use SimpleDateFormat to parse the date you're given and then to format the way you want it

http://developer.android.com/reference/java/text/SimpleDateFormat.html

Comments

0

Like others have said, to convert a timestamp to a date you use SimpleDateFormat.

Date d = new Date(timestamp);
String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(d);

But it seems like you don't know where to put it. You will need to set a ViewBinder in your SimpleAdapter object that will display the date in the format you want.

SimpleAdapter adapter = new SimpleAdapter(this, myList,R.layout.line_item,
        new String[] { "name", "showdate","showstart", "showvenue", "image250" },
        new int[] { R.id.title, R.id.showdate, R.id.showstart, R.id.showvenue, R.id.list_image });

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if (columnIndex == cursor.getColumnIndex("showdate")
                || columnIndex == cursor.getColumnIndex("showstart")) {
            Date d = new Date(cursor.getLong(columnIndex));
            String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(d);
            ((TextView) view).setText(formattedDate);

            return true;
        }
        return false;
    }
});

setListAdapter(adapter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.