0

I want to show a mini form with button and Edittext inside after clicking the listview.

This is code from the internet. How can I show a simple form with button and textbox after a long clicked in listview item.

I'm using Android Studio.

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;
    }
Community
  • 1
  • 1
Paul
  • 149
  • 1
  • 13

1 Answers1

0

You might want to implement an ItemLongClickListener as follows:

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

Now If you want the textBox and button to appear as a AlertDialog or you want to navigate to another page is the question.

If it should be a popup with a textBox

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();

If it should navigate to a page

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • Ahhh i see, so all i need to do is to have an alert box? – Paul Jul 22 '15 at 05:16
  • Thanks a lot, it gives me an idea on how to do it . thanks you :D – Paul Jul 22 '15 at 05:17
  • Sir Uma Kanth , How can i remove selected listviewitem from listview by its ID or its position , Please give me sample code for my references. Thanks in advance. – Paul Jul 22 '15 at 05:20
  • Thanks it helps a lot – Paul Jul 22 '15 at 08:22
  • Sir Uma Kanth , It is possible to pass the value of listview Id into another form? , if it's possible then how to do it?. – Paul Jul 22 '15 at 08:29
  • Yes, it is possible to send values to another activity using [`Intents`](http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android) – Uma Kanth Jul 22 '15 at 08:30
  • ahhh i get it. I will research more about it. Once again thank you :D – Paul Jul 22 '15 at 08:49