2

I have dynamic listview on my android client app that receive data from remote mysql DB. I have a context menu that opens on user click on any row i that listview. There is an option named' add to cart' on that menu. I want to add the clicked row item to the cart when that context menu option is selected. So how do I get the row ID for adding that slected/clicked row item to the cart just like shopping carts? I later need to display the selected row item in my 'Show Cart' class..Pls help..thanks in advance. I didnt find much articles that discuss about it with actual code .my listview display class is here:

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() {}
ess.crazy
  • 296
  • 1
  • 4
  • 24

1 Answers1

1
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
            int menuItemIndex = item.getItemId();
            int position = (info.position);
....

the menuItem index returns the context menu item selected (add to chart, ...) and the info.position the position of the list that was clicked. (if you also have the list of data that populated the list view you can get it by the same index.

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
  • Thanks @Nuno Goncalves .I need to store all the list items that the user user want to add to cart by selecting the'Add to Cart' option in each rows corresponding context menu.So how do i store the different items that are added to cart by the user and later display when the same user clicks on show cart option in context menu?Could you explain this too? – ess.crazy Jun 13 '12 at 07:24
  • @ess.crazy, if my answer answered your question, please mark it as accepted. Concertning your next objective, You should save the item selected on a database. And later on, on context menu click to view that item, you can start a new activity and see the full information of that item. If you want to see all items on the cart, you can create a new activity in which you see all items that are stored on the database. – Nuno Gonçalves Jun 13 '12 at 07:37