AlertDialog布局元素 - 如何访问它们?

时间:2011-12-24 11:06:39

标签: java android android-activity dialog

我一直在研究GUI和Android应用程序。当我突然被一项简单的任务猝不及防时,一切都很顺利: 我有一项活动。在里面,我有一个列表视图。我想要做的是:当单击ListView中的项目时 - 打开包含有关此位置的更多信息的Dialog。好吧,显示一个Dialog有点容易,但问题是我无法改变f.e.标题。为什么?这是我的代码:

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

}
}

直接问题是在onItemClick函数中,我想从Dialog布局访问prowadzacyName TextView。它将始终为null,因为我设置了setContentView(searchView)。我这样做是因为这是我活动的布局。

有没有可能的方法来访问这些元素,或者更确切地说是以任何其他方式获取我想要的东西? (我认为Dialog是另一个Activity,但它听起来令人毛骨悚然 - 每个可能的对话的另一个活动?!:p)

2 个答案:

答案 0 :(得分:3)

据我所知,您使用了

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

如果您想在这种情况下访问子视图,请使用

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

我认为这可以解决您的问题。

答案 1 :(得分:2)

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

试一试