2010-11-23 101 views
2

這是類。我想在longpress上添加上下文菜單,但作爲新手我發現我很困難。請幫忙。Android:幫助從列表視圖創建上下文菜單

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; 
    } 

} 
+1

歡迎#1。您需要回顧以前的問題,並將所提供的答案之一標記爲正確的答案。 – 2010-11-23 23:01:50

+0

對不起梅拉。我不知道我必須這樣做。現在已經完成了,謝謝。 – Riza 2010-11-23 23:10:49

回答

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

這是一個可能幫助你解決第二個問題的例子。

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>