如何使用 onItemClickListener 存储 ListView 中的可点击项目?
Posted
技术标签:
【中文标题】如何使用 onItemClickListener 存储 ListView 中的可点击项目?【英文标题】:How to store the Clickable items From ListView using onItemClickListener? 【发布时间】:2012-07-13 07:26:11 【问题描述】:如何在 listView 中存储可点击项目(许多)值并在下一个活动 listView 中显示项目。我试过了,但我只得到一个值(如果我点击更多项目,最后一个只显示)来自之前的Activity listView.i 使用了String[] 类型的GlobalVariable myval;
全局类
class GlobalClass extends Application
public static String[] myval;
家庭活动
public class HomeActivity extends Activity
String[] Category="----SELECT---- ","BEVERAGES","BREAKFAST","LUNCH","DINNER","DESSERTS","APPETIZERS & SIDES";
String[] Beverage="PEPSI","COKE","LASSI","FALOODA","BUTTER MILK","GREEN TEA","BADAM MILK","MASALA CHAI" ;
String[] Breakfast="SIRLOIN & EGG","CFS STEAK & EGG","T-BONE & EGG","TWO EGGS BREAKFAST","2EGG W/MEAT","BEL WAFFLE","BEL WAFFLE W/MEAT","BLUEBERRY CAKE","CAKES","FABULOUS FRENCH TST","MOONS","FRENCH TST";
String[] Lunch=" CRAB CAKE SANDWICH","TUSCAN GRILLED CHICKEN PANINI","SOUTHWEST TURKEY CLUB"," LOBSTER ROLL","TUNA MELT"," FRENCH DIP","YOUR OWN SANDWICH";
String[] Dinner="DUBLIN BAY PRAWN","CRAB FROM BRITTANY","WHITE ASPARAGUS","BLEWIT MUSHROOM","JOHN DORY FISH","MONKFISH","VEAL SWEET BREAD","LAMB","PIGEON FRY";
String[] Desserts="ECHOURGNAC CHEESE","HAZELNUT","GARIGUETTE STRAWBERRY","MOUSSE & ZEST","APRICOT","CHOCOLATE";
String[] Appetizers="Greek Artichoke Spinach","Seafood Appetizers","Tapenade Flatbread","Cranberry Blue Cheese","Fig and Blue Cheese","Sun-Dried Tomato","Tropical Crab Rangoon";
Spinner spinner;
ListView l1;
Button b1;
Button b2;
Button b3;
EditText e1;
EditText e2;
String[] item;
int myid;
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
spinner = (Spinner) findViewById(R.id.spinnerCategory);
l1=(ListView) findViewById (R.id.list);
b1=(Button)findViewById(R.id.button3);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
ArrayAdapter<String> adapter = new ArrayAdapter<String> this,android.R.layout.simple_spinner_item, Category);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final ArrayAdapter<String> bever = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Beverage);
// Assign adapter to ListView
final ArrayAdapter<String> Breakf = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Breakfast);
final ArrayAdapter<String> lunc= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Lunch);
final ArrayAdapter<String> Dinn= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Dinner);
final ArrayAdapter<String> Dessert = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Desserts);
final ArrayAdapter<String> Appet= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Appetizers);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id)
String Text = parent.getSelectedItem().toString();
if(Text.equals("----SELECT----"))
else if(Text.equals("BEVERAGES"))
l1.setAdapter(bever);
return;
else if(Text.equals("BREAKFAST"))
l1.setAdapter(Breakf);
return;
else if(Text.equals("LUNCH"))
l1.setAdapter(lunc);
return;
else if(Text.equals("DINNER"))
l1.setAdapter(Dinn);
return;
else if(Text.equals("DESSERTS"))
l1.setAdapter(Dessert);
return;
else if(Text.equals("APPETIZERS & SIDES"))
l1.setAdapter(Appet);
return;
public void onNothingSelected(AdapterView<?> arg0)
);
l1.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(), Toast.LENGTH_SHORT).show();
String s2=(String) ((TextView) view).getText();
// String ss=Integer.toString(myid);
String[] S5=new String[]s2;
//String[] S1=new String[]s2;
GlobalClass.myval=S5;
for (int i=0;i<GlobalClass.myval.length;i++)
System .out.println("Clicked-->"+GlobalClass.myval[i]);
//System.out.println("Clicked:" + GlobalClass.myval);
// System.out.println("items--"+l1.getItemIdAtPosition(position));
//i.putExtra("item", item);
//GlobalClass.myval=(String[]) l1.getSelectedItem();
//GlobalClass.myval=a1;
//String[] item = (String[]) (l1.getItemAtPosition(position));
//String[] a2=item;
//GlobalClass.myval=a2;
);
b1.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
String tno = e1.getText().toString();
int tn = Integer.parseInt(tno);
Intent i=new Intent(getApplicationContext(),TicketActivity.class);
Bundle b=new Bundle();
b.putInt("Table No:", tn);
i.putExtras(b);
String et= e2.getText().toString();
int et1 = Integer.parseInt(et);
Bundle be=new Bundle();
be.putInt("Guest:", et1);
i.putExtras(be);
startActivity(i);
);
提前致谢
【问题讨论】:
【参考方案1】:在列表项中单击
l1.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(), Toast.LENGTH_SHORT).show();
String s2=(String) ((TextView) view).getText();
// String ss=Integer.toString(myid);
you add values to a string array like
String[] S5=new String[]s2;
this will ovverride the previous element the all time shall
not add any other value
//String[] S1=new String[]s2;
GlobalClass.myval=S5;
for (int i=0;i<GlobalClass.myval.length;i++)
System .out.println("Clicked-->"+GlobalClass.myval[i]);
先这样做
// 在你的活动中创建之前声明数组列表
private ArrayList<String> AddedItems = new ArrayList<String>();
l1.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(), Toast.LENGTH_SHORT).show();
when lsit tiem is clicked add it to a array list like
AddedItems.add(((TextView) view).getText());
Once all click events are done
before going to next activity
b1.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
convert the string list to string array or whereevr in the activity you want
// create string array from array list
myval= listPar.toArray(new String[AddedItems .size()]);
);
【讨论】:
我的要求是将所有点击的项目存储在一个字符串数组中,并且我不在 onClick 中使用意图。我将访问下一个活动以使用 myval 全局变量访问项目 如何创建自定义列表视图,其中包含 QTY、ITEMS、PRICE 等 3 列 chat.***.com/rooms/5804/learn-android 我在这里添加了一些代码 抱歉回复晚了,但我去了一些地方,请从同一个地方获取 xml【参考方案2】:每次单击 ListView 上的项目时都会调用 OnItemClick()。每次调用 OnItemClick() 方法时,您都在创建 S5 数组。之后,您将使用仅具有最新单击值的新创建的数组覆盖 GlobalClass.myval。要收集所有值,请维护一个全局数组。您也可以直接在 GlobalClass.myval 中添加单击的值(假设它是所需类型的数组)。另一个建议,维护一个 List 而不是数组。
【讨论】:
【参考方案3】:向 ListView 添加内容,然后对每个 ListItem 值使用 onItemClickListener。您可以通过以下代码实现您的目标。
将它用于您的全局变量而不是字符串数组
全局类
class GlobalClass extends Application
public static List<String> myVal = new ArrayList<String>() ;
家庭活动
OnItemClickListener
listView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
String clickedvalue =(String) parent.getItemAtPosition(position);
myVal.add(clickedvalue);
);
现在所有点击的值都将在您的 ListArray 中。
希望对你有帮助。
【讨论】:
【参考方案4】: There is one more way we can add items to string array like this
int i = 0 ;
String[] S5;
fix its size
else if(Text.equals("BEVERAGES"))
l1.setAdapter(bever);
return;
else if(Text.equals("BREAKFAST"))
l1.setAdapter(Breakf);
S5=new String[]Breakf.size();
return;
else if(Text.equals("LUNCH"))
l1.setAdapter(lunc);
S5=new String[]lunc.size();
return;
else if(Text.equals("DINNER"))
l1.setAdapter(Dinn);
S5=new String[]Dinn.size();
return;
else if(Text.equals("DESSERTS"))
l1.setAdapter(Dessert);
S5=new String[]Dessert.size();
return;
else if(Text.equals("APPETIZERS & SIDES"))
l1.setAdapter(Appet);
S5=new String[]Appet.size();
return;
l1.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "SELECTED :: " +((TextView) view).getText(), Toast.LENGTH_SHORT).show();
String s2=(String) ((TextView) view).getText();
// String ss=Integer.toString(myid);
//String[] S1=new String[]s2;
// it add a new element to string array all the time list item is clicked
S5[i] = s2 ;
i++
);
【讨论】:
我需要在自定义列表视图中显示 3 列数量、价格、项目请发送 XML 布局和源示例 你需要的列表项布局在这里chat.***.com/rooms/5804/learn-android以上是关于如何使用 onItemClickListener 存储 ListView 中的可点击项目?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 onItemClickListener 存储 ListView 中的可点击项目?
执行长按时如何防止 OnItemClickListener 工作?
如何通过 onItemClickListener 在不同的类( ConsumerDescAndEdit.java )中使用某个类的变量?
如何使用 Android Studio 在 Twitter Kit 的 TweetUI 列表上设置 OnItemClickListener?