ListView嵌套ListView的数据处理更新问题

天堂的键盘 发布于 2016/05/18 15:21
阅读 1K+
收藏 0

解读下一代网络:算力网络正从理想照进现实!>>>

有个需求需要用到listview嵌套listview,数据加载显示完成后,点击子listview的item时,报错:log如下
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131297320, class android.widget.ListView) with Adapter(class com.zhkj.zhpf.prodbrowse.AllMarketFloorActivity$SecondItemAdapter)]

百度了下,调了一个多小时没调好,子listview的数据是在父listview中getview遍历的时候放进去的,求解是否adapter数据通知刷新的时候没写好,代码如下:

public class AllMarketFloorActivity extends Activity implements OnClickListener{
private Context context;
ItemFloorAdapter flooradapter;
SecondItemAdapter SecondAdapter;
String title ;
ListView lv_all_floor,lv_item_floor;
TextView tv_all_floor,tv_floor_back;
EditText et_allfloor_search;
String[] floorarray;
ArrayList<String[]> floorlist=new ArrayList<String[]>();
Intent intent=new Intent();
int[] colors=new int[]{R.color.floor1,R.color.floor2,R.color.floor3,R.color.floor4,R.color.floor5,R.color.floor6,R.color.floor7,R.color.floor8,R.color.floor9,R.color.floor10};
     Handler handler=new Handler(){
    public void handleMessage(android.os.Message msg) {
    switch (msg.what) {
case 1:
if(SecondAdapter!=null){
SecondAdapter.notifyDataSetChanged();
}
  if(flooradapter!=null){
  flooradapter.notifyDataSetChanged();
  }
break;
default:
break;
}
    };
     };

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_market_allfloor);
    GlobalFunction.activityList.add(this);
    context=this;
    title = getIntent().getStringExtra("title");
    System.out.println("title  "+title);
    init();
    }
     private void init(){
    et_allfloor_search=(EditText)findViewById(R.id.et_allfloor_search);
    et_allfloor_search.setOnClickListener(this);
    lv_all_floor=(ListView)findViewById(R.id.lv_all_floor);
    tv_all_floor=(TextView)findViewById(R.id.tv_all_floor);
    tv_floor_back=(TextView)findViewById(R.id.tv_floor_back);
    tv_floor_back.setOnClickListener(this);
    if(title!=null){
    tv_all_floor.setText(title+"楼层");
    }
    tv_floor_back=(TextView)findViewById(R.id.tv_floor_back);
   
         flooradapter=new ItemFloorAdapter();
         lv_all_floor.setAdapter(flooradapter);
   
     }
     //外层listview 适配
     class ItemFloorAdapter extends BaseAdapter{
         LayoutInflater  inflate;
         int location;
         public ItemFloorAdapter(){
        super();
        notifyDataSetChanged();
         }
@Override
public int getCount() {
return MarketDetailActivity.MarketInfor_floors.size(); 
}


@Override
public Object getItem(int position) {
return MarketDetailActivity.MarketInfor_floors.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder vh=null;
if(view==null){
if(inflate==null){
inflate=(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
}
vh=new ViewHolder();
view = inflate.inflate(R.layout.item_all_floor,null);
vh.tv_floor_name=(TextView)view.findViewById(R.id.tv_floor_name);
vh.lv_item_floor=(ListView)view.findViewById(R.id.lv_item_floor);
vh.lv_item_floor.setTag(position);
vh.lv_item_floor.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int positions, long arg3) {
location =Integer.parseInt(arg0.getTag().toString());
System.out.println("当前点击位置 "+location);
intent.putExtra("categoryId", "");
intent.putExtra("marketId", "");
intent.putExtra("marketName", "");
intent.putExtra("storeId", "");
intent.putExtra("isSearch", "1");
intent.putExtra("keyWord", floorlist.get(location)[positions]);
intent.setClass(context, SearchProdActivity.class);
startActivity(intent);
}

});
view.setTag(vh);
}else{
vh=(ViewHolder)view.getTag();
}
//适配器数据配置
if (!(MarketDetailActivity.MarketInfor_floors.get(position).getBusin()).equals("")) {
String floors = MarketDetailActivity.MarketInfor_floors.get(position).getBusin();
floorarray=null;
//截取楼层数据填充到数组
floorarray = floors.split("\\|");
//数组添加到arraylist
floorlist.add(floorarray);
//item的listvew数据直接使用数组的
SecondAdapter=new SecondItemAdapter();
vh.lv_item_floor.setAdapter(SecondAdapter);
//通知刷新adapter
handler.sendEmptyMessage(1);
//设置listview全部展开
Utility.setListViewHeightBasedOnChildren(vh.lv_item_floor);
}
vh.tv_floor_name.setText(MarketDetailActivity.MarketInfor_floors.get(position).getCode());
//颜色循环
if(MarketDetailActivity.MarketInfor_floors.size()>colors.length){
      String positions=String.valueOf(position).substring(String.valueOf(position).length()-1);
      int po=Integer.valueOf(positions);
      vh.tv_floor_name.setBackgroundResource(colors[po]);
}else{
vh.tv_floor_name.setBackgroundResource(colors[position]);
}
return view;
}
    class ViewHolder{
    TextView tv_floor_name;
    ListView lv_item_floor;
    }
     }
     //第二个listview适配器
     class SecondItemAdapter extends BaseAdapter{
         LayoutInflater  inflate;
         public SecondItemAdapter() {
      super();
      notifyDataSetChanged();
      }
@Override
public int getCount() {
return floorarray.length; 
}


@Override
public Object getItem(int position) {
return floorarray[position];
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolders vh=null;
if(view==null){
vh=new ViewHolders();
if(inflate==null){
inflate=(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
}
view = inflate.inflate(R.layout.item_floor_second,null);
vh.tv_floor_storename=(TextView)view.findViewById(R.id.tv_floor_storename);
view.setTag(vh);
}else{
vh=(ViewHolders)view.getTag();
}
vh.tv_floor_storename.setText(floorarray[position]);
return view;
}
    class ViewHolders{
    TextView tv_floor_storename;
    }
     }
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_floor_back:
finish();
break;
case R.id.et_allfloor_search:
Intent intent = new Intent();
intent.putExtra("keyWord", "");
intent.setClass(context, SearchActivity.class);
startActivity(intent);
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}

加载中
返回顶部
顶部