使用自定义listAdapter进行过滤的ListView

发布于 2024-12-05 06:26:31 字数 1049 浏览 0 评论 0原文

我使用以下代码来使用过滤后的 ListView: 如何在 Android 上动态更新 ListView

我正在尝试将列表视图中的文本颜色设置为黑色。显然这只能通过使用我之前制作的自定义适配器来完成。问题是,如果我切换到自定义适配器,我的 ListView 就不再被正确过滤。有什么想法吗?代码如下:

public class OffCampusStopsAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] names;

public OffCampusStopsAdapter(Context context, String[] names) {
    super(context, R.layout.rowlayout, names);
    this.context = context;
    this.names = names;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.offcampusstopslayout, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.stoplabel);
    textView.setText(names[position]);
    String s = names[position];

    return rowView;
}

}

Im using the following code to use a filtered ListView:
How to dynamically update a ListView on Android

and am trying to make the text color black in the listview. Apparently this can only be done by using a custom adapter, which I had made before. The problem is if i switch to the custom adapter, my ListView doesnt get properly filtered anymore. any ideas? Heres the code:

public class OffCampusStopsAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] names;

public OffCampusStopsAdapter(Context context, String[] names) {
    super(context, R.layout.rowlayout, names);
    this.context = context;
    this.names = names;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.offcampusstopslayout, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.stoplabel);
    textView.setText(names[position]);
    String s = names[position];

    return rowView;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-12-12 06:26:32

我这样做:

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


    //Get the current alert object
    Al al = getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        alertView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, alertView, true);
    }
    else
    {
        alertView = (LinearLayout) convertView;
    }
    //Get the text boxes from the listitem.xml file
    TextView alertText =(TextView)alertView.findViewById(R.id.txtAlertText);
    TextView alertDate =(TextView)alertView.findViewById(R.id.txtAlertDate);

    //Assign the appropriate data from our alert object above

    alertText.setTypeface(font);
    alertDate.setTypeface(font);
    alertText.setText(al.getOfficeName());
    alertDate.setText(al.getAddress());


    return alertView;
}

I do this way:

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


    //Get the current alert object
    Al al = getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        alertView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, alertView, true);
    }
    else
    {
        alertView = (LinearLayout) convertView;
    }
    //Get the text boxes from the listitem.xml file
    TextView alertText =(TextView)alertView.findViewById(R.id.txtAlertText);
    TextView alertDate =(TextView)alertView.findViewById(R.id.txtAlertDate);

    //Assign the appropriate data from our alert object above

    alertText.setTypeface(font);
    alertDate.setTypeface(font);
    alertText.setText(al.getOfficeName());
    alertDate.setText(al.getAddress());


    return alertView;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文