如何将ListView传递给我的Contextmenu项目?

时间:2012-02-03 14:22:44

标签: android listview

在我的deleteLogItem上我需要传递listView,这样我就可以找到位置(id)中的项目来删除它。

我怎么能把它传递给他。

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;



public class LogActivity extends ListActivity {

private static final int DELETE_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Parse.initialize(this, "sjfsjdfhjshfjhsjdfhsjhfjshdf4",
            "sdfgsdfgsdfg76sdfg76s7dfgsd");

    final ProgressDialog dialog = ProgressDialog.show(LogActivity.this, "",
            "Loading. Please wait...", true);

    ParseQuery query = new ParseQuery("Devices");
    query.whereEqualTo("Device", "ffffffff-e0d7-1802-8031-65e37f7a7ed3");
    query.findInBackground(new FindCallback() {
        public void done(List<ParseObject> scoreList, ParseException e) {
            if (e == null) {

                initListView(scoreList);
                dialog.dismiss();
            } else {
                objectRetrievalFailed();
            }
        }
    });

}

protected void objectRetrievalFailed() {
    // TODO Auto-generated method stub

}

private void initListView(List<ParseObject> objects) {
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
            2);

    HashMap<String, String> map;

    for (ParseObject hashMap : objects) {

        map = new HashMap<String, String>();
        map.put("line0", hashMap.getObjectId());
        map.put("line1", hashMap.getString("Model"));
        map.put("line2", hashMap.getString("Device"));
        map.put("line3", (String) hashMap.getCreatedAt().toString());
        list.add(map);
    }

    // the from array specifies which keys from the map
    // we want to view in our ListView
    String[] from = { "line1", "line2", "line3" };

    // the to array specifies the TextViews from the xml layout
    // on which we want to display the values defined in the from array
    int[] to = { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2 };

    // create the adapter and assign it to the listview

    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.first_list, from, to);
    setListAdapter(adapter);
    registerForContextMenu(getListView());
}

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, "Delete");
}

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case DELETE_ID:
        deleteLogItem(info.id);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

private void deleteLogItem(long id) {
    // TODO Auto-generated method stub
    HashMap<String, String> o = (HashMap<String, String>) l
            .getItemAtPosition((int)id);

    // String selection = l.getItemAtPosition(position).toString();
    String selection = o.get("line0");
    Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}

}

编辑:我只是添加了ListView l = getListView();

这一行

0 个答案:

没有答案