如何处理在Android中实现惰性列表功能的动态列表?
我正在开发一个与聊天相关的应用程序。我的数据库中有大约4500个条目,这个数据库显示了用户图像和聊天信息。我在代码文件下面实现了更多时间显示列表中加载的任何建议的所有信息 代码文件:如何处理在Android中实现惰性列表功能的动态列表?,android,lazylist,Android,Lazylist,我正在开发一个与聊天相关的应用程序。我的数据库中有大约4500个条目,这个数据库显示了用户图像和聊天信息。我在代码文件下面实现了更多时间显示列表中加载的任何建议的所有信息 代码文件: // Handle the messages and post messages to list view private void handlerMessage(final String messageType, final int dis) { myProgress
// Handle the messages and post messages to list view
private void handlerMessage(final String messageType, final int dis)
{
myProgressDialog = ProgressDialog.show(Shout1.this,"","Loading...",true);
new Thread()
{
public void run()
{
try {
String xml;
Userid=getSharedPreferences("MyLoginInfo", 0).getString("user_id", "");
if(messageType.length()==0)
{
xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>"+Userid+"</LoginUserID></spGetUserMessages>";
messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));
Message msg = myHandler.obtainMessage();
myHandler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myProgressDialog.dismiss();
}
}.start();
}
// Handle the messages
private Handler myHandler = new Handler()
{
public void handleMessage(Message msg)
{
Log.v("Shout=====","inhandleMessage ======="+messages.size());
if(messages.size()==0)
{((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
Toast.makeText(Shout1.this, "No messages in that Location", Toast.LENGTH_SHORT).show();
}
else
{
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
addElements(messages);
}
}
};
//add messagess to List
protected void addElements(final List<MessageClass> messages2)
{
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
LinearLayout ll5=new LinearLayout(this);
ll5.setBackgroundColor(Color.WHITE);
ll5.setOrientation(1);
for(int i=0;i<messages2.size();i++)
{
LinearLayout ll1=new LinearLayout(this);
ll1.setPadding(0, 3, 0, 0);
ll1.setBackgroundResource(R.drawable.tabmessage);
ll1.setGravity(Gravity.CENTER);
final TextView messageid = new TextView(this);
messageid.setText(messages2.get(i).getmessageId());
final TextView targetid = new TextView(this);
targetid.setText(messages2.get(i).getUserid());
final TextView targetMessage = new TextView(this);
targetMessage.setText(messages2.get(i).getmessage());
LinearLayout layoutmsg=new LinearLayout(this);
layoutmsg.setClickable(true);
layoutmsg.setOrientation(1);
layoutmsg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ImageView msgImage=new ImageView(this);
final TextView msgimageInfo=new TextView(this);
try
{
String photoXml="<spGetMessagePhoto><MessageID>"+messages2.get(i).getmessageId()+"</MessageID></spGetMessagePhoto>";
String photoInfoFromDB1=new DataParsingComm().ILGetMessageImage(photoXml);
msgimageInfo.setText(photoInfoFromDB1);
if(photoInfoFromDB1.equalsIgnoreCase("anytype{}")|| photoInfoFromDB1==null)
{
msgImage.setVisibility(8);
}
else
{
byte[] imgArry= Base64.decode(photoInfoFromDB1);
Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);
msgImage.setImageBitmap(bit);
}
}catch (Exception ee) {
// TODO: handle exception
ee.printStackTrace();
}
final TextView msg=new TextView(this);
msg.setText(messages2.get(i).getmessage());
final TextView msg1=new TextView(this);
if(messages2.get(i).getmessage().length()>60)
msg1.setText(messages2.get(i).getmessage().substring(0, 60)+"...");
else
msg1.setText(messages2.get(i).getmessage());
msg1.setTextColor(Color.BLACK);
msg1.setTypeface(Typeface.MONOSPACE);
msg1.setTextSize(13);
msg1.setWidth(250);
layoutmsg.addView(msgImage);
layoutmsg.addView(msg1);
ImageView PersonImage=new ImageView(this);
final TextView imageInfo=new TextView(this);
try{
//get the image based on user ID
String photoXml="<spGetUserPhoto><UserID>"+messages2.get(i).getUserid()+"</UserID></spGetUserPhoto>";
Log.v("check","photoXml.."+photoXml);
String photoInfoFromDB=new DataParsingComm().ILGetImage(photoXml);
Log.v("photoInfoFromDB","photoInfoFromDB.."+photoInfoFromDB);
imageInfo.setText(photoInfoFromDB);
if(photoInfoFromDB.equalsIgnoreCase("anytype{}")||photoInfoFromDB.equals(null) )
{
PersonImage.setImageResource(R.drawable.person);
}
else
{
byte[] imgArry= Base64.decode(photoInfoFromDB);
Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);
Log.v("Shout","Image Lengthhhhhhhhhhhhh"+imgArry.length);
PersonImage.setImageBitmap(bit);
}
}catch (Exception ee) {
ee.printStackTrace();
}
final TextView personName=new TextView(this);
if(messages2.get(i).getUname().equalsIgnoreCase("noname"))
personName.setText("");
else
personName.setText(messages2.get(i).getUname());
personName.setTextColor(Color.rgb(0, 102, 51));
LinearLayout layoutPersonImage=new LinearLayout(this);
layoutPersonImage.setOrientation(1);
layoutPersonImage.setGravity(Gravity.CENTER);
layoutPersonImage.addView(PersonImage);
layoutPersonImage.addView(personName);
ll1.addView(layoutPersonImage);
ll1.addView(layoutmsg);
ll5.addView(ll1);
}
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(ll5);
}
//处理消息并将消息发布到列表视图
私有void handlerMessage(最终字符串messageType,最终int-dis)
{
myProgressDialog=ProgressDialog.show(Shout1.this,“,“Loading…”,true);
新线程()
{
公开募捐
{
试一试{
字符串xml;
Userid=getSharedReferences(“MyLoginInfo”,0).getString(“user_id”,0);
if(messageType.length()==0)
{
xml=”“+Userid+”;
messages=parse.getUserMessages(dataparsing.ILGetUserMessages(xml));
Message msg=myHandler.obtainMessage();
发送消息(msg);
}
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
myProgressDialog.discouse();
}
}.start();
}
//处理消息
私有处理程序myHandler=新处理程序()
{
公共无效handleMessage(消息消息消息)
{
Log.v(“Shout===”,“inhandleMessage===”+messages.size());
if(messages.size()==0)
{((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
Toast.makeText(Shout1.this,“该位置没有消息”,Toast.LENGTH_SHORT.show();
}
其他的
{
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
附录(信息);
}
}
};
//将消息添加到列表中
受保护的无效补遗(最终列表消息2)
{
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
LinearLayout ll5=新的LinearLayout(本);
ll5.setBackgroundColor(颜色:白色);
ll5.定向(1);
对于(int i=0;i60)
msg1.setText(messages2.get(i.getmessage().substring(0,60)+“…”);
其他的
msg1.setText(messages2.get(i.getmessage());
msg1.setTextColor(Color.BLACK);
msg1.setTypeface(Typeface.MONOSPACE);
msg1.setTextSize(13);
msg1.设置宽度(250);
layoutmsg.addView(msgImage);
layoutmsg.addView(msg1);
ImageView PersonImage=新的ImageView(此);
最终文本视图图像信息=新文本视图(此);
试一试{
//根据用户ID获取图像
字符串photoXml=“+messages2.get(i).getUserid()+”;
Log.v(“check”、“photoXml..”+photoXml);
字符串photoInfoFromDB=newdataparsingcomm().ILGetImage(photoXml);
Log.v(“photoInfoFromDB”、“photoInfoFromDB..”+photoInfoFromDB);
imageInfo.setText(photoInfoFromDB);
if(photoInfoFromDB.equalsIgnoreCase(“anytype{}”)| | photoInfoFromDB.equals(null))
{
PersonImage.setImageResource(R.drawable.person);
}
其他的
{
字节[]imgArry=Base64.decode(photoInfoFromDB);
位图位=位图工厂.decodeByteArray(imgArry,0,imgArry.length);
Log.v(“呼喊”、“图像长度”+imgArry.length);
PersonImage.setImageBitmap(位);
}
}捕获(异常ee){
ee.printStackTrace();
}
最终文本视图人名=新文本视图(此);
if(messages2.get(i.getUname().equalsIgnoreCase(“noname”))
personName.setText(“”);
其他的
setText(messages2.get(i.getUname());
setTextColor(Color.rgb(0,102,51));
LinearLayout layoutPersonImage=新的LinearLayout(此);
LayoutPersonImageSetOrientation(1);
布局人像。设置重力(重力。中心);
layoutPersonImage.addView(PersonImage);
layoutPersonImage.addView(personName);
ll1.添加视图(layoutPersonImage);
ll1.addView(layoutmsg);
ll5.addView(ll1);
}
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(ll5);
}
您可以将图像存储在高速缓存中并访问它。查看此示例:
您可以将图像存储在高速缓存中并访问它。查看此示例:
如果我正确理解您的代码,您将使用线性布局来显示消息,每次收到新消息时都会重新创建整个布局。这是一种非常低效的方法 您应该使用ListView显示消息列表,并使用自定义适配器加载消息。Android处理滚动出视图的项目的回收:它们被重用以提高内存效率。无需在收到新消息时手动重新加载每个项,只需将新项添加到集合中,并在适配器上调用notifyDatasetChanged()
请参阅《Android开发指南》中关于和的文章。如果我正确理解了您的代码,您将使用线性布局来显示消息,每次收到新消息时都会重新创建整个布局。这是一种非常低效的方法 您应该使用ListView显示消息列表,并使用自定义适配器加载消息。Android处理滚动出视图的项目的回收:它们被重用以提高内存效率。无需在收到新消息时手动重新加载每个项,只需将新项添加到集合中,并在适配器上调用notifyDatasetChanged() 请参阅Android开发指南中关于和的文章