列表视图中的可点击项目

时间:2013-03-04 15:29:46

标签: java android xml

我试图让我的listview项目可点击;但无法让它正常工作。当我点击某个项目时,它会切换屏幕,但不会移动到所需的屏幕。这是代码

private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
    lv = (ListView) findViewById(R.id.listView1);
    // Instanciating an array list (you don't need to do this, you already have yours)
    ArrayList<String> menu_Items = new ArrayList<String>();
    menu_Items.add("Fill Treatment Form");
    menu_Items.add("View Medical History");
    menu_Items.add("View Medication");
    menu_Items.add("View Diet");
    menu_Items.add("View First Aid");
    menu_Items.add("Look Up Map");
    menu_Items.add("Account Settings");
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menu_Items);
    lv.setAdapter(arrayAdapter);


    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

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

        }

    });

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg1, View view, int position, long id) {

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

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg2, View view, int position, long id) {

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

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg3, View view, int position, long id) {

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

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg4, View view, int position, long id) {

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

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg5, View view, int position, long id) {

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

   lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg6, View view, int position, long id) {

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

当我点击任何listview项目时,会打开Accounte Settings Activity。 我是新手,所以请不要判断我的编码:)

5 个答案:

答案 0 :(得分:2)

列表视图只能有一个OnItemClickListener,所以除了最后一个之外都会被丢弃。这就是SettingsActivity开放的原因,无论你点击哪个项目。

要解决这个问题,请使用一个侦听器,让这个侦听器处理不同的选项。

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg1, View view, int position, long id) {
        switch(position) {
           case 0:
               // start activity 1
               break;
           case 1:
               // start activity 2
               break;
           case 2:
               // start activity 3
               break;
           // more case statements

    }
});

答案 1 :(得分:1)

你是不是在android:onClick="myClickMethod"的布局中添加了listview然后创建了一个同名的方法?在Inthere中,您可以看到确切按下的视图。 listView也只有一个onClickListener; - )

干杯

答案 2 :(得分:1)

尝试向点击侦听器添加switch语句

private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
    lv = (ListView) findViewById(R.id.listView1);
    // Instanciating an array list (you don't need to do this, you already have yours)
    ArrayList<String> menu_Items = new ArrayList<String>();
    menu_Items.add("Fill Treatment Form");
    menu_Items.add("View Medical History");
    menu_Items.add("View Medication");
    menu_Items.add("View Diet");
    menu_Items.add("View First Aid");
    menu_Items.add("Look Up Map");
    menu_Items.add("Account Settings");
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menu_Items);
    lv.setAdapter(arrayAdapter);


    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        switch(position){
          case 0:
        Intent i = new Intent(getApplicationContext(), FillTreatmentActivity.class);
                startActivity(i);
        break;
          case 1:
        Intent i = new Intent(getApplicationContext(), MedicalHistoryActivity.class);
            startActivity(i);
        break;
          case 2:
        Intent i = new Intent(getApplicationContext(), MedicationActivity.class);
            startActivity(i);
        break;
          case 3:
        Intent i = new Intent(getApplicationContext(),DietActivity.class);
                startActivity(i);
        break;
          case 4:
        Intent i = new Intent(getApplicationContext(),FirstAidActivity.class);
                startActivity(i);
        break;
          case 5:
        Intent i = new Intent(getApplicationContext(),MapActivity.class);
                startActivity(i);
        break;
          case 6:
        Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
                startActivity(i);
        break;

        }


        }

    });
}

答案 3 :(得分:0)

这是做到这一点的方法;我做错了。 谢谢你的帮助。 和平/

private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
    lv = (ListView) findViewById(R.id.listView1);
    // Instanciating an array list (you don't need to do this, you already have yours)
    ArrayList<String> menu_Items = new ArrayList<String>();
    menu_Items.add("Fill Treatment Form");
    menu_Items.add("View Medical History");
    menu_Items.add("View Medication");
    menu_Items.add("View Diet");
    menu_Items.add("View First Aid");
    menu_Items.add("Look Up Map");
    menu_Items.add("Account Settings");
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menu_Items);
    lv.setAdapter(arrayAdapter);


    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        switch(position){
          case 0:
        Intent i = new Intent(getApplicationContext(), FillTreatmentActivity.class);
                startActivity(i);
        break;
          case 1:
        Intent j = new Intent(getApplicationContext(), MedicalHistoryActivity.class);
            startActivity(j);
        break;
          case 2:
        Intent k = new Intent(getApplicationContext(), MedicationActivity.class);
            startActivity(k);
        break;
          case 3:
        Intent l = new Intent(getApplicationContext(),DietActivity.class);
                startActivity(l);
        break;
          case 4:
        Intent m = new Intent(getApplicationContext(),FirstAidActivity.class);
                startActivity(m);
        break;
          case 5:
        Intent n = new Intent(getApplicationContext(),MapActivity.class);
                startActivity(n);
        break;
          case 6:
        Intent o = new Intent(getApplicationContext(),SettingsActivity.class);
                startActivity(o);
        break;

        }


        }

    });

答案 4 :(得分:0)

您需要在Listener上创建,它将处理所有列表。像这样:

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg6, View view, int position, long id) {
swith(position){
        case 1:
        Intent i = new Intent(getApplicationContext(), FillTreatmentActivity.class);
            startActivity(i);
            break;
        case 2:
        ...
        }
        }
    });

或者您可以扩展类ArrayAdapter并为每个元素实现单个侦听器