0

First i am sorry for my bad English. :)

I must find out which item has been clicked on to add it in an ArrayList. If items are clicked successively, I want to add them successively in the ArrayList.

final ArrayList oldPostion = new ArrayList<Integer>();

ArrayAdapter adapterONE = new ArrayAdapter(this,android.R.layout.simple_list_item_activated_1, list);
ListView one = (ListView) findViewById(R.id.listViewOne);
one.setAdapter(adapterONE);

one.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        oldPostion.add(position);
        int something = 0;
        if(oldPostion != null && oldPostion.get(position - 1) != null){
            if( oldPostion.get(position - 1) == position){

                //do something like this
                Toast.makeText(getApplicationContext(), "Item First Item" + "Selected Item" + one.getAdapter().getItem(position), Toast.LENGTH_LONG).show();
                something = 1 + 1;

            }else if(oldPostion.get(position) == position){

                //do something like this
                Toast.makeText(getApplicationContext(), "Item Second Item" + "Selected Item" + one.getAdapter().getItem(position) , Toast.LENGTH_LONG).show();
                something = 1 - 1;

            }

            //Result 
            Toast.makeText(getApplicationContext(), Integer.toString(something), Toast.LENGTH_LONG).show();
        }
    }
}
madlymad
  • 6,367
  • 6
  • 37
  • 68
Rida Am
  • 369
  • 4
  • 15
  • position - is the index id of an item in a list. Each item in a list has an id number in sequential order starting from 0 onward's. What do you want to store in the array, that number or the items text??? – Tasos Sep 12 '15 at 22:09
  • The Text. that I can save the text in *.txt or *.dat file – Rida Am Sep 12 '15 at 22:20

1 Answers1

0

To get the text, inside public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

add

TextView tv = (TextView) view.findViewById(R.id.the_text_view_id);
String text = tv.getText().toString();
Tasos
  • 5,321
  • 1
  • 15
  • 26
  • But I don't have a TextView in my layout ? – Rida Am Sep 12 '15 at 22:39
  • @Rida Am - What do you mean by Only the ListView?? you have nothing in the list view?? no text, no images, no buttons, no nothing?? what's this text you want to get then? – Tasos Sep 12 '15 at 22:43
  • I have a ListView that filled with an List that contains some Product name and that's all – Rida Am Sep 12 '15 at 22:44
  • and I must add this in a array list successively – Rida Am Sep 12 '15 at 22:46
  • @Rida Am - and this product name is not a text view? what is it then, how do you define it? – Tasos Sep 12 '15 at 22:46
  • No. I use the android.R.layout.simple_item_1 in the adapter and my list – Rida Am Sep 12 '15 at 22:48
  • or can I add there a TextView that shown in the ListView ? – Rida Am Sep 12 '15 at 22:51
  • @Rida Am - Have a look here -- http://stackoverflow.com/questions/3663745/what-is-android-r-layout-simple-list-item-1 -- The inbuilt android layout uses a TextView with id of (text1) -- so my answer is correct – Tasos Sep 12 '15 at 22:52
  • Now I understand the ListView have a TextView an from this a can get the Text – Rida Am Sep 12 '15 at 22:55
  • @Rida Am - yeah thats right. the simple layout you use is build in to android, so you don't have to write one your own. And it has one TextView inside it. – Tasos Sep 12 '15 at 22:57
  • @Rida Am - if the items have text, then they use a TextView. Because you are using an inbuilt layout and cant see the layout as it's build in then it threw you back a bit. But in the future if you use an inbuilt layout and has text, its using TextView. You just need find the XML file for that layout on the net or google to see what id's the textview's use if you want to ge the text – Tasos Sep 12 '15 at 23:04