自定义的ListView适配器[机器人] [英] Custom ListView Adapter [Android]

查看:115
本文介绍了自定义的ListView适配器[机器人]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被困在一个小错误试图用Java实现一个自定义列表视图的一个Android应用程序。

I've been stuck on a little bug trying to implement a custom listview in Java for an Android application.

我想列出一堆话(通常为100℃; N< 500),并通过改变文字颜色突出那些行的子集。在这两组词(全球和子集)中列出的集合(目前一个ArrayList)

I'm trying to list a bunch words (typically, 100 < n < 500) and highlight a subset of those rows by changing the text color. The both sets of words (global and subset) are listed in a collection (currently an ArrayList)

现在的问题是,有些字缺失。这似乎是随机的。我认为这可能是更有可能的是针对'突出'缺少单词。 (即 我已经试过了code几个不同的变化,但这里是我目前得到:

The problem is that some words are missing. It seems random. I think it might be more likely that the words intended for 'highlighting' are missing. (I.e. I've tried a couple different variations of code, but here is what I've currently got:

 public class ResultsAdapter<T> extends ArrayAdapter<String> {

 private ArrayList<String> mHighlightSet;
 private ArrayList<String> mGlobalSet;
 private Context mContext;

 public ResultsAdapter(
   Context context, 
   int textViewResourceId,
   ArrayList<String> globalSet, 
   ArrayList<String> highlightSet) {

  super(context, textViewResourceId, globalSet);

  mContext = context;
  mGlobalSet = globalSet;
  mHighlightSet = highlightSet;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // return super.getView(position, convertView, parent);

  final String text = mGlobalSet.get(position);

  TextView view = new TextView(mContext);
  view.setText(text);

  if(mHighlightSet.contains(text))
   view.setTextColor(Color.RED);
  else
   view.setTextColor(Color.WHITE);

  return view;
 }

这个自定义适配器被实例化并分配由以下code:

This custom adapter gets instantiated and assigned by the following code:

  if (mSummaryList != null & mAllWords != null & foundWords != null) {

   ArrayList<String> globalSet = new ArrayList<String>(mAllWords.keySet()); // mAllWords is a TreeMap
   ArrayList<String> subset = hud.getFoundWords();

   mResultsAdapter = new ResultsAdapter<String>(this, R.layout.simplerow, globalSet, subset);

   mSummaryList.setAdapter(mResultsAdapter);
   mSummaryList.setOnItemClickListener(onWordListItemClickListener); 
  }

似乎还有的数据变量之间的一些断开,并且显示的内容在屏幕上。我迷路了,请大家帮忙。

It appears that there's some disconnect between the data variables, and what shows up on the screen. I'm lost, please help.

在此先感谢!

推荐答案

据,如果你测试它与少数的话,那么你可以更好地看到,如果你确实有一个问题可能有帮助。我假设 mAllWords 是某种地图。通过这样做 mAllWords.keySet()你得到随机顺序的话。这可能使得它很难说,如果一个字实际上是有或没有。你应该尝试进行排序的话有,或者使用一些已知的有序集合,这样可以更好地知道怎么回事。

It might help if you test it out with a small number of words, so you can better see if you actually have a problem. I'm assuming that mAllWords is some sort of map. By doing mAllWords.keySet() you are getting the words in a random order. This probably makes it hard to tell if a word is actually there or not. You should try sorting the words there, or using some known ordered set so you can better tell whats going on.

此外,getView你不希望创建一个TextView。这实在是低效率的。相反,你应该从已经膨胀布局视图和更新的造型。即,是这样的:

Also, in getView you do not want to be creating a TextView. This is really inefficient. Instead, you should get a view from the already inflated layout and update the styling. I.e, something like:

public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);

  TextView textView = view.findById(R.id.text);  // id of the text view in R.layout.simplerow

  String text = textView.getText();

  if(mHighlightSet.contains(text))
   view.setTextColor(Color.RED);
  else
   view.setTextColor(Color.WHITE);

  return view;
 }

超的getView将已经填写正确的字。你只需要更新您的getView方法中的造型。

The super's getView will already fill in the correct word. You just want to update the styling in your getView method.

这篇关于自定义的ListView适配器[机器人]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆