【问题标题】:Positioning in android listview在android listview中定位
【发布时间】:2012-06-13 23:04:57
【问题描述】:

我在 android listview 中定位时遇到了一些问题,下一个 Activity 没有加载,而它们都在清单中。

这是我的代码:

package com.WNF;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Actiemenu extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] actiemenu = getResources().getStringArray(R.array.actiemenu);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.actiemenu, R.id.label,     actiemenu));

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

            switch(position) {
            case '0' :            
                Intent intent = new Intent(Actiemenu.this, Acties.class);
                intent.putExtra("extras", position);
                startActivity(intent);
                break;
            case '1' :            
                Intent intent2 = new Intent(Actiemenu.this, Acties2.class);
                intent2.putExtra("extras", position);
                startActivity(intent2);
                break;
        }


      }
    });

}
}

接收 Intent 的代码也存在于 Acties 中

package com.WNF;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class Acties extends Activity{
// aanroepen van een bundle, kan je elke naam geven die je maar wilt, 
//zolang de bundle als de onCreate maar dezelfde naam hebben
@Override  
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        // de setContentView is niets meer dan de gegevens van de
//View ophalen uit de R.layout.naamvandeXML
        // Onthoud goed dat je dezelfde XMLs voor meerdere pagina's 
//kan gebruiken.
        setContentView(R.layout.acties1);

        Intent i = getIntent();
        i.getStringExtra("extras");
   }
}

谁能看到我做错了什么? 在此先感谢:)

【问题讨论】:

  • 我现在要睡觉了 xD 这里是凌晨 1:10,如果有需要我会在明天回答 ;)

标签: android listview android-intent


【解决方案1】:

您的 switch 语句错误地尝试将 integer 位置值与 ASCII 字符 '0''1' 匹配。由于'0' 的ASCII 值为48,'1' 的ASCII 值为49,所以单击前两个列表项时没有任何反应。

你应该去掉单引号,只使用整数:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch(position) {
    case 0: //Instead of '0' 
        Intent intent = new Intent(Actiemenu.this, Acties.class);
        intent.putExtra("extras", position);
        startActivity(intent);
        break;
    case 1: //Instead of '1'
        Intent intent2 = new Intent(Actiemenu.this, Acties2.class);
        intent2.putExtra("extras", position);
        startActivity(intent2);
        break;
}

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多