2013-02-11 54 views
0

我正在使用Android v4。我創建了一個列表視圖,顯示人接觸在Android中點擊編輯並保存在上下文菜單中時編輯列表項目

public class MainActivity extends Activity { 

    public ListView mainListView ; 
    public ArrayAdapter<String> listAdapter ; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Find the ListView resource. 
    mainListView = (ListView) findViewById(R.id.mainListView); 
    String[] contacts = new String[] { "Adam", "James", "Alex", "Tom", 
             "Horton", "Jim","Einstein","Thomson","Dick","Mascow"}; 
    ArrayList<String> contactsList = new ArrayList<String>(); 
    contactsList.addAll(Arrays.asList(contacts)); 

    listAdapter = new ArrayAdapter<String>(this, R.layout.rowview, contactsList); 
    listAdapter.add("Cameroon"); 

    // Set the ArrayAdapter as the ListView's adapter. 
    mainListView.setAdapter(listAdapter); 
    registerForContextMenu(mainListView); 
} 

,顯示文本菜單上長點擊列表項。 而Context_menu.xml如下,

<?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> 

<item 
    android:id="@+id/Delete" 
    android:title="@string/Delete">  
</item> 

</menu> 

當「刪除」,在右鍵菜單點擊我收到的ListItem值去除列表中的列表項。而我的上下文菜單代碼如下,

public boolean onContextItemSelected(MenuItem item){ 
    AdapterContextMenuInfo info=(AdapterContextMenuInfo)item.getMenuInfo(); 
    switch(item.getItemId()) 
    { 
    case R.id.Edit: 
     // Edit List Item and Save 
     return true; 
    case R.id.Delete: 
     listAdapter.remove(listAdapter.getItem(info.position)); 
     Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show(); 
    } 

    return true;   
} 

現在,我的疑問是如何被點擊編輯按鈕時,編輯特定列表項,保存編輯的聯繫。

我是Android新手,對處理事件沒有任何意見。 請任何人幫忙!

謝謝。

回答

0

你可以做做這樣

View view = yourListView.getChildAt(index - 
    yourListView.getFirstVisiblePosition()); 
TextView someText = ((TextView) view findViewById(R.id.textview01)); 
someText.setText("updated"); 
+1

感謝您的答覆。我想動態地輸入來自用戶的輸入並保存。 – Mahe 2013-02-11 12:22:53

相關問題