我如何更新列表视图中的单行?

时间:2014-11-04 05:34:47

标签: android arrays

说明

我正在下载字体文件,当下载文件时,我将该文件用作自定义字体。但是当第1行更新时,它也会更新第8行,反之亦然。我已经尝试了所有的方法,但我无法解决。 getView代码如下。请帮我解决这个问题。

提前致谢。

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if(convertView == null) {
        Log.d("inside value", position + "");
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.sub_family_item, null);
        holder.subVariantText = (TextView) view.findViewById(R.id.sub_family_style_text);
        holder.subTextStyle = (TextView) view.findViewById(R.id.text_style);
        holder.progressBar = (ProgressBar) view.findViewById(R.id.progress_bar_id);
        view.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) view.getTag();
    }

    SubFont fontStyle = getItem(position);
    if(fontStyle != null)
    {
        holder.subVariantText.setText(fontStyle.getSubList());
        holder.subTextStyle.setText(fontStyle.getFontStyleText());
    }

    File file = new File(FontConstant.folder+"/fontFile" + selectedPosition + position + ".ttf");
    if(file.exists() && file.length() == this.fileSize)
    {
        TextView textView = (TextView) view.findViewById(R.id.text_style);
        holder.progressBar.setVisibility(View.INVISIBLE);
        Typeface custom_font = Typeface.createFromFile(file);
        holder.subTextStyle.setTypeface(custom_font);
        holder.subTextStyle.setVisibility(View.VISIBLE);
    }
    return view;
}

static class ViewHolder
{
    TextView subVariantText;
    TextView subTextStyle;
    ProgressBar progressBar;
}

4 个答案:

答案 0 :(得分:1)

您需要在else中添加getView语句:

if(fontStyle != null)
{
    holder.subVariantText.setText(fontStyle.getSubList());
    holder.subTextStyle.setText(fontStyle.getFontStyleText());
}
else
{
   //what should this row display if fontStyle  is null?
}

if(file.exists() && file.length() == this.fileSize)
{
    TextView textView = (TextView) view.findViewById(R.id.text_style);
    holder.progressBar.setVisibility(View.INVISIBLE);
    Typeface custom_font = Typeface.createFromFile(file);
    holder.subTextStyle.setTypeface(custom_font);
    holder.subTextStyle.setVisibility(View.VISIBLE);
}
else
{
   //what should this row display if the file is not exist?
}

答案 1 :(得分:1)

将数据添加到传递给适配器的数据集合

然后致电

 listView.notifyDataSetChanged();

就是这样。

答案 2 :(得分:0)

使用以下代码: -

myListView.invalidateViews();

myListView.notifyDataSetChanged();

有关详细信息,请参阅以下答案: -

How to refresh Android listview?

答案 3 :(得分:0)

尝试这样可以帮到你,

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView == null) {
        Log.d("inside value", position + "");
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.sub_family_item, null, false);
        holder.subVariantText = (TextView) convertView.findViewById(R.id.sub_family_style_text);
        holder.subTextStyle = (TextView) convertView.findViewById(R.id.text_style);
        holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress_bar_id);

       convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    SubFont fontStyle = getItem(position);

    if(fontStyle != null)
    {
        holder.subVariantText.setText(fontStyle.getSubList());
        holder.subTextStyle.setText(fontStyle.getFontStyleText());
    }

    File file = new File(FontConstant.folder+"/fontFile" + selectedPosition + position + ".ttf");

   if(file.exists() && file.length() == this.fileSize)
    {
        holder.progressBar.setVisibility(View.INVISIBLE);
        Typeface custom_font = Typeface.createFromFile(file);
        holder.subTextStyle.setTypeface(custom_font);
        holder.subTextStyle.setVisibility(View.VISIBLE);
    } else{
       holder.subTextStyle.setVisibility(View.GONE);
    }
    return convertView;
}

static class ViewHolder
{
    TextView subVariantText;
    TextView subTextStyle;
    ProgressBar progressBar;
}

我希望它能帮到你