listview onitemclick can't refer - android

So, i plan to make a listview that if one of its item clicked, it goes to other activity. it's obvious that i want to use intent with extras.
But in my code, the extras is not reffering.
I tried to refer the extras with exact value, it worked, but if I put the extras to refer my object/variable, it won't work.
So far my code goes like this
public class bulan extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tagihan_list);
Intent i = getIntent();
final String no_pel = i.getStringExtra("no_pel");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("nopel", no_pel));
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
JSON json = new JSON();
JSONObject jobj = json.getJSON("http://10.0.2.2/KP/tagihan.php", pairs);
try {
int length = jobj.getInt("panjang");
for(int n = 1; n <= length; n++){
String m = Integer.toString(n);
JSONObject row = jobj.getJSONObject(m);
String id_tagihan = "No. tagihan : " + row.getString("id");
String bulan = row.getString("bulan");
String tahun = row.getString("tahun");
String tagihan = "Rp. " + row.getString("tagihan");
String status = row.getString("status");
HashMap<String, String> map = new HashMap<String, String>();
map.put("id_tagihan", id_tagihan);
map.put("bulan", bulan);
map.put("tahun", tahun);
map.put("tagihan", tagihan);
map.put("status", status);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list,
new String[] {"id_tagihan","bulan","tahun","tagihan","status"}, new int[] {
R.id.id_tagihan, R.id.bulan, R.id.tahun, R.id.tagihan, R.id.status});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String sid = ((TextView) view.findViewById(R.id.id_tagihan)).getText().toString();
String snopel = no_pel;
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
}
});
}
}
Like I explained before, it's this part
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", sid);
i.putExtra("no_pel", snopel);
startActivity(i);
that change my code to work if I write them like this
Intent i = new Intent(bulan.this, rincian.class);
i.putExtra("id", "00001");
i.putExtra("no_pel", "0620120001");
startActivity(i);

try
Intent i = new Intent(bulan.this, rincian.class);
Bundle b=new Bundle();
b.putString("id",sid);
b.putString("no_pel",snopel);
i.putExtras(b);
startActivity(i);
In your rincian class get the values as follows.
b=getIntent().getExtras();
String fri_id=b.getString("id");
String nopel=b.getString("no_pel");

Related

how to show data that get from selected listview in another activity

i have an app where in a form show the list view. the user can click one of the list view and when clicking the app will move to another activity and it's activity will show some data based on "code" that sent from the list view form. my app is success to move in another activity but doesn't show the data. please help me.
Thank you.
This is my code in list view form :
final ListView lv = (ListView)findViewById(R.id.lv);
String url = "http://192.168.43.244/wewash/listview.php";
try {
JSONArray data = new JSONArray(getJSONUrl(url));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("nama", c.getString("nama"));
map.put("tanggal_keluar", c.getString("tanggal_keluar"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(halaman_utama.this, MyArrList, R.layout.listview,
new String[] {"nama", "tanggal_keluar","id", ""}, new int[] {R.id.tsnama, R.id.tstglk, R.id.kode, R.id.tsspasi});
lv.setAdapter(sAdap);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String kode = ((TextView) findViewById(R.id.kode)).getText().toString();
Intent in = new Intent(halaman_utama.this, p_daftar_pakaian.class);
//menampilkan value dari item yg diklik
in.putExtra("id", kode);
startActivity(in);
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and it's my code in new activity :
Intent intent = getIntent();
String kode = intent.getStringExtra("id");
String url = "http://192.168.43.244/wewash/listview_detail.php?id="+kode;
try {
JSONArray data = new JSONArray(getJSONUrl(url));
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String idpk = c.getString("id");
String namapk = c.getString("nama");
String paketpk = c.getString("paket");
String cbiasapk = c.getString("cbiasa");
String ckhususpk = c.getString("ckhusus");
String tglantarpk = c.getString("tanggal_keluar");
String totalpk = c.getString("total");
// String nomorpk = c.getString("no_hp");
atasnama.setText(namapk);
ereview.setText("Id Pelanggan : " +idpk+
"\nNama Pelanggan : "+namapk+
"\nPaket : "+paketpk+
"\nCucian Biasa : "+cbiasapk+" kg"+
"\nCucian Khusus : "+ckhususpk+" item"+
"\nTotal : "+totalpk+" rupiah"+
"\nTanggal Antar : "+tglantarpk);
// enomer.setText(nomorpk);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
and it's the php file
<?php
mysql_connect("localhost","root","");
mysql_select_db("wewash");
$kode=$_GET["id"];
$sql=mysql_query("select * from pakaian where id='$kode'");
$arrayId=array();
if($sql === FALSE) {
die(mysql_error());
}
while($row=mysql_fetch_array($sql)){
$arrayId["id"]=$row["id"];
$arrayId["nama"]=$row["nama"];
$arrayId["tanggal_keluar"]=$row["tanggal_keluar"];
$arrayId["paket"]=$row["paket"];
$arrayId["cbiasa"]=$row["cbiasa"];
$arrayId["ckhusus"]=$row["ckhusus"];
$arrayId["total"]=$row["total"];
}
echo json_encode($arrayId);
?>

How to refresh list View in android?

I am using .net web services. I am trying to get list in ListView. Right now it is showing me the first list, but when I am trying to get it again using the same method it is giving me a response in log but not displaying in list.
I have used mAdapter.notifyDataSetChanged(); in my Adapter but it's not working. Please help. thanks
My code:
Intent mIntent = getIntent();
mIntent.getStringExtra("folder_name");
Id = mIntent.getStringExtra("folder_ID");
mIntent.getStringExtra("item_parent");
User_ID = mIntent.getStringExtra("User_ID");
subfolderTreedata();
}
public void subfolderTreedata() {
try {
--------
--------
-------- //some code here...
SoapObject SubfolderResponse = (SoapObject)envelope.getResponse();
Log.i("SubFolders", SubfolderResponse.toString());
String File_Ext=" ";
subfoldersitem = new String[SubfolderResponse.getPropertyCount()];
System.out.println(subfoldersitem.length);
for(int i=0; i < SubfolderResponse.getPropertyCount(); i++) {
SoapObject SingleSubFolder = (SoapObject)SubfolderResponse.getProperty(i);
subfoldersitem[0] = SingleSubFolder.getProperty(1).toString();
subfoldersitem[1] = SingleSubFolder.getProperty(0).toString();
subfoldersitem[2] = SingleSubFolder.getProperty(3).toString();
if(KEY_SUBJECTTYPE.equalsIgnoreCase("Folder")) {
item = new FolderList(Folderimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else{
StringTokenizer tokens = new StringTokenizer(Name, ".");
#SuppressWarnings("unused")
String first_string = tokens.nextToken();
File_Ext = tokens.nextToken();
if(File_Ext.equalsIgnoreCase("TIF")) {
item = new FolderList(TIFimages[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item);
} else {
item = new FolderList(noImage[0], subfoldersitem[0], subfoldersitem[1], subfoldersitem[2]);
Subfolderdata.add(item); }
}
}
subfolderslistview = (ListView)findViewById(R.id.subfolderslistview);
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
subfolderslistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
LazyAdapter ca = (LazyAdapter)parent.getAdapter();
FolderList item_name = (FolderList)ca.getItem(position);
FolderList DocumentID = (FolderList)ca.getItem(position);
FolderList type = (FolderList)ca.getItem(position);
Intent mIntent = new Intent();
mIntent.putExtra("item_name", item_name.folder_name);
mIntent.putExtra("item_id", DocumentID.ID);
mIntent.putExtra("item_type", type.type);
mIntent.getStringExtra("item_name");
String Type = mIntent.getStringExtra("item_type");
Log.i("Type", Type);
if(Type.equalsIgnoreCase("Folder")){
Id = mIntent.getStringExtra("item_id");
mAdapter.notifyDataSetChanged();
subfolderTreedata();
} else {
Intent i = new Intent(getApplicationContext(), Display_image.class);
i.putExtra("item_name", item_name.folder_name);
i.putExtra("ID", DocumentID.ID);
i.putExtra("item_type", type.type);
i.putExtra("User_ID",User_ID);
i.getStringExtra("item_name");
Id = i.getStringExtra("ID");
i.getStringExtra("item_type");
Log.i("id", Id);
startActivity(i);
}
}
});
public void list() {
mAdapter = new LazyAdapter(this, R.layout.jpg_row, Subfolderdata);
subfolderslistview.setAdapter(mAdapter);
}
Use like this list();.
Call this method from Where do you want.

Android listview header

i have an app that is showing data in listview ,but i want first row to be inflated by diferent layout , and i did that but since there is gona be a big number of listitems , i want to optimize listview and there is issue. i cant optimize listview when iam filling listview on that way , so how can i put content that should go in fist row inside listview header witch is inflated by some layout ,here is the code of Adapter
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
LinearLayout content = (LinearLayout) findViewById(R.id.content);
LinearLayout refLayout = (LinearLayout) findViewById(R.id.refLayout);
refLayout.setVisibility(View.GONE);
mBtnNaslovnica = (Button) findViewById(R.id.mBtnNaslovnica);
mBtnNaslovnica.setSelected(true);
TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
loadPage();
ImageButton mBtnRefresh = (ImageButton) findViewById(R.id.btnRefresh);
mBtnRefresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new LoadingTask().execute(URL);
}
});
}
public void loadPage(){
ArrayList<HashMap<String, String>> homeList = new ArrayList<HashMap<String, String>>();
JSONObject jsonobj;
try {
jsonobj = new JSONObject(getIntent().getStringExtra("json"));
JSONObject datajson = jsonobj.getJSONObject("data");
JSONArray news = datajson.getJSONArray(TAG_NEWS);
JSONArray actual = datajson.getJSONArray("actual");
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String story = c.getString(TAG_STORY);
String shorten = c.getString(TAG_SH_STORY);
String author = c.getString(TAG_AUTHOR);
String datetime = c.getString(TAG_DATETIME);
String img = c.getString(TAG_IMG);
String big_img = c.getString(TAG_BIG_IMG);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_STORY, story);
map.put(TAG_IMG, img);
map.put(TAG_BIG_IMG, big_img);
map.put(TAG_DATETIME, datetime);
map.put(TAG_AUTHOR, author);
// adding HashList to ArrayList
homeList.add(map);}
for(int i = 0; i < actual.length(); i++){
JSONObject c = actual.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ACT_TIME);
String body = c.getString(TAG_ACT_BODY);
String anews = " | "+ id+ " " + body;
String cur_anews = ((TextView) findViewById(R.id.scroller)).getText().toString();
String complete = anews + cur_anews;
TextView anewstv = (TextView) findViewById(R.id.scroller);
anewstv.setText(complete);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, homeList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cur_title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cur_story = ((TextView) view.findViewById(R.id.einfo2)).getText().toString();
String cur_author = ((TextView) view.findViewById(R.id.einfo1)).getText().toString();
String cur_datetime = ((TextView) view.findViewById(R.id.tVdatetime)).getText().toString();
String cur_actual = ((TextView) findViewById(R.id.scroller)).getText().toString();
ImageView cur_img = (ImageView) view.findViewById(R.id.list_image);
String cur_img_url = (String) cur_img.getTag();
Intent i = new Intent("com.example.androidhive.CURENTNEWS");
i.putExtra("CUR_TITLE", cur_title);
i.putExtra("CUR_STORY", cur_story);
i.putExtra("CUR_AUTHOR", cur_author);
i.putExtra("CUR_DATETIME", cur_datetime);
i.putExtra("CUR_IMG_URL", cur_img_url);
i.putExtra("CUR_ACTUAL", cur_actual);
startActivity(i);
}
});
}
public void reloadPage(String jsonstring){
ArrayList<HashMap<String, String>> homeList = new ArrayList<HashMap<String, String>>();
JSONObject jsonobj;
try {
jsonobj = new JSONObject(jsonstring);
JSONObject datajson = jsonobj.getJSONObject("data");
JSONArray news = datajson.getJSONArray(TAG_NEWS);
JSONArray actual = datajson.getJSONArray("actual");
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String story = c.getString(TAG_STORY);
String shorten = c.getString(TAG_SH_STORY);
String author = c.getString(TAG_AUTHOR);
String datetime = c.getString(TAG_DATETIME);
String img = c.getString(TAG_IMG);
String big_img = c.getString(TAG_BIG_IMG);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_STORY, story);
map.put(TAG_IMG, img);
map.put(TAG_BIG_IMG, big_img);
map.put(TAG_DATETIME, datetime);
map.put(TAG_AUTHOR, author);
// adding HashList to ArrayList
homeList.add(map);}
for(int i = 0; i < actual.length(); i++){
JSONObject c = actual.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ACT_TIME);
String body = c.getString(TAG_ACT_BODY);
String anews = " | "+ id+ " " + body;
String cur_anews = ((TextView) findViewById(R.id.scroller)).getText().toString();
String complete = anews + cur_anews;
TextView anewstv = (TextView) findViewById(R.id.scroller);
anewstv.setText(complete);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, homeList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cur_title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cur_story = ((TextView) view.findViewById(R.id.einfo2)).getText().toString();
String cur_author = ((TextView) view.findViewById(R.id.einfo1)).getText().toString();
String cur_datetime = ((TextView) view.findViewById(R.id.tVdatetime)).getText().toString();
String cur_actual = ((TextView) findViewById(R.id.scroller)).getText().toString();
ImageView cur_img = (ImageView) view.findViewById(R.id.list_image);
String cur_img_url = (String) cur_img.getTag();
Intent i = new Intent("com.example.androidhive.CURENTNEWS");
i.putExtra("CUR_TITLE", cur_title);
i.putExtra("CUR_STORY", cur_story);
i.putExtra("CUR_AUTHOR", cur_author);
i.putExtra("CUR_DATETIME", cur_datetime);
i.putExtra("CUR_IMG_URL", cur_img_url);
i.putExtra("CUR_ACTUAL", cur_actual);
startActivity(i);
}
});
}
public void startNewActivity(){
}
public class LoadingTask extends AsyncTask<String, Object, Object>{
XMLParser parser = new XMLParser();
JSONParser jParser = new JSONParser();
LinearLayout content = (LinearLayout) findViewById(R.id.content);
LinearLayout refLayout = (LinearLayout) findViewById(R.id.refLayout);
protected void onPreExecute(){
content.setClickable(false);
refLayout.setVisibility(View.VISIBLE);
}
#Override
protected Object doInBackground(String... params) {
// TODO Auto-generated method stub
String URL = params[0];
JSONObject json = jParser.getJSONFromUrl(URL);
//String xml = parser.getXmlFromUrl(URL); // getting XML from URL
// getting DOM element
return json;
}
protected void onPostExecute(Object result){
String json;
json = result.toString();
reloadPage(json);
refLayout.setVisibility(View.GONE);
}
}
}
If I got your point - you can go with 2 approaches:
Add headerView to the list. That is just easy as inflate your View and pass it to
addHeaderView(View) of your List. Note: you must add this view before setting the adapter, or it will throw the exception.
However, as your 'header' is representing the same data as all other items, but has different layout - I suggest not to use Header here. Instead, try to implement getItemViewType() in your adapter. http://developer.android.com/reference/android/widget/BaseAdapter.html#getItemViewType(int)
If you'll do - you'll have ability to check which type of layout to return in getView() method. And Android will take care of optimizing and reusing your inflated Views for you, so you can be sure that convertView, passed to your getView will be of the right type and layout.
Please let me know if I should explain with more details.
You can do it like this:
...
convertView.setOnClickListener(new OnItemClick(position));
...
public class OnItemClick implements OnClickListener {
private final int index;
public OnItemClick(int _index) {
this.index = _index;
}
#Override
public void onClick(View v) {
//dosomething
}
}

Saving Android Preferences

Hi, I have a listview and i'm wanting it so that when you press on an item it stores your selection in shared preferences. The purpose for this is so that next time I open the app it skips the selection process.
So what I'm wanting to know is how do I incorporate this function into my code?
Here is my code so far:
public class SelectTeamActivity extends ListActivity {
public String fulldata = null;
public String position = null;
public String divisionName= null;
public List<String> teamsList = null;
public String divName = null;
protected void loadData() {
fulldata = getIntent().getStringExtra("fullData");
position = getIntent().getStringExtra("itemIndex");
Log.v("lc", "selectteamActivity:" + fulldata);
Log.v("lc", "position:" + position);
int positionInt = Integer.parseInt(position);
try{
JSONObject obj = new JSONObject(fulldata);
teamsList = new ArrayList<String>();
JSONObject objData = obj.getJSONObject("data");
JSONArray teamInfoArray = objData.getJSONArray("structure");
for(int r = 0; r < teamInfoArray.length(); r++ ){
JSONObject teamFeedStructureDict = teamInfoArray.getJSONObject(r);
JSONArray teamStructureArray =
(JSONArray) teamFeedStructureDict.get("divisions");
JSONObject teamFeedDivisionsDictionary =
teamStructureArray.getJSONObject(positionInt);
divName = teamFeedDivisionsDictionary.getString("name");
JSONArray teamNamesArray =
(JSONArray) teamFeedDivisionsDictionary.get("teams");
for(int t = 0; t < teamNamesArray.length(); t++){
JSONObject teamNamesDict = teamNamesArray.getJSONObject(t);
teamsList.add(teamNamesDict.getString("name"));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selectact);
loadData();
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText(divName);
TextView redHeaderText = (TextView) findViewById(R.id.redheadertext);
redHeaderText.setText("Please select your team:");
setListAdapter( new ArrayAdapter<String>(this, R.layout.single_item,
teamsList));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {
Intent intent = new Intent(this, HomeActivity.class);
String curPos = Integer.toString(position);
//or just use the position:
intent.putExtra("itemIndex", curPos);
intent.putExtra("fullData", fulldata); //or just the part you want
startActivity(intent);
}
}
In your Activity's onCreate():
SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
In your onListItemClick():
preferences.edit().putInt("KEY", position).commit();
Everywhere in your project:
int position = preferences.getInt("KEY", -1);
(-1 is a default value that means when there is no value with the given key, return this value)

Converting UNIX time from Android JSON feed

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!
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);
Checked out the SimpleDateFormat object.
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
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);

Categories

Resources