android中ListView資料重新整理時的同步方法

來源:互聯網
上載者:User

android中ListView資料重新整理時的同步方法

   本文執行個體講述了android中ListView資料重新整理時的同步方法。分享給大家供大家參考。具體實現方法如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

public class Main extends BaseActivity {

private static final String TAG = "tag";

private static final int STATUS_CHANGE = 0;

ExpandableListView mElv;

ArrayList<GroupInfo> mGroupArray;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mElv = (ExpandableListView) findViewById(R.id.contact_list);

mStatus = (TextView) findViewById(R.id.setStatus);

mGroupArray = getIntent().getParcelableArrayListExtra("groupArray");// => 取資料

mExpandableAdapter = new ExpandableAdapter(this, Main.this);

mElv.setAdapter(mExpandableAdapter);

// 非同步對比伺服器分組和本地分組

HandlerThread handlerThread = new HandlerThread("handler_thread");

handlerThread.start();

UpdateGroupHandler myHandler = new UpdateGroupHandler(

handlerThread.getLooper());

Message message = myHandler.obtainMessage();

message.sendToTarget();

mHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case STATUS_CHANGE:

// 處理UI更新等操作

updateUI();

break;

}

};

};

}

/**

* 發送訊息更新UI

*/

private void sendMessageToUpdateUI() {

Message msg = new Message();

msg.what = STATUS_CHANGE;

mHandler.sendMessage(msg);

// 向Handler發送訊息,更新UI

}

private void updateUI() {

// 詳細的更新

mExpandableAdapter.notifyDataSetChanged();

// 更新ExpandableListView

}

/**

* 非同步重新整理分組的handler

*

* @author administrator

*

*/

class UpdateGroupHandler extends Handler {

public UpdateGroupHandler() {

}

public UpdateGroupHandler(Looper looper) {

super(looper);

}

@Override

public void handleMessage(Message msg) {

ContactsManagerDbAdapter dbAdapter = new ContactsManagerDbAdapter(

Main.this);

dbAdapter.open();

// =>doSomeThing...

mGroupArray = groupList;

System.out.println("========資料更新後,重新整理listview=========");

sendMessageToUpdateUI();

}

}

private class ExpandableAdapter extends BaseExpandableListAdapter {

Activity activity;

LayoutInflater layoutInflater;

public ExpandableAdapter(Activity a, Context context) {

activity = a;

layoutInflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public Object getChild(int groupPosition, int childPosition) {

return mGroupArray.get(groupPosition).getChildList()

.get(childPosition);

}

public long getChildId(int groupPosition, int childPosition) {

return childPosition;

}

public int getChildrenCount(int groupPosition) {

return mGroupArray.get(groupPosition).getChildList().size();

}

public View getChildView(int groupPosition, int childPosition,

boolean isLastChild, View convertView, ViewGroup parent) {

// .....

return vi;

}

public Object getGroup(int groupPosition) {

return mGroupArray.get(groupPosition);

}

public int getGroupCount() {

return mGroupArray.size();

}

public long getGroupId(int groupPosition) {

return groupPosition;

}

public View getGroupView(int groupPosition, boolean isExpanded,

View convertView, ViewGroup parent) {

GroupInfo groupInfo = mGroupArray.get(groupPosition);

String string = groupInfo.getName();

convertView = (View) layoutInflater.inflate(R.layout.group_layout,

null);

final TextView textView = (TextView) convertView

.findViewById(R.id.groupName);

if (textView != null) {

textView.setText(string);

}

return convertView;

}

public boolean hasStableIds() {

return true;

}

public boolean isChildSelectable(int groupPosition, int childPosition) {

return true;

}

}

}

  代碼只是提取的部分,應該沒多大影響.

  上面集合mGroupArray存在資料共用,測試多次發現報錯有兩種:

  =>1.java.lang.IndexOutOfBoundsException: Invalid location 3, size is 3

  =>2.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.

  第一個問題,資料同步問題,我弄了下沒解決.

  第二個問題,改變適配器Adapter內容時不要在後台線程中,必須在UI線程中處理

  我將上面子線程UpdateGroupHandler裡的資料更新利用handler提取到了主線程賦值

  ?

1

Message.obj = groupList;

  額,改好後測試多次,發現這兩問題都解決了,發現還是對handler理解的不夠.

  發下更改的程式碼片段:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mElv = (ExpandableListView) findViewById(R.id.contact_list);

mStatus = (TextView) findViewById(R.id.setStatus);

mGroupArray = getIntent().getParcelableArrayListExtra("groupArray");

// => 取資料

mExpandableAdapter = new ExpandableAdapter(this, Main.this);

mElv.setAdapter(mExpandableAdapter);

// 非同步對比伺服器分組和本地分組

HandlerThread handlerThread = new HandlerThread("handler_thread");

handlerThread.start();

UpdateGroupHandler myHandler = new UpdateGroupHandler(

handlerThread.getLooper());

Message message = myHandler.obtainMessage();

message.sendToTarget();

mHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case STATUS_CHANGE:

// 處理UI更新等操作

updateUI(msg.obj);

break;

}

};

};

}

/**

* 發送訊息更新UI

*/

private void sendMessageToUpdateUI(ArrayList<GroupInfo> groupList) {

Message msg = new Message();

msg.what = STATUS_CHANGE;

msg.obj = groupList;

mHandler.sendMessage(msg);

// 向Handler發送訊息,更新UI

}

@SuppressWarnings("unchecked")

private void updateUI(Object obj) {

// 詳細的更新

mGroupArray = (ArrayList<GroupInfo>) obj;

mExpandableAdapter.notifyDataSetChanged();

// 更新ExpandableListView

}

/**

* 非同步重新整理分組的handler

*

* @author administrator

*

*/

class UpdateGroupHandler extends Handler {

public UpdateGroupHandler() {

}

public UpdateGroupHandler(Looper looper) {

super(looper);

}

@Override

public void handleMessage(Message msg) {

ContactsManagerDbAdapter dbAdapter = new ContactsManagerDbAdapter(

Main.this);

dbAdapter.open();

// =>doSomeThing...

System.out.println("========資料更新後,重新整理listview=========");

sendMessageToUpdateUI(groupList);

}

}

  希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.