0

我正在弄清楚如何折叠我的 ListView。

我有一个这样的字符串数组:

  • 爱丽丝
  • 鲍勃
  • 无效的
  • 无效的
  • 无效的
  • 卡尔

我需要显示一个非空的 ListView,但是如果我点击 Carl,我需要知道 Carl 排在第 5 位。

我的 ListAdapter 显示了 3 个条目(Alice、Bob、empty_entry);如果我点击 empty_entry 它需要 Carl 但位置是 2 而不是 5。

如何修复我的代码?

package com.gorgo.pirates.model;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.pirates.R;

public class ListViewCustomAdapter extends BaseAdapter {
public String title[];

public Context context;
public LayoutInflater inflater;

public ListViewCustomAdapter(Context context, String[] title) {
    super();

    this.context = context;
    this.title = title;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // return not-null values count
    int count = 0;
    for (int i = 0; i < title.length; i++) {
        if (title[i] != null)
            count++;
    }
    return count;
}

@Override
public Object getItem(int position) {
    int start = position;

    while (start != title.length) {
        if (title[start] != null)
            return title[start];
        else
            start++;
    }

    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public static class ViewHolder {
    TextView txtViewTitle;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Typeface chops = Typeface.createFromAsset(this.context.getAssets(),
            "Commodore.ttf");

    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.listitem_row, null);

        holder.txtViewTitle = (TextView) convertView
                .findViewById(R.id.txtViewTitle);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewTitle.setTypeface(chops);
    holder.txtViewTitle.setTextColor(Color.CYAN);
    holder.txtViewTitle.setShadowLayer(10, 5, 5, Color.BLACK);
    holder.txtViewTitle.measure(MeasureSpec.makeMeasureSpec(
            convertView.getWidth() - 5, MeasureSpec.EXACTLY), MeasureSpec
            .makeMeasureSpec(convertView.getHeight(), MeasureSpec.EXACTLY));
    holder.txtViewTitle.layout(0, 0,
            holder.txtViewTitle.getMeasuredWidth(),
            holder.txtViewTitle.getMeasuredHeight());
    holder.txtViewTitle.setTextSize(13);

    return convertView;
}
}

谢谢 :)

4

0 回答 0