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) {

            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);
        }
    }); 
}

当我单击任何列表视图项目帐户设置活动打开。我是新手,所以请不要评判我的编码:)

4

5 回答 5

2

一个列表视图只能有一个OnItemClickListener,所以除了最后一个之外的所有东西都被扔掉了。这就是为什么SettingsActivity无论你点击哪个项目都是 openend 的原因。

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

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

    }
});
于 2013-03-04T15:32:52.357 回答
1

为什么不添加android:onClick="myClickMethod"布局,listview然后创建一个具有相同名称的方法?在那里,您可以看到确切的视图已被按下。listView 也只有一个onClickListener;-)

干杯

于 2013-03-04T15:34:32.450 回答
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;

        }


        }

    });
}
于 2013-03-04T15:37:48.450 回答
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;

        }


        }

    });
于 2013-03-04T15:53:28.613 回答
0

您需要在将处理所有列表的侦听器上创建。像这样的东西:

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:
        ...
        }
        }
    });

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

于 2013-03-05T07:52:12.447 回答