为动态ListView设置单击侦听器

时间:2016-04-19 09:03:57

标签: android listview onclicklistener

感谢永远在线......

我已经能够使用数据库中的信息填充ListView。

现在,我想让setOnClickListener将项目转到不同的页面。目前,我正在使用“switch(position)”构造,但它不能正常运行,因为我不能硬编码ListView上有多少项。

请帮助我解决这个问题。

公共类SurveyActivity扩展了AppCompatActivity {

ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db;
DatabaseHelper helper = new DatabaseHelper(this);

String listItem = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_survey);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Add new survey", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

            PopupMenu pop = new PopupMenu(SurveyActivity.this, fab);
            pop.getMenuInflater().inflate(R.menu.addsurvey_menu, pop.getMenu());
            pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {

                    Toast.makeText(getApplicationContext(), "Your choice is to " + item.getTitle().toString().toUpperCase() + " a survey form", Toast.LENGTH_LONG).show();

                    switch (item.getItemId()) {

                        case R.id.c:
                            //Takes admin to page where survey is created

                            Intent i = new Intent(getApplicationContext(), CreateSurvey.class);
                            startActivity(i);

                        case R.id.d:
                            //gets ready created XML forms from server

                        case R.id.r:
                            //refreshes and updates survey list from db

                    }

                    return true;
                }
            });

            pop.show();

        }
    });

    db = helper.getReadableDatabase();

    //Cursor crs = db.rawQuery("SELECT * FROM tbNames", null);
    Cursor crs=db.query("tbNames",new String[]{ "names"},null,null,null,null,null);

    if(crs.moveToFirst()){
        do {
            listItem =crs.getString(0);
            list.add(listItem);

        }while(crs.moveToNext());
    }

    ListView surveyList = (ListView) findViewById(R.id.surveylist);

    ArrayAdapter<String> aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    surveyList.setAdapter(aAdapter);
    registerForContextMenu(surveyList);

    surveyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {

                case 0:
                    Intent i = new Intent(getApplicationContext(), hivActivity.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent intent = new Intent(SurveyActivity.this, CreatedSurveyActivity.class);
                    startActivity(intent);
            }
        }
    });

}

}

我的应用程序允许用户创建多个调查,这些调查将显示在LIstView中。

问题是如何动态设置项目的监听器。

3 个答案:

答案 0 :(得分:0)

也许使用setOn 项目 ClickListener()

答案 1 :(得分:0)

发布您的代码会很有帮助,但是在类似情况下经常有用的方法是:

  1. 在生成时在每个视图上设置标记。例如,项目ID,或只是增量。
  2. onClickListener将接收视图作为参数。然后可以从中检索标签。
  3. 然后,此标记可以与页面名称,页面网址或您尝试加载的任何其他信息相匹配。

答案 2 :(得分:0)

您可以尝试此代码

if (!empty($old_user)) {
    if ($result_dest)  {
    // if they were logged in and are now logged out
    echo 'Logged out.<br />';
    $date = date("Y-m-d H:i:s");
    //Displays message, of the current datetime.
    echo "You logged out at: $date";

    do_html_url('login.php', 'Login');
} else {
    // they were logged in and could not be logged out
    echo 'Could not log you out.<br />';
}

第一项将调用surveyList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == 0) { Intent i = new Intent(getApplicationContext(), hivActivity.class); startActivity(i); } else { Intent intent = new Intent(SurveyActivity.this, CreatedSurveyActivity.class); startActivity(intent); } } }); ,所有其他项目将调用hivActivity。如果您需要将某些内容传递给CreatedSurveyActivity,请将其置于意图中,例如CreatedSurveyActivity