所以我有listview加载来自mysql的数据.. 它加载ID(可见性:消失),名称,价格,..每个列表 我想要它,当我长时间点击和上下文菜单显示,我点击项目或项目输出它将获得项目的ID并发送到下一个活动..但我的只获得listview的第一个ID ..请帮助我
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
registerForContextMenu(lv);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Barang Masuk/Keluar");
menu.add(0, v.getId(), 0, "Masuk");
menu.add(0, v.getId(), 0, "Keluar");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Masuk") {
String pid = ((TextView) findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
galian_barang_masuk.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
} else if (item.getTitle() == "Keluar") {
String pid = ((TextView) findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
galian_barang_keluar.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
} else {
return false;
}
return true;
}
所以,当我从listview点击“pasir”然后上下文菜单出现.. 它转到listview“tanah”的第一个数据
http://imageshack.com/a/img835/2201/jrth.jpg
http://imageshack.com/a/img600/7562/ftzl.jpg
如果我使用直接点击listview onitem click ..使用此代码
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
Intent in = new Intent(getApplicationContext(),
galian_barang_masuk.class);
in.putExtra(TAG_PID, pid);
startActivityForResult(in, 100);
}
});
但是当我使用上下文时出现错误..不知道如何..
编辑:我想它..我制作新的textview ..我通过长按加载ID并将其保存到新的textview
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int pos, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
x = (TextView) findViewById(R.id.x);
x.setText(pid);
return false;
}
});
之后在onContextItemSelected中我将in.putExtra(TAG_PID, pid)
更改为in.putExtra(TAG_PID, x.getText().toString());
新的textview,用于收集我长按的每个listitem ID ..
答案 0 :(得分:0)
您可以使用AdapterContextMenuInfo从上下文菜单中获取所选列表项。例如,如果您有一个绑定到ListView的字符串列表,您将获得所选项目,如下所示:
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
String listItem = list.get(info.position);
//do something with the list item
}
答案 1 :(得分:0)
findViewById(R.id.pid),列表中的每一行都有一个具有相同id的视图。所以结果可以是随机的(不确定它是否总是选择第一场比赛)。在这些情况下,您可以尝试使用自定义适配器,该适配器可将所需值设置为视图的标记。 即在渲染列表行时,使用setTag方法为textview设置所需的值,然后使用getTag检索它。