【发布时间】:2017-06-12 09:36:08
【问题描述】:
我尝试编写我的第一个 android 应用程序。现在我有一个问题 - setOnItemClickListener 不起作用。我在手机上启动了此应用程序,但按钮不起作用。贝娄你可以看到我的代码。如果您需要,我已准备好回答其他问题。感谢您的关注。
public class MainActivity extends Activity {
static class ElementSong {
String SongName;
ElementSong(String name) {
this.SongName = name;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText searchField = (EditText) findViewById(R.id.search_field); findViewById(R.id.Search_button);
final Button SearchButton = (Button) findViewById(R.id.Search_button);
final ListView item = (ListView) findViewById(R.id.item);
final ItemsAdapter adapter = new ItemsAdapter();
final Button SongButton = (Button) findViewById(R.id.SongButton);
item.setAdapter(adapter);
Integer.valueOf(price.getText().toString())));
InputStream databaseInputStream = getResources().openRawResource(R.raw.data);
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
br = new BufferedReader(new InputStreamReader(databaseInputStream));
try {
while ((line = br.readLine()) != null) {
//will be some code here
}
br.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
adapter.add(new ElementSong("123"));
item.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.exit(0);
}
});
}
private class ItemsAdapter extends ArrayAdapter<ElementSong> {
public ItemsAdapter() {
super(MainActivity.this, R.layout.item);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final View view = getLayoutInflater().inflate(R.layout.item, null);
final ElementSong item = getItem(position);
view.findViewById(R.id.SongButton)).setText(item.SongName);
return view;
}
}}
【问题讨论】:
-
错误是什么?
-
您没有在任何
Button上设置click listener,而是将其设置为您的ListeView。 -
你到底想做什么
-
使用 finish() 代替 System.exit()。参见 stackoverflow.com/questions/11257449/system-exit-in-android。知道为什么
-
@jackjay 是的,但我的列表视图包含按钮。它可以包含许多按钮。它们都将由
adapter.add(new ElementSong("123"));创建
标签: java android inputstream android-button