Android多列ListView

时间:2011-07-20 12:00:18

标签: android listview

我的要求是:当我点击列表行时,需要进行其他活动。当我运行此代码时,我得到 NullPointerException ; (我在评论中标记了错误位置)

这是我的代码:

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

        }
    }

当我从菜单选项中单击“确定”时,我正在设置参数;

  @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

我必须设置标题名称,该名称是从单击的上一个列表选项中选取的(选择的第一列名称)。

请帮帮我..

提前致谢。

1 个答案:

答案 0 :(得分:1)

在onListItemClick()事件中,不应使用this.getListAdapter().getItem(position)来获取项目中第一列的值,而应使用View v作为事件的参数,获取代表第一列的视图。例如,如果第一列是文本视图,则应执行以下操作:

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