2

我的 android 客户端应用程序上有动态列表视图,可以从远程 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() {}
4

1 回答 1

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 被单击的列表的位置。(如果您还有填充列表视图的数据列表,则可以通过相同的索引获取它。

于 2012-06-13T06:58:57.613 回答