我的 android 客户端应用程序上有动态 ListView ,它从远程 mysql 数据库接收数据。我有一个上下文菜单,当用户单击该 ListView 中的任何行时打开。该菜单上有一个名为“添加到购物车”的选项。我想在选择上下文菜单选项时将单击的行项目添加到购物车。那么,如何像购物车一样获取用于将选中/单击的行项目添加到购物车的行 ID?我稍后需要在我的“显示购物车”类中显示选定的行项目..请帮助..提前致谢。我没有找到很多用实际代码讨论它的文章。我的 ListView 显示类在这里:
public class MainMenu extends ListActivity {
Intent intent = getIntent();
InputStream is;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/phpmyadmin/php.php");
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
is = entity.getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("log_tag", "connection success ");
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result=sb.toString();
JSONArray jArray = null;
try {
jArray = new JSONArray(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int arrayLength=jArray.length();
String F_NAME[]=new String[arrayLength];
for(int i=0;i<arrayLength;i++){
JSONObject json_data = null;
try {
json_data = jArray.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
F_NAME[i]=json_data.getString("F_NAME");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
setListAdapter(new ArrayAdapter<String>(this, R.layout.list,F_NAME));
ListView lv = getListView();
lv.setTextFilterEnabled(true); registerForContextMenu(lv);}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.fltmenu, menu); menu.setHeaderTitle("Ask for");}
public boolean onContextItemSelected(MenuItem item) { //find out which menu item was pressed switch (item.getItemId()) {
case R.id.ToCart:
Dotocart();
return true;
default:
return false;}}private void Dotocart() {}
最佳答案
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
int position = (info.position);
....
menuItem 索引返回所选的上下文菜单项(添加到图表,...)和 info.position 被单击的列表的位置。 (如果您还有填充 ListView 的数据列表,您可以通过相同的索引获取它。
关于android - 获取android中动态 ListView 的行ID以添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009628/