dontgonearthecastle
dontgonearthecastle

Reputation: 5

AlertDialog layout elements - how access them?

I've been working on a GUI for and Android app. Everything was going ok when I suddenly was caught off guard by a simple task: I have an activity. Inside, I have a list view. What I want to do is: when item from the ListView is clicked - open Dialog containing more information about this position. Well, showing a Dialog is kinda easy, but the problem is that I can't change f.e. the Title. Why? Here's my code:

public class SzukajActivity extends Activity {
private ListView searchResultsListView;
private String DB[] = {"Blah", "Bleh", "Boing", "Quing", "Something"};
private ArrayList<String> searchResults = new ArrayList<String>();
private EditText searchNameEditText;
private ArrayAdapter<String> searchListViewAdapter;
private TextView prowadzacyName;
private AlertDialog prowadzacyDialog;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    LayoutInflater searchFactory = LayoutInflater.from(this);
    final View searchView = searchFactory.inflate(R.layout.search, null);
    setContentView(searchView);

    // prowadzacySite Dialog
    LayoutInflater prowadzacySiteFactory = LayoutInflater.from(this);
    final View prowadzacyDialogView = prowadzacySiteFactory.inflate(R.layout.prowadzacy_site, null);
    prowadzacyName = (TextView) findViewById(R.id.prowadzacyname);
    //prowadzacyName.setText("a KUKU!");
    AlertDialog.Builder prowadzacyDialogBuilder = new AlertDialog.Builder(this);
    prowadzacyDialogBuilder.setTitle("Prowadzacy")
                      .setCancelable(false)
                      .setView(prowadzacyDialogView)
                      .setPositiveButton("Zapisz się", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       })
                       .setNegativeButton("Zakoncz", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
    prowadzacyDialog = prowadzacyDialogBuilder.create();

    searchResultsListView = (ListView) findViewById(R.id.searchKurczeBladeView);
    searchListViewAdapter = new ArrayAdapter<String>(this, R.layout.spotkanialistitem, searchResults);
    searchResultsListView.setAdapter(searchListViewAdapter);
    searchResultsListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            prowadzacyName = (TextView) findViewById(R.id.prowadzacyname);
            if(prowadzacyName == null) Toast.makeText(SzukajActivity.this, "BOOOOM!", Toast.LENGTH_SHORT).show();
            //prowadzacyName.setText("A KUKU!");

            prowadzacyDialog.show();
        }
    });
    searchNameEditText = (EditText) findViewById(R.id.searchName);
    searchNameEditText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
            {
                searchResults.clear();
                for(int i = 0; i < 5; ++i)
                {
                    if(DB[i].contains(searchNameEditText.getText()))
                    {
                        searchResults.add(DB[i]);
                    }
                }
                if(searchResults.size() == 0)
                    Toast.makeText(SzukajActivity.this, "Nie znaleziono odpowiadających prowadzących", Toast.LENGTH_SHORT).show();
                searchListViewAdapter.notifyDataSetChanged();
                return true;
            }
            else
                return false;
        }
    });

}
}

The direct problem is that in onItemClick function, I want to access prowadzacyName which is a TextView from a Dialog layout. It will always be null, because I set setContentView(searchView). I do it because it's the layout of my activity.

Is there a possible way to access those elements, or rather any other way to get what I want? (I thought about Dialog being an other Activity, but it sounds creepy to me - another activity for every possible dialog?! :p)

Upvotes: 0

Views: 1567

Answers (2)

Balaji.K
Balaji.K

Reputation: 4829

As per my knowledge ,you used

    final View searchView = searchFactory.inflate(R.layout.search, null);
    setContentView(searchView);

If you want to access the child view in this case use

  prowadzacyName = (TextView)searchView.findViewById(R.id.prowadzacyname);

I think this may solve your problem.

Upvotes: 3

prowadzacyName = (TextView) prowadzacyDialogView.findViewById(R.id.prowadzacyname);

give a shot

Upvotes: 2

Related Questions