在安卓中获取动态列表视图的行ID以添加到购物车



我的安卓客户端应用程序上有动态列表视图,可以从远程 mysql 数据库接收数据。我有一个上下文菜单,当用户单击该列表视图的任何行时打开。该菜单上有一个名为"添加到购物车"的选项。我想在选择该上下文菜单选项时将单击的行项目添加到购物车中。那么,如何获取行 ID,以便像购物车一样将该移动/单击的行项目添加到购物车中?我稍后需要在我的"显示购物车"类中显示选定的行项目。请帮忙..提前谢谢。我没有找到太多讨论它与实际代码的文章.我的列表视图显示类在这里:

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);
....
菜单项

索引返回选定的上下文菜单项(添加到图表,...),info.position 返回单击的列表的位置。(如果您还有填充列表视图的数据列表,则可以通过同一索引获取它。

相关内容

  • 没有找到相关文章