2011-07-20 56 views
1

我的要求是:當我點擊列表行時,它需要去其他活動。當我運行這個代碼時,我得到了NullPointerException; (我標誌着註釋錯誤的地方)Android多列ListView

這是我的代碼:

ArrayList<Object> routeList = getWmRoute(); 
ArrayList<String> routhPath = new ArrayList<String>(); 
ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>(); 

for(int i = 0; i<routeList.size();i++){ 
    HashMap<String, String> map = new HashMap<String, String>(); 
    map.put("RetailerCode", ((WMRoute) routeList.get(i)).getDescription()); 
    map.put("RetailerName", ((WMRoute) routeList.get(i)).getBusinessUnit()); 
    alist.add(map); 
    } 

ListView list=getListView(); 
sd = new SimpleAdapter(this,alist,R.layout.retalier_rows,new String[]{"RetailerCode","RetailerName"},new int[]{R.id.retailerCode,R.id.retailerName}); 
list.setAdapter(sd); 
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
list.setSelected(true); 
list.setSelection(0); 
list.setTextFilterEnabled(true); 
list.setItemsCanFocus(true); 
list.setTextFilterEnabled(true); 
list.setItemChecked(positions,true); 
keyword = ((WMRoute) routeList.get(0)).getBusinessUnit(); // here Problem 1 place 
//keyword = (String) list.getItemAtPosition(0); // here Problem 1 place 

這是我的聽衆部分:

@Override 


    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Object o = this.getListAdapter().getItem(position); //java.lang.NullPointerException - problem here 2nd place 
     keyword = o.toString(); // have to get first column value 
     positions = position; 
     if(position != 0){ 
      Bundle bundle = new Bundle(); 
      Intent showContent = new Intent(getApplicationContext(),RetailerOptionActivity.class); 
      int postion = position; 
      String aString = Integer.toString(postion); 
      bundle.putString("positon", aString); 
      startActivity(showContent); 

     } 
    } 

當我點擊「OK」,從菜單中選擇,我設置參數;

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case 0: 
      Intent showContent = new Intent(getApplicationContext(),RetailerOptionActivity.class); 
      Bundle bundle = new Bundle(); 
      bundle.putString("RetailerName", keyword); 
      showContent.putExtras(bundle); 
      startActivity(showContent); 
      return true; 
     case 1: 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

這是我的另一項活動:

Bundle bundle = this.getIntent().getExtras(); 
String RetailerName = bundle.getString("RetailerName"); 

setTitle(RetailerName + " - Retailer Option"); // want to get the parameter & set the title name 

我必須設置標題名稱,這是從以前的列表中選擇點擊(選擇的第一列名)採摘。

請幫我..

在此先感謝。

+0

您能發佈錯誤消息嗎?你也可能想重新設定問題的格式,它不清楚實際的問題是什麼......就好像你在屏幕上粘貼了代碼,就是這樣。你想達到什麼目的? – Marty

回答

1

在您的onListItemClick()事件中,您應該使用View v(作爲事件的參數),而不是使用this.getListAdapter().getItem(position)來獲取項目中第一列的值,以獲取代表第一列。例如,如果第一列是textview,則應執行以下操作:

TextView tv = (TextView)v.findViewById(R.id.firstColumnTextView); 
keyword = tv.getText();