2015-07-21 62 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

相关问题