• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need help with implementing an alert dialog list view listener

Hello all,

I am trying to implement a series of menus that the user can select. You start on a screen with a list view, the user selects one of the items in the list and an alert dialog box pops up asking them whether they want to edit or delete the item. For some reason I can't get this list in the alert dialog to register. You can probably see what I'm trying to do.

I have never done an alert dialog inside the OnClick listener before and I'm kind of confused.

Here is my code:

Code:
public class F2Activity extends DashboardActivity 
{

	

protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView (R.layout.activity_f2);
    setTitleFromActivityLabel (R.id.title_text);
  
    String[] grids = getResources().getStringArray(R.array.grids_array);
    final ListView listView = (ListView) findViewById(R.id.listView2);
   listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
    	grids));
    listView.setOnItemClickListener(new OnItemClickListener()
    {
    	public void onItemClickListener(AdapterView<?>parent, View view,
    			int position, long id)
    	{
    		//Toast.makeText(getApplicationContext(), "you have selected " + listView.getSelectedItem().toString(), Toast.LENGTH_LONG);
    		
    		
    	}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int        position, long id) {
			
			

		   
			// TODO Auto-generated method stub
			Intent selectedItemByUser = new Intent();
			setResult(RESULT_OK, selectedItemByUser);
			Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();	
			
			
			  
			AlertDialog.Builder builder = new AlertDialog.Builder(F2Activity.this);
			builder.setTitle("Select Edit or Delete");

			final ListView modeList = new ListView(F2Activity.this);
			
			//using the variable runTimeName to keep track of user choice to modify menu.
			
		    final String runTimeName = listView.getItemAtPosition(position).toString();
		    
		    
		    listView.getItemAtPosition(position).getClass();
			String[] stringArray = new String[] { "Edit " + runTimeName, "Delete " + runTimeName};
			ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(F2Activity.this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
			modeList.setAdapter(modeAdapter);
            
			builder.setView(modeList);
			Dialog dialog = builder.create();
			
			dialog.show();
	
			//////////IS THIS EVEN THE RIGHT WAY TO DO THIS????? OR DO MY OnClicks conflict???
			modeList.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent2, View view2, int position2, long rowID){
					//do something.....
					
					
			if(modeList.getItemAtPosition(position2).toString() == "Edit " + runTimeName)
			{
				//start intent to go to edit menu, only top things are editable....
				Intent changeToEdit = new Intent(view2.getContext(), EditGridActivity.class);
	    		startActivityForResult(changeToEdit, 0);
	    		// MAKE A CLASS FOR GRID!!!!!!!!!!!!!!!!!!!!!!, INVLOVING GRIDNICKNAME, GRID NAME, FIRST NAME, LAST NAME, PASSWORD, LOGIN URI....!
	    		Toast.makeText(getApplicationContext(), "my " + modeList.getItemAtPosition(position2).getClass(), Toast.LENGTH_LONG);
			}
			else if(modeList.getItemAtPosition(position2).toString() == "Delete " + runTimeName)
			{
			  //remove from grid using length check? minus 1 less then before in length?????
				Toast.makeText(getApplicationContext(), "you wanted to delete " + modeList.getItemAtPosition(position2), Toast.LENGTH_LONG);
			}
			
			
				}
			});
			
		}
		
    });
    
    
    // onclicklisten for add button.
    Button addButton = (Button) findViewById(R.id.addGridButton);
    
    addButton.setOnClickListener(new View.OnClickListener() {
    	public void onClick(View view) {
    		Intent changeIntent = new Intent(view.getContext(), AddGridActivity.class);
    		startActivityForResult(changeIntent, 0);
    	}
		
		
	});
    
}
    
} // end class
 
I don't know if this is any indication of the onClick working or not, but if I change getApplicationContext() to getParent() the app force closes.
 
Back
Top Bottom