setOnItemClickListener - 不起作用

时间:2017-06-12 09:36:08

标签: java android inputstream android-button

我曾尝试编写我的第一个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;

    }

}}

1 个答案:

答案 0 :(得分:0)

system.exit(0);替换为finish(); 或任何其他操作。所以你知道点击是否已经完成。