Вопрос

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() {}
Это было полезно?

Решение

    @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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top