2013-03-06 50 views
0

工作,这是我的可扩展列表视图:NotifyDataSetChanged不ExpandableListView

public class SnatchView extends ExpandableListView { 
public SnatchAdt m_adt; 

public FileBrowser fileBrowser; 

public SnatchView(Context context) { 
    super(context); 
    m_adt = new SnatchAdt(context, this); 
} 

public SnatchView(Context context, AttributeSet attrs) { 
    super(context); 
    m_adt = new SnatchAdt(context, this); 
} 

public void start() { 
    setAdapter(m_adt); 
    m_adt.start(); 
    setBackgroundColor(SnatchAdt.clr_back); 
    setGroupIndicator(null);// getResources().getDrawable(R.drawable.folder_icon) 
} 

public void edit() { 
    m_adt.notifyDataSetChanged(); 
    m_adt.notifyDataSetInvalidated(); 
} 

public String getChildPath(int group, int child) { 
    return m_adt.getChildPath(group, child); 
} 

public boolean getChildDelete(int group, int child) { 
    return m_adt.getChildDelete(group, child); 
} 

public void close() { 
    m_adt.close(); 
} 
} 

这是我的适配器的更多revelant部分:

public class SnatchAdt extends BaseExpandableListAdapter implements ExpandableListAdapter { 
private final String TAG = SnatchAdt.class.getSimpleName(); 

protected static int clr_back = 0xff356AA0;// 0xFFCCCCCC; 

protected int clr_text = 0xffffffff;// 0xFF000044; 

private SnatchView snatchView; 

public class SnatchItem { 
    public String m_path; 

    public String m_name; 

    public boolean toDelete = false; 

    LinearLayout m_view; 
} 

public class SnatchGroup { 
    private Vector<SnatchItem> m_items = new Vector<SnatchItem>(); 

    public SnatchItem get(int index) { 
     return m_items.get(index); 
    } 

    public int get_count() { 
     return m_items.size(); 
    } 

    public void add_item(String path, String name, boolean toDelete) { 

     final SnatchItem item = new SnatchItem(); 
     item.m_name = name; 
     item.m_path = path; 
     item.toDelete = toDelete; 
     item.m_view = new LinearLayout(m_context); 
     Bitmap myBitmap = BitmapFactory.decodeResource(m_context.getResources(), R.drawable.file_icon); 

     ImageView v = new ImageView(m_context); 
     v.setImageBitmap(myBitmap); 

     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(50, 0, 0, 0); 
     v.setLayoutParams(lp); 
     final CheckBox cb = new CheckBox(m_context); 
     cb.setPadding(10, 10, 10, 10); 
     cb.setFocusable(false); 
     cb.setFocusableInTouchMode(false); 

     cb.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (cb.isChecked()) { 
        item.toDelete = true; 
        snatchView.smoothScrollBy(1, 1); 
       } else { 
        item.toDelete = false; 
        snatchView.smoothScrollBy(1, 1); 
       } 
       // snatchView.invalidateViews(); 
       // notifyDataSetChanged(); 
       snatchView.edit(); 
       // snatchView.post(new Runnable() { 
       // @Override 
       // public void run() { 
       // notifyDataSetChanged(); 
       // } 
       // }); 
      } 
     }); 

     TextView view = new TextView(m_context); 
     view.setText(name); 
     view.setBackgroundColor(clr_back); 
     view.setTextSize(18); 
     view.setTextColor(clr_text); 
     view.setPadding(20, 0, 0, 0); 
     item.m_view.addView(cb); 
     item.m_view.addView(v); 
     item.m_view.addView(view); 
     item.m_view.setPadding(12, 20, 0, 20); 
     item.m_view.setBackgroundColor(clr_back); 
     m_items.add(item); 
     item.m_view.setId(clr_back); 
    } 

    String m_path; 

    LinearLayout m_view; 
} 

private Vector<SnatchGroup> m_groups = new Vector<SnatchGroup>(); 

private DataSetObserver m_obs; 

private Context m_context; 

private boolean canceling = false; 

private Handler m_hand_ui = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     if (m_groups != null) { 
      m_groups.add((SnatchGroup) msg.obj); 
     } 
     if (m_obs != null) { 
      m_obs.onChanged(); 
     } 
     super.handleMessage(msg); 
    } 
}; 

private Thread m_thread = new Thread() { 
    @Override 
    public void run() { 
     set_group_root(); 
    } 
}; 

public SnatchAdt(Context ctx, SnatchView snatchView) { 
    m_context = ctx; 
    this.snatchView = snatchView; 
} 

private synchronized Object get_child(int groupPosition, int childPosition) { 
    return m_groups.get(groupPosition).get(childPosition); 
} 

private synchronized String get_child_path(int groupPosition, int childPosition) { 
    return m_groups.get(groupPosition).get(childPosition).m_path; 
} 
} 

现在,它不坏,我只移动后列表视图。此外,我拨打snatchView.edit();的代码不会被调用,除非我移动listview。有没有人有一个ideea如何使代码工作? 我也尝试调用invalidate,notifyDataSetChanged,notifyDataSetInvalidated,在runOnUIThread或单独的线程中调用notifyDataSetChange,但仍然没有运气。

回答

-3

我已将Expandable ListView更改为正常的ListView,并将复选框的焦点和setEnabled设置为false

+1

这不是正确的解决方案。你不应该把它标记为正确的。答案是错误的 – Pallavi 2014-07-23 08:44:03