为什么Intent不能与setOnItemClickListener一起使用?

时间:2016-10-07 21:01:04

标签: java android listview android-intent

我正在尝试通过调用setOnItemClickListener使我的listItem可单击并传入一个意图来启动我的新活动,但它不起作用。基本上我的意图是使用不同的数据重用相同的活动但是现在,我只想按顺序获得点击功能。但是,此基本功能不起作用。

通过“不起作用”,我并不是说应用程序崩溃了。我的意思是listItem单击不会调用目标Activity。

我已将代码放入onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createDatabase();

        listView = (ListView) findViewById(R.id.lvSemesters);
        databaseHelper = new DatabaseHelper(MainActivity.this);
        semesterList = new DatabaseHelper(MainActivity.this).getAllSemesters();
        this.adapter = new SemesterAdapter(MainActivity.this, semesterList);
        floatActionButton = (FloatingActionButton) findViewById(R.id.addActionButton);


        floatActionButton.setOnClickListener(this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, CourseActivity.class);
                startActivity(intent);
            }
        });
    }

这是我的完整MainActivity以防万一:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView listView;
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase db;
    private FloatingActionButton floatActionButton;
    private SemesterAdapter adapter;
    List<SemesterEntity> semesterList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createDatabase();

        listView = (ListView) findViewById(R.id.lvSemesters);
        databaseHelper = new DatabaseHelper(MainActivity.this);
        semesterList = new DatabaseHelper(MainActivity.this).getAllSemesters();
        this.adapter = new SemesterAdapter(MainActivity.this, semesterList);
        floatActionButton = (FloatingActionButton) findViewById(R.id.addActionButton);


        floatActionButton.setOnClickListener(this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, CourseActivity.class);
                startActivity(intent);
            }
        });
    }

    protected void createDatabase() {
        db = openOrCreateDatabase("semesterDatabase", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS semesterTable(id INTEGER PRIMARY KEY, semester VARCHAR);");
    }

    public void select() {
        databaseHelper = new DatabaseHelper(MainActivity.this);
        semesterList = databaseHelper.getAllSemesters();
        SemesterAdapter adapter = new SemesterAdapter(MainActivity.this, semesterList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        int counter = databaseHelper.getUsersCount();
        if (counter < 8) {
            switch (id) {
                case R.id.addActionButton:
                    String semesterName = "Semester";
                    String query = "INSERT INTO semesterTable (semester) VALUES('" + semesterName + "');";
                    db.execSQL(query);
                    select();
                    break;
            }
        } else {
            Toast.makeText(this, "You can't add more than 8 semesters", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public void clearTable() {
        db.execSQL("delete from " + DatabaseHelper.TABLE_NAME);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.delete) {
            clearTable();
            select();
        }
        return super.onOptionsItemSelected(item);
    }
}

编辑:我已经解决了问题

基本上我在CardView的onClick XML属性中设置了一个方法,并在MainActivity中使用必要的意图代码创建了一个方法。

1 个答案:

答案 0 :(得分:0)

基本上我已经在CardView的onClick XML属性中设置了一个方法,并在MainActivity中使用必要的意图代码创建了一个方法。