gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:33:32 25 4
gpt4 key购买 nike

我一直在为 Android 应用程序开发 GUI。一切都很顺利,突然我被一个简单的任务打了个措手不及:我有一个 Activity 。在里面,我有一个 ListView 。我想要做的是:当单击 ListView 中的项目时 - 打开包含有关此位置的更多信息的对话框。嗯,显示对话框有点简单,但问题是我无法更改 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 函数中,我想从对话框布局访问 prowadzacyName 它是一个 TextView 。它始终为 null,因为我设置了 setContentView(searchView)。我这样做是因为这是我的 Activity 的布局。

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

最佳答案

据我所知,您使用了

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

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

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

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

关于java - AlertDialog 布局元素 - 如何访问它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8624133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com