如何按 Android 中的位置显示列表视图中的项目
How to display Item from a Listview by position in Android
我想按位置显示列表视图中的项目。例如:如果我点击第 3 个位置,我需要显示第 3 个项目。这是我的代码。我想在点击Item按钮时显示。
for (int k = 0; k < jsonarrays.length(); k++) {
JSONObject main_menus = jsonarrays.getJSONObject(k);
SUB_MENU_ID = main_menus.getString(TAG_SUB_MENU_ID);
SUB_MENU_NAME = main_menus.getString(TAG_SUB_MENU_NAME);
HashMap<String, String> submenu = new HashMap<String, String>();
submenu.put(TAG_SUB_MENU_ID, SUB_MENU_ID);
submenu.put(TAG_SUB_MENU_NAME, SUB_MENU_NAME);
submenus.add(submenu);
}
@Override
protected void onPostExecute(String result) {
submenuadapter = new SubmenufoodCategoryAdapter(submenus, getActivity());
sb_list.setAdapter(submenuadapter);
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});}
这是我的适配器 Class.:
public class SubmenufoodCategoryAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> mainmenu;
HashMap<String, String> mainmenumap = new HashMap<String, String>();
Context con;
public SubmenufoodCategoryAdapter( ArrayList<HashMap<String, String>> submenu, Context con) {
super();
this.mainmenu = submenu;
this.con = con;
}
@Override
public int getCount() {
return mainmenu.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainmenumap = mainmenu.get(position);
View rowview;
rowview = layoutInflater.inflate(R.layout.submenu_button, null);
holder.btn = (Button) rowview.findViewById(R.id.submenu_layout_button);
holder.btn.setText(mainmenumap.get(FoodMenuItemFragment.TAG_SUB_MENU_NAME));
return rowview;
}
public class Holder {
Button btn;
}
}
您可以启动另一个 Activity
并使用 Intent
发送这些数据
您可以试试下面的代码片段:
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//save data form here to some variables and on button click send data to where you want to show whether in new activity or something else
sbmenus.get(2).get(FoodMenuItemFragment.TAG_SUB_MENU_NAME);
}
});}
不要将此代码与您的要求进行比较..我只是给你一个想法..
我认为您正在尝试将适配器中的所有数据膨胀到列表视图。我制作了一个示例,其中我在对话框中显示了从适配器到列表视图的所有详细信息,因此
builder.setAdapter(notes_second_adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
notes_child_category_second.setText(notes_second_list
.get(position).get(KIDZDAY_TAG_DESCRIPTION));
acti_id_to_save = notes_second_list.get(position).get(
KIDZDAY_TAG_REF_ID);
//sampel_1 = "Nihal Srivastava";
}
});
dialog = builder.create();
dialog.show();
在这里你可以看到。将数据放入适配器后,您可以调用 ArrayList.get(position).value of that field.. 通过您的点击,它将根据位置
获取数据
您可以使用 AlertDialog。 Here是例子,回答2。
public void dialogBox(String message) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(message);
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertDialogBuilder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
使用要显示的数据调用 itemClick() 上的 dialogBox(String) 方法。
我想按位置显示列表视图中的项目。例如:如果我点击第 3 个位置,我需要显示第 3 个项目。这是我的代码。我想在点击Item按钮时显示。
for (int k = 0; k < jsonarrays.length(); k++) {
JSONObject main_menus = jsonarrays.getJSONObject(k);
SUB_MENU_ID = main_menus.getString(TAG_SUB_MENU_ID);
SUB_MENU_NAME = main_menus.getString(TAG_SUB_MENU_NAME);
HashMap<String, String> submenu = new HashMap<String, String>();
submenu.put(TAG_SUB_MENU_ID, SUB_MENU_ID);
submenu.put(TAG_SUB_MENU_NAME, SUB_MENU_NAME);
submenus.add(submenu);
}
@Override
protected void onPostExecute(String result) {
submenuadapter = new SubmenufoodCategoryAdapter(submenus, getActivity());
sb_list.setAdapter(submenuadapter);
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});}
这是我的适配器 Class.:
public class SubmenufoodCategoryAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> mainmenu;
HashMap<String, String> mainmenumap = new HashMap<String, String>();
Context con;
public SubmenufoodCategoryAdapter( ArrayList<HashMap<String, String>> submenu, Context con) {
super();
this.mainmenu = submenu;
this.con = con;
}
@Override
public int getCount() {
return mainmenu.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainmenumap = mainmenu.get(position);
View rowview;
rowview = layoutInflater.inflate(R.layout.submenu_button, null);
holder.btn = (Button) rowview.findViewById(R.id.submenu_layout_button);
holder.btn.setText(mainmenumap.get(FoodMenuItemFragment.TAG_SUB_MENU_NAME));
return rowview;
}
public class Holder {
Button btn;
}
}
您可以启动另一个 Activity
并使用 Intent
您可以试试下面的代码片段:
sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//save data form here to some variables and on button click send data to where you want to show whether in new activity or something else
sbmenus.get(2).get(FoodMenuItemFragment.TAG_SUB_MENU_NAME);
}
});}
不要将此代码与您的要求进行比较..我只是给你一个想法..
我认为您正在尝试将适配器中的所有数据膨胀到列表视图。我制作了一个示例,其中我在对话框中显示了从适配器到列表视图的所有详细信息,因此
builder.setAdapter(notes_second_adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
notes_child_category_second.setText(notes_second_list
.get(position).get(KIDZDAY_TAG_DESCRIPTION));
acti_id_to_save = notes_second_list.get(position).get(
KIDZDAY_TAG_REF_ID);
//sampel_1 = "Nihal Srivastava";
}
});
dialog = builder.create();
dialog.show();
在这里你可以看到。将数据放入适配器后,您可以调用 ArrayList.get(position).value of that field.. 通过您的点击,它将根据位置
获取数据您可以使用 AlertDialog。 Here是例子,回答2。
public void dialogBox(String message) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(message);
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertDialogBuilder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
使用要显示的数据调用 itemClick() 上的 dialogBox(String) 方法。