0

I want to get a toast if someone push for example the listitem A and then it should calculate the days until the date of this item. And how can I get out of this list the date and save day month and year in different variable?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<Map<String, String>> list = buildData();
        String[] from = { "name", "purpose" };
        int[] to = { android.R.id.text1, android.R.id.text2 };

        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_2, from, to);
        setListAdapter(adapter);
    }

    private ArrayList<Map<String, String>> buildData() {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(putData("A", "20.7.2013"));
        list.add(putData("B", "21.7.2013"));
        list.add(putData("C", "22.7.2013"));
        return list;
    }

    private HashMap<String, String> putData(String name, String purpose) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("name", name);
        item.put("purpose", purpose);
        return item;
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

0

To get the "4 days from now" formatted text use: DateUtils.getRelativeTimeSpanString();

To parse the date string into a date use SimpleDateFormat. You can see more of that here. Convert string to Date in java

Another approach would be to create Java Date objects and use their methods to populate your list instead of deriving dates from strings. That is convert dates to strings, not strings to date, since you seem to know the dates already. The Date object is more flexible than a string representing a date.

==update== Your data is stored in the list variable. You can retrieve it when clicked using the position variable of the onListItemClick() function.

Community
  • 1
  • 1
Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47
  • and how can I get the item which is clicked? –  Jul 16 '13 at 23:27
  • I can't answer everything in a single post, but luckily Stack Overflow has teh answers. http://stackoverflow.com/questions/9151630/custom-adapter-get-item-number-of-clicked-item-in-inflated-listview – Plastic Sturgeon Jul 16 '13 at 23:37
  • I used this and how can I get the variable purpose (date) –  Jul 17 '13 at 00:02
  • protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String selection = l.getItemAtPosition(position).toString(); Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); } –  Jul 17 '13 at 00:02
  • updated above. if you have a new question, you should make a new question. On Stack overflow, its not a help forum, but a question and answer forum. If you think I haver answered the original question, please choose my answer as accepted. If its not, please claify your question above. thanks. – Plastic Sturgeon Jul 17 '13 at 17:57