android listview JSONArray元素是否重复?
我有问题我的json元素有json数组我从适配器循环了数组,所以当我向下滚动并再次向上滚动时,json数组中附加的元素从2复制到4,然后是8等等。如何解决这个问题,下面是我的代码 此代码中的问题android listview JSONArray元素是否重复?,android,listview,Android,Listview,我有问题我的json元素有json数组我从适配器循环了数组,所以当我向下滚动并再次向上滚动时,json数组中附加的元素从2复制到4,然后是8等等。如何解决这个问题,下面是我的代码 此代码中的问题 //if post has attachments if ( attachments.length() != 0 ) { holder.post_attachmentbox.se
//if post has attachments
if ( attachments.length() != 0 )
{
holder.post_attachmentbox.setVisibility(View.VISIBLE);
//add attachments to post
for(int i = 0; i < attachments.length(); i++)
{
JSONObject attachment = attachments.getJSONObject(i);
//prevent add duplicate attachments
if( attachment.getLong("postid") == p.getLong("postid"))
{
Button attach = new Button(context);
attach.setText(attachment.getString("filename"));
holder.attachment_bit_handler.addView(attach);
}
}
}else
//post not has attachments
{
holder.post_attachmentbox.setVisibility(View.GONE);
}
//如果post有附件
如果(附件.length()!=0)
{
holder.post_attachmentbox.setVisibility(视图可见);
//向帖子添加附件
对于(int i=0;i
完整代码
public class a_ShowThread_pListView extends ArrayAdapter<Object>{
Context context;
private LayoutInflater mInflater;
@SuppressWarnings("rawtypes")
ArrayList ob;
int resource ;
Typeface font_hb ,font_new,font_kufi;
/*================================================
* Setup ListView
*===============================================*/
@SuppressWarnings("unchecked")
public a_ShowThread_pListView (Context context, int resource, @SuppressWarnings("rawtypes") ArrayList objects) {
super(context, resource,objects);
this.context = context;
this.ob = objects;
this.resource = resource;
mInflater = LayoutInflater.from(context);
this.font_hb = Typeface.createFromAsset(context.getAssets(),"fonts/hb.ttf");
this.font_new = Typeface.createFromAsset(context.getAssets(),"fonts/neu.ttf");
this.font_kufi = Typeface.createFromAsset(context.getAssets(),"fonts/kufi.ttf");
}
/*================================================
* Items Counter
*===============================================*/
public int getCount() {
return ob.size();
}
/*================================================
* Item Posistion JSON
*===============================================*/
public JSONObject getItem(JSONObject position) {
return position;
}
/*================================================
* Item Position
*===============================================*/
public long getItemId(int position) {
return position;
}
/*================================================
* Hold Views inside Chant Bit View
*===============================================*/
static class ViewHolder {
TextView postername;
TextView usertitle;
TextView registerdate;
TextView posterposts;
TextView message;
HorizontalScrollView post_attachments_scrollview;
LinearLayout post_attachmentbox;
LinearLayout attachment_bit_handler;
TextView attachment_text;
JSONArray attachments;
}
/*================================================
* Setup Each View raw by raw
*===============================================*/
@SuppressWarnings("unused")
@SuppressLint("ResourceAsColor") public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
JSONObject p = (JSONObject) getItem(position);
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.a_postbit, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.postername = (TextView) convertView.findViewById(R.id.postername); //poster username
holder.usertitle = (TextView) convertView.findViewById(R.id.usertitle); //poster title
holder.registerdate = (TextView) convertView.findViewById(R.id.registerdate); //poster reigster date
holder.posterposts = (TextView) convertView.findViewById(R.id.posterposts); // poster posts counter
holder.message = (TextView) convertView.findViewById(R.id.message); //post message
holder.post_attachments_scrollview = (HorizontalScrollView) convertView.findViewById(R.id.post_attachments_scrollview); // attachments view box
holder.post_attachmentbox = (LinearLayout) convertView.findViewById(R.id.post_attachmentbox); //attachment box hide / show;
holder.attachment_text = (TextView) convertView.findViewById(R.id.attachment_text); //Text Attachment legend
holder.attachment_bit_handler = (LinearLayout) convertView.findViewById(R.id.attachment_bit_handler); //append post attachment to this view
}
else
{
holder = (ViewHolder) convertView.getTag();
}
try {
//add values
holder.postername.setText(p.getString("postusername"));
holder.usertitle.setText(p.getString("usertitle"));
holder.registerdate.setText(context.getResources().getString(R.string.joindate) +" : "+ p.getString("joindate"));
holder.posterposts.setText( context.getResources().getString(R.string.user_posts) +" : " + p.getLong("posts"));
holder.message.setText( Html.fromHtml(p.getString("pagetext")));
//fonts
holder.postername.setTypeface(font_new);
holder.usertitle.setTypeface(font_new);
holder.registerdate.setTypeface(font_new);
holder.posterposts.setTypeface(font_new);
holder.message.setTypeface(font_kufi);
holder.attachment_text.setTypeface(font_kufi);
/********************************
* if this post is a Thread
*/
if ( p.getInt("is_thread") == 1 )
{
holder.registerdate.setVisibility(View.VISIBLE);
holder.posterposts.setVisibility(View.VISIBLE);
}else
/********************************
* Normal Post
*/
{
holder.registerdate.setVisibility(View.GONE);
holder.posterposts.setVisibility(View.GONE);
}
/********************************
* if post has attachments
*/
try {
JSONArray attachments = p.getJSONArray("attachments");
//if post has attachments
if ( attachments.length() != 0 )
{
holder.post_attachmentbox.setVisibility(View.VISIBLE);
//add attachments to post
for(int i = 0; i < attachments.length(); i++)
{
JSONObject attachment = attachments.getJSONObject(i);
//prevent add duplicate attachments
if( attachment.getLong("postid") == p.getLong("postid"))
{
Button attach = new Button(context);
attach.setText(attachment.getString("filename"));
holder.attachment_bit_handler.addView(attach);
}
}
}else
//post not has attachments
{
holder.post_attachmentbox.setVisibility(View.GONE);
}
} catch (JSONException e)
{
holder.post_attachmentbox.setVisibility(View.GONE);
}
} catch (JSONException e) {e.printStackTrace();}
return convertView;
}
}
public class a\u ShowThread\u pListView扩展了ArrayAdapter{
语境;
私人停车场;
@抑制警告(“原始类型”)
ArrayList ob;
智力资源;
字体字体为hb,字体为new,字体为kufi;
/*================================================
*设置列表视图
*===============================================*/
@抑制警告(“未选中”)
公共a_ShowThread_pListView(上下文上下文、int资源、@SuppressWarnings(“rawtypes”)数组列表对象){
超级(上下文、资源、对象);
this.context=上下文;
this.ob=对象;
这个资源=资源;
mInflater=LayoutInflater.from(上下文);
this.font_hb=Typeface.createFromAsset(context.getAssets(),“font/hb.ttf”);
this.font_new=Typeface.createFromAsset(context.getAssets(),“font/neu.ttf”);
this.font_kufi=Typeface.createFromAsset(context.getAssets(),“font/kufi.ttf”);
}
/*================================================
*物品柜台
*===============================================*/
public int getCount(){
返回ob.size();
}
/*================================================
*项目Posistion JSON
*===============================================*/
公共JSONObject getItem(JSONObject位置){
返回位置;
}
/*================================================
*项目位置
*===============================================*/
公共长getItemId(int位置){
返回位置;
}
/*================================================
*在唱诵位视图内保持视图
*===============================================*/
静态类视窗夹{
TextView postername;
文本视图用户标题;
文本视图注册表日期;
TextView posterposts;
文本视图消息;
水平滚动视图发布\附件\滚动视图;
线性布局邮筒附件箱;
线性布局附件\u位\u处理器;
text查看附件\ u文本;
JSONArray附件;
}
/*================================================
*按原始设置每个视图
*===============================================*/
@抑制警告(“未使用”)
@SuppressLint(“ResourceAsColor”)公共视图getView(最终整型位置、视图转换视图、视图组父视图)
{
最终持票人;
JSONObject p=(JSONObject)getItem(位置);
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.a_postbit,null);
holder=新的ViewHolder();
convertView.setTag(支架);
holder.postername=(TextView)convertView.findViewById(R.id.postername);//海报用户名
holder.usertitle=(TextView)convertView.findViewById(R.id.usertitle);//海报标题
holder.registerdate=(TextView)convertView.findViewById(R.id.registerdate);//海报发布日期
holder.posterposts=(TextView)convertView.findViewById(R.id.posterposts);//海报贴计数器
holder.message=(TextView)convertView.findViewById(R.id.message);//发布消息
holder.post_attachments_scrollview=(HorizontalScrollView)convertView.findViewById(R.id.post_attachments_scrollview);//附件视图框
holder.post_attachmentbox=(LinearLayout)convertView.findViewById(R.id.post_attachmentbox);//附件框隐藏/显示;
holder.attachment_text=(TextView)convertView.findViewById(R.id.attachment_text);//文本附件图例
holder.attachment_bit_handler=(LinearLayout)convertView.findViewById(R.id.attachment_bit_handler);//将post附件附加到此视图
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
试一试{
//增加价值
holder.postername.setText(p.getString(“postername”);
holder.usertitle.setText(p.getString(“usertitle”);
holder.registerdate.setText(context.getResources().getString(R.string.joindate)+“:”+p.getString(“joindate”);
holder.posterposts.setText(context.getResources().getString(R.string.user_posts)+“:”+p.getLong(“posts”);
holder.message.setText(Html.fromHtml(p.getString(“pagetext”)));
//字体
holder.postername.setTypeface(新字体);
holder.usertitle.setTypeface(新字体);
holder.attachment_bit_handler