user2042881
user2042881

Reputation: 125

Listview onitem click data into arraylist

When I click on list on product1 I get [product1], similarly when clicking on product2 I get [product2], but I want this: if I click on product 1 and then product 2, output has to be: [product1, product2].

public class MyListFrag extends ListFragment {

    Context context;    
    ListView listView;
    List<Content> rowItems;

    public static final String[] titles = new String[] { "Product1", "Product2", "product3" };
    public static final String[] descriptions = new String[] { "Android", "iphone", "blackberry"};



    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        this.context = activity.getApplicationContext();

        rowItems = new ArrayList<Content>();
        for (int i = 0; i < titles.length; i++) {
            Content item = new Content( titles[i], descriptions[i]);
            rowItems.add(item);         

        }       

        setListAdapter(new CustomBaseAdapter(activity.getApplicationContext(), rowItems));
    }


    public void onListItemClick(ListView l, View view, final int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, view, position, id);       

          String data1 =rowItems.get(position).getTitle();    
           System.out.println("Title"+data1);

          String data2 =rowItems.get(position).getDescriptio();       
          System.out.println("Descr"+data1);


          ArrayList<String> titleArray = new ArrayList<String>();         
          TitleArray.add(data1);      

                  ArrayList<String> desArray = new ArrayList<String>();       
          desArray.add(data2);

          System.out.println("Title::::"+titleArray);         
          System.out.println("descr:::"+desArray);   


    }



}

Upvotes: 1

Views: 1457

Answers (3)

Abhishek Birdawade
Abhishek Birdawade

Reputation: 301

I have modified this class and declared arrayList(titleArray and desArray) globally.Now it should work.

public class MyListFrag extends ListFragment {

Context context;    
ListView listView;
List<Content> rowItems;

ArrayList<String> titleArray = new ArrayList<String>();  
ArrayList<String> desArray = new ArrayList<String>();      

public static final String[] titles = new String[] { "Product1", "Product2", "product3" };
public static final String[] descriptions = new String[] { "Android", "iphone", "blackberry"};



public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    this.context = activity.getApplicationContext();

    rowItems = new ArrayList<Content>();
    for (int i = 0; i < titles.length; i++) {
        Content item = new Content( titles[i], descriptions[i]);
        rowItems.add(item);         

    }       

    setListAdapter(new CustomBaseAdapter(activity.getApplicationContext(), rowItems));
}


public void onListItemClick(ListView l, View view, final int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, view, position, id);       

      String data1 =rowItems.get(position).getTitle();    
       System.out.println("Title"+data1);

      String data2 =rowItems.get(position).getDescriptio();       
      System.out.println("Descr"+data1);



      titleArray.add(data1);      
      desArray.add(data2);

      System.out.println("Title::::"+titleArray);         
      System.out.println("descr:::"+desArray);   


}

}

Upvotes: 0

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Just Track the position where user had click.

ArrayList<Integer> posArray =new ArrayList<Integer> ();
     public void onListItemClick(ListView l, View view, final int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, view, position, id);       
     posArray.add(Integer.parseInt(position));
             // rest of the code   
    
    
        }

You can get all list of data by using posArray like

for(int i=0;i<posArray.size();i++){
System.out.println(String.ValueOf(titles[posArray.get(i)]));
}

Or Simply you can use titles[posArray.get(i)]) On onListItemClick, too.

EDIT

Try to initialize globally to

 ArrayList<String> titleArray = new ArrayList<String>();         

    
 ArrayList<String> desArray = new ArrayList<String>();  

& then add them into onListItemClick method

             TitleArray.add(data1);      
              desArray.add(data2);
    
              System.out.println("Title::::"+titleArray);         
              System.out.println("descr:::"+desArray); 

  

Upvotes: 1

Arunkumar
Arunkumar

Reputation: 3841

Your titleArray and desArray are local variables, they need to be moved out of onListItemClick method

Upvotes: 0

Related Questions