android开发,向listView添加数据,要求序号从1开始自动增加,删除其中一条自减怎么实现?

android开发,向listView添加数据,要求序号从1开始自动增加,删除其中一条自减怎么实现?,第1张

按照我对问题的理解,解决方法如下:

在ListView的adapter中的getView()方法里给序号赋值就可以了,getView()方法不是有个int型参数是position嘛,你给序号赋值为position+1就可以了。不管你像listview添加数据还是删除数据,总是要重新刷新adapter的,只要刷新adapter就会走getView()方法,就能更新序号了。

不知道是不是你想要的回答。

把20条数据放在一个List集合中,自定义一个适配器,收到数据后判断,然后调用list的remove方法移除,再调用add方法添加数据,然后调用adapter.notifyDataSetChanged()这样就会更新为集合中最新的数据

以保存场景为例:

public static String SceneList2String(List SceneList)

throws IOException {

// 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()

// 然后将得到的字符数据装载到ObjectOutputStream

ObjectOutputStream objectOutputStream = new ObjectOutputStream(

byteArrayOutputStream)

// writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它

objectOutputStream.writeObject(SceneList)

// 最后,用Base64.encode将字节文件转换成Base64编码保存在String中

String SceneListString = new String(Base64.encode(

byteArrayOutputStream.toByteArray(), Base64.DEFAULT))

// 关闭objectOutputStream

objectOutputStream.close()

return SceneListString

}

@SuppressWarnings("unchecked")

public static List String2SceneList(String SceneListString)

throws StreamCorruptedException, IOException,

ClassNotFoundException {

byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),

Base64.DEFAULT)

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(

mobileBytes)

ObjectInputStream objectInputStream = new ObjectInputStream(

byteArrayInputStream)

List SceneList = (List) objectInputStream

.readObject()

objectInputStream.close()

return SceneList

}

最后通过

SharedPreferences mySharedPreferences= getSharedPreferences("scenelist", Context.MODE_PRIVATE)

Editor edit = mySharedPreferences.edit()

try {

String liststr = Utils.SceneList2String(MyApp.scenesList)

edit.putString(Constants.SCENE_LIST,liststr)

edit.commit()

} catch (IOException e) {

e.printStackTrace()

}

SharedPreferences sharedPreferences= getActivity().getSharedPreference ("scenelist", Context.MODE_PRIVATE)

String liststr = sharedPreferences.getString(Constants.SCENE_LIST, "")

try {

showSceneList = Utils.String2SceneList(liststr)

} catch (StreamCorruptedException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} catch (ClassNotFoundException e) {

e.printStackTrace()

}

进行保存获取。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11618135.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存