1

当我点击 product1 上的列表时,我得到[product1],同样,当我点击 product2 时,我得到了[product2],但我想要这个:如果我点击产品 1,然后点击产品 2,输出必须是:[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);   


    }



}
4

3 回答 3

1

只需跟踪用户点击的位置。

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   
    
    
        }

您可以使用posArraylike获取所有数据列表

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

或者您也可以使用titles[posArray.get(i)])OnListItemClick。

编辑

尝试全局初始化为

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

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

& 然后将它们添加到 onListItemClick 方法中

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

  
于 2013-08-13T06:32:33.927 回答
0

我已经修改了这个类并全局声明了 arrayList(titleArray 和 desArray)。现在它应该可以工作了。

公共类 MyListFrag 扩展 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);   


}

}

于 2013-08-13T08:45:55.180 回答
0

你的titleArraydesArray是局部变量,它们需要移出onListItemClick方法

于 2013-08-13T06:48:25.950 回答