我試圖寫我的第一個android應用程序。現在我有一個問題 - setOnItemClickListener不起作用。我在手機上啓動了此應用程序,但按鈕無法使用。貝婁你可以看到我的代碼。如果需要,我準備回答其他問題。感謝您的關注。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;
}
}}
什麼是錯誤? – Kaushal28
你沒有在任何'Button'上設置'click listener',而是將它設置爲你的'ListeView'。 –
你想要做什麼 – Anil