我的列表视图显示前 5 个项目,它重复 android 中的项目
my listview showing the first 5 item its repating the items in android
大家好,我在 android 中遇到了一个问题,我需要的是我的 Fragment 中有一个列表视图,它是使用数据库中的值动态设置的,我的列表视图视图正在更新动态列表,因为我有 8 个项目 列表显示前 5 个项目并从第一个开始重复我的代码就像 this.please 帮助我,谢谢 advance.please 帮助我,我不是能够找到错误是什么。
我提前使用 Baseadapter.Thanks 创建了列表视图
class FashionAdapter extends BaseAdapter
{
Context context;
String redu[];
JSONArray article;
LayoutInflater inflater;
private List<Fashion> articlelist;
FashionAdapter(JSONArray article, Context context) {
this.context = context;
this.article = article;
//this.content=content;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return article.length();
}
@Override
public Object getItem(int position) {
try {
return article.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/eenadu-webfont.ttf");
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.fashoinlist_item, null);
Log.d("item postion",""+ position);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
holder = (ViewHolder) convertView.getTag(); // return last set view of ith item
}
return convertView;
}
}
我是这样写view hloder class的,在这里面我intilize了所有的views。
class ViewHolder {
ImageView fImage;
TextView fsadec,fTitle;
public ViewHolder(View itemView) {
fImage = (ImageView) itemView.findViewById(R.id.fashion_image);
fsadec=(TextView) itemView.findViewById(R.id.fashion_sdec);
fTitle=(TextView) itemView.findViewById(R.id.fashion_title);
}
请帮我解决这个问题,我找不到错误所在。
如果块
将下面的代码移到外面
JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
并在else语句后写
else{
holder = (ViewHolder) convertView.getTag();
} // return last set view of ith item
JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
尝试创建适配器 class 使用
https://www.buzzingandroid.com/tools/android-layout-finder/
并检查从您的数据库中输入的项目数是否正确。
大家好,我在 android 中遇到了一个问题,我需要的是我的 Fragment 中有一个列表视图,它是使用数据库中的值动态设置的,我的列表视图视图正在更新动态列表,因为我有 8 个项目 列表显示前 5 个项目并从第一个开始重复我的代码就像 this.please 帮助我,谢谢 advance.please 帮助我,我不是能够找到错误是什么。 我提前使用 Baseadapter.Thanks 创建了列表视图
class FashionAdapter extends BaseAdapter
{
Context context;
String redu[];
JSONArray article;
LayoutInflater inflater;
private List<Fashion> articlelist;
FashionAdapter(JSONArray article, Context context) {
this.context = context;
this.article = article;
//this.content=content;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return article.length();
}
@Override
public Object getItem(int position) {
try {
return article.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/eenadu-webfont.ttf");
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.fashoinlist_item, null);
Log.d("item postion",""+ position);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
holder = (ViewHolder) convertView.getTag(); // return last set view of ith item
}
return convertView;
}
}
我是这样写view hloder class的,在这里面我intilize了所有的views。 class ViewHolder {
ImageView fImage;
TextView fsadec,fTitle;
public ViewHolder(View itemView) {
fImage = (ImageView) itemView.findViewById(R.id.fashion_image);
fsadec=(TextView) itemView.findViewById(R.id.fashion_sdec);
fTitle=(TextView) itemView.findViewById(R.id.fashion_title);
}
请帮我解决这个问题,我找不到错误所在。
如果块
将下面的代码移到外面 JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
并在else语句后写
else{
holder = (ViewHolder) convertView.getTag();
} // return last set view of ith item
JSONObject FasionObject;
try {
FasionObject=article.getJSONObject(position);
holder.fTitle.setText(FasionObject.getString("articletitle"));
holder.fsadec.setText(FasionObject.getString("articleshortdec"));
holder.fTitle.setTypeface(font);
holder.fsadec.setTypeface(font);
String articlThumImage=FasionObject.getString("articlethumbimg");
Picasso.with(getActivity()).load(articlThumImage).into(holder.fImage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
尝试创建适配器 class 使用 https://www.buzzingandroid.com/tools/android-layout-finder/ 并检查从您的数据库中输入的项目数是否正确。