当我点击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);
}
}
答案 0 :(得分: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
}
您可以使用posArray
之类的
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);
答案 1 :(得分:0)
您的titleArray
和desArray
是局部变量,需要将其移出onListItemClick
方法
答案 2 :(得分:0)
我修改了这个类并全局声明了arrayList(titleArray和desArray)。现在它应该可以工作。
public class 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);
}
}