Android:帮助从listview创建上下文菜单

时间:2010-11-23 22:47:09

标签: android listview contextmenu

这是班级。我想在longpress上添加上下文菜单,但作为一个新手,我发现我很难。请帮忙。

 private ListView lvUsers;
 private ArrayList<UserBO> mListUsers;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);


        mListUsers = getUsers();
        lvUsers = (ListView) findViewById(R.id.lv_user);
        lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));                

    }

    public ArrayList<UserBO> getUsers(){          

  DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
  try {
   dbAdapter.createDataBase();
  } catch (IOException e) {
   Log.i("*** select ",e.getMessage());
  }
     dbAdapter.openDataBase();  
  String query="SELECT * FROM user;";
  ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
  dbAdapter.close();

  ArrayList<UserBO> usersList = new ArrayList<UserBO>();
  for (int i = 0; i < stringList.size(); i++) {
   ArrayList<String> list = stringList.get(i);
   UserBO user = new UserBO();
   try {
    user.id = Integer.parseInt(list.get(0));
    user.name = list.get(1);
    user.age = list.get(2);
   } catch (Exception e) {
    Log.i("***" + Select.class.toString(), e.getMessage());
   }
   usersList.add(user);
  }
  return usersList;
 }

 // ***ListAdapter***
 private class ListAdapter extends ArrayAdapter<UserBO> {  // --CloneChangeRequired
  private ArrayList<UserBO> mList;  // --CloneChangeRequired
  private Context mContext;

  public ListAdapter(Context context, int textViewResourceId,ArrayList<UserBO> list) { // --CloneChangeRequired
   super(context, textViewResourceId, list);
   this.mList = list;
   this.mContext = context;
  }

  public View getView(int position, View convertView, ViewGroup parent){
   View view = convertView;
   try{
   if (view == null) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.list_item, null);  // --CloneChangeRequired(list_item)
   }
   final UserBO listItem = mList.get(position);  // --CloneChangeRequired    
   if (listItem != null) {
    // setting list_item views      
    ( (TextView) view.findViewById(R.id.tv_id) ).setText( listItem.getId()+"");
    ( (TextView) view.findViewById(R.id.tv_name) ).setText( listItem.getName() );
    ( (TextView) view.findViewById(R.id.tv_age) ).setText( listItem.getAge()+"" );

   }}catch(Exception e){
    Log.i(Select.ListAdapter.class.toString(), e.getMessage());    
   }
   return view;
  }

 }

2 个答案:

答案 0 :(得分:4)

这应该可以解决问题。别忘了:registerForContextMenu(lvUsers);

/** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.select);

        mListUsers = getUsers();
        lvUsers = (ListView) findViewById(R.id.lv_user);
        lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));  
        **registerForContextMenu(lvUsers);**      
        }

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  switch (item.getItemId()) {
  case R.id.edit:
    editNote(info.id);
    return true;
  case R.id.delete:
    deleteNote(info.id);
    return true;
  default:
    return super.onContextItemSelected(item);
  }
}

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit"
          android:title="@string/edit" />
    <item android:id="@+id/delete"
          android:title="@string/delete" />
</menu>

答案 1 :(得分:1)

这是一个可以帮助您解决第二个问题的示例。

<强> MainActivity.class

 public class MainActivity extends Activity {
    TextView textView;
    Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Data to be passed
        final String data = "String to Pass";

        textView = (TextView) findViewById(R.id.text1);
        textView.setText(data);

        //Pass Data with Button
        final Button btn = (Button) findViewById(R.id.btn1);
          btn.setOnClickListener(new View.OnClickListener() {

              @Override
          public void onClick(View v) {

                intent = new Intent(v.getContext(), SecondActivity.class);
                intent.putExtra("sampleData", data);
                startActivity(intent);  
              }
         });
    }
}

<强> SecondActivity.class

public class SecondActivity extends Activity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         //Get data from Intent
         Bundle extras = getIntent().getExtras();
         String data = extras.getString("sampleData");

         textView = new TextView(this);
         textView.setText("String: " + data);

         setContentView(textView);
    }

}

不要忘记将您的活动添加到AndroidManifest.xml。

<强>的AndroidManifest.xml

<activity android:name=".SecondActivity"></activity>