2015-07-21 100 views
0

我想在單擊列表視圖後顯示帶有按鈕和Edittext的迷你表格。單擊列表視圖後顯示帶按鈕和Edittext的迷你表格

這是來自互聯網的代碼。如何在長按ListView項目後顯示帶有按鈕和文本框的簡單窗體。

我正在使用Android Studio。如果你想textBox和按鈕顯示爲AlertDialog或者要導航到另一個頁面的問題

lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       int pos, long id) { 
      // TODO Auto-generated method stub 

      //do what you want to do. 
      //If you want to delete that entry. 
      lv.remove(arg2);//where arg2 is position of item you click 
      myAdapter.notifyDataSetChanged(); 

      return true; 
     } 
    }); 

現在:

public class sample extends Activity{ 

    SimpleAdapter simpleAdpt; 
    private EditText pass; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sample); 
     initList(); 

     // We get the ListView component from the layout 
     ListView lv = (ListView) findViewById(R.id.listView); 

     simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1}); 

     lv.setAdapter(simpleAdpt); 
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>(); 

    private void initList() { 
     planetsList.add(createPlanet("planet", "Mercury")); 
     planetsList.add(createPlanet("planet", "Venus")); 
     planetsList.add(createPlanet("planet", "Mars")); 
     planetsList.add(createPlanet("planet", "Jupiter")); 
     planetsList.add(createPlanet("planet", "Saturn")); 
     planetsList.add(createPlanet("planet", "Uranus")); 
     planetsList.add(createPlanet("planet", "Neptune")); 
    } 

    private HashMap<String, String> createPlanet(String key, String name) { 
     HashMap<String, String> planet = new HashMap<String, String>(); 
     planet.put(key, name); 

     return planet; 
    } 

回答

0

你可能想實現一個ItemLongClickListener如下。

如果它應該是一個文本框的彈出

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setTitle("Title"); 
alert.setMessage("Message"); 

// Set an EditText view to get user input 
final EditText input = new EditText(this); 
alert.setView(input); 

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

    // Do something with value! 
    } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
    // Canceled. 
    } 
}); 

alert.show(); 

如果要導航至某個頁面

Intent intent = new Intent(ActivityA.this, ActivityB.class); 
startActivity(intent); 
+0

唉唉我明白了,所以我需要做的是有一個警報框? – Paul

+0

非常感謝,它給了我一個關於如何去做的想法。謝謝你:D – Paul

+0

Sir Uma Kanth,我如何從列表視圖中刪除選定的listviewitem的ID或其位置,請給我示例代碼以供參考。提前致謝。 – Paul

相關問題