【问题标题】:Android transfer bundles and place objects in listviewAndroid 传输捆绑包并在列表视图中放置对象
【发布时间】:2011-07-10 13:28:41
【问题描述】:

我想创建一个列表视图,其中填充了我通过捆绑转移到此活动的项目。我对此完全陌生,所以我不知道如何。这是我到目前为止的位置:

    public class Viewer extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    super.onCreate(savedInstanceState);
    Bundle b = getIntent().getExtras();
    final String name = b.getString("name");
    Bundle a = getIntent().getExtras();
    final String number = a.getString("number");

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, DETAILS)); 
    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
        // When clicked, show a toast with the TextView text 
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
            Toast.LENGTH_SHORT).show(); 
      } 
    }); 
} 
static final String[] DETAILS = new String[] { 
    name, number
  }; 
} 

我想让字符串“name”和“number”出现在列表中,但是当我将它们放在

时出现错误“无法解析为变量”
    static final String[] DETAILS = new String[] { 
    name, number
  }; 
}

【问题讨论】:

    标签: android arrays listview bundle


    【解决方案1】:

    在加载类时创建static final String[] DETAILS。但是 final String name = b.getString("name"); 将在稍后调用 onCreate() 时执行。

    这应该可行:

    static String[] DETAILS;
    
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
    
        super.onCreate(savedInstanceState);
        Bundle b = getIntent().getExtras();
        final String name = b.getString("name");
        Bundle a = getIntent().getExtras();
        final String number = a.getString("number");
    
    
        DETAILS = new String[] {name, number};
    
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, DETAILS)); 
        ListView lv = getListView(); 
        lv.setTextFilterEnabled(true); 
        lv.setOnItemClickListener(new OnItemClickListener() { 
          public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
            // When clicked, show a toast with the TextView text 
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
                Toast.LENGTH_SHORT).show(); 
          } 
        }); 
    } 
    

    【讨论】:

    • 感谢您的回答。这是有道理的,但我得到了 FC ......我显然用 ArrayAdapter 得到了 NullPointerException。有什么想法吗?
    • 如果我改变 DETAILS = new String[] {name, number};到 DETAILS = new String[] {"something"};,它有效。那么一定是向DETAILS数组传输数据有问题?
    • 是的。如果 a.getString(xy) 返回 null,则 ArrayAdapter 将创建 NullPointerException,因为 DETAILS 数组中有一个 null 值。
    • 这很奇怪。从 logcat 发布日志。
    • 好的,它只适用于数字。我已经把我的捆绑代码放在上面。这是正确的方法吗?
    猜你喜欢
    • 2013-11-24
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多