【问题标题】:Getting row ID of dynamic listview in android to add to cart在android中获取动态列表视图的行ID以添加到购物车
【发布时间】:2012-06-16 02:25:11
【问题描述】:

我的 android 客户端应用程序上有动态列表视图,可以从远程 mysql 数据库接收数据。我有一个上下文菜单,在用户单击该列表视图的任何行时打开。该菜单上有一个名为“添加到购物车”的选项。 I want to add the clicked row item to the cart when that context menu option is selected.那么如何获取行 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() {}

【问题讨论】:

    标签: android listview contextmenu cart rowid


    【解决方案1】:
        @Override
        public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
                int menuItemIndex = item.getItemId();
                int position = (info.position);
    ....
    

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

    【讨论】:

    • 谢谢@Nuno Goncalves。我需要通过在每行对应的上下文菜单中选择“添加到购物车”选项来存储用户想要添加到购物车的所有列表项。那么我该怎么做存储用户添加到购物车中的不同项目,稍后在同一用户单击上下文菜单中的显示购物车选项时显示?你能解释一下吗?
    • @ess.crazy,如果我的回答回答了您的问题,请将其标记为已接受。协调您的下一个目标,您应该将所选项目保存在数据库中。稍后,在上下文菜单中单击以查看该项目,您可以开始一个新活动并查看该项目的完整信息。如果您想查看购物车中的所有商品,您可以创建一个新活动,在其中查看存储在数据库中的所有商品。
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多