我有一个自定义适配器扩展ArrayAdapter
。这是我在getView
:
if(searchBy.equalsIgnoreCase("title") == true)
{
holder.title.setText(currItem.getTitle());
}
else if(searchBy.equalsIgnoreCase("publisher") == true)
{
holder.title.setText(currItem.getPublisher().getName());
}
问题出在第二中。每次代码进入第二个if时,textview的文本变为空(完全没有文本)。
我已调试并记录我的应用程序,以确保代码转到第二个if。现在,我试过这样的事情:
if(searchBy.equalsIgnoreCase("title") == true)
{
holder.title.setText(currItem.getTitle());
}
else if(searchBy.equalsIgnoreCase("publisher") == true)
{
holder.title.setText("TEST");
}
第二个中的“TEST”如果从未显示过,textview就像之前一样显示一个空字符串。
然后,我试图反转两个if:
if(searchBy.equalsIgnoreCase("title") == true)
{
holder.title.setText(currItem.getPublisher().getName());
}
else if(searchBy.equalsIgnoreCase("publisher") == true)
{
holder.title.setText(currItem.getTitle());
}
从第一个if调用代码holder.title.setText(currItem.getTitle());
时,它的工作正常。但是,当它从第二个调用时,出现错误。 textview只是空的。
我之前从未遇到过这样的事情,而且我一直在努力解决这个问题。
这是我的完整getView
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
LinearLayout categoryWrapper;
Holder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, null);
holder = new Holder();
holder.title = (TextView) row.findViewById(R.id.autocomplete_result_item_title);
holder.category = (TextView) row.findViewById(R.id.autocomplete_item_category);
holder.line1 = (View) row.findViewById(R.id.autocomplete_line_1);
holder.line2 = (View) row.findViewById(R.id.autocomplete_line_2);
holder.line3 = (View) row.findViewById(R.id.autocomplete_line_3);
row.setTag(holder);
} else {
holder = (Holder) row.getTag();
}
categoryWrapper = (LinearLayout) row.findViewById(R.id.autocomplete_category_wrapper);
final PurchasedItem currItem = getItem(position);
if(isStore == true)
{
currCategory = currItem.getBook_types();
prevItem = (position == 0) ? null : getItem(position-1).getBook_types();
}
else
{
currCategory = currItem.getCategory().getName();
prevItem = (position == 0) ? null : getItem(position-1).getCategory().getName();
}
if(searchBy.trim().equalsIgnoreCase("title") == true)
{
Log.d("auto1", currItem.getTitle());
holder.title.setText(currItem.getTitle());
//text = currItem.getTitle();
//holder.title.setText(currItem.getPublisher().getName());
//holder.title.setText("asd");
}
else if(searchBy.trim().equalsIgnoreCase("publisher") == true)
{
Log.d("auto2", currItem.getPublisher().getName());
holder.title.setText(currItem.getPublisher().getName());
//text = currItem.getPublisher().getName();
//holder.title.setText("asd");
}
/*else if(searchBy.equalsIgnoreCase("author") == true)
{
holder.title.setText(currItem.getAuthor_name());
//text = currItem.getAuthor_name();
}*/
//holder.title.setText(currItem.getTitle());
if(source != 2 && source != 3)
{
holder.category.setVisibility(View.VISIBLE);
holder.category.setText((currCategory.equalsIgnoreCase("magazine"))?"Magazine":(currCategory.equalsIgnoreCase("newspaper"))?"Newspaper":(currCategory.equalsIgnoreCase("book"))?"Book":"Apps");
if (position == 0) {
categoryWrapper.setVisibility(View.VISIBLE);
} else
{
if (currCategory.equals(prevItem))
categoryWrapper.setVisibility(View.GONE);
else
categoryWrapper.setVisibility(View.VISIBLE);
}
}
else
{
holder.category.setVisibility(View.GONE);
}
// atur line
if (position < getCount()- 1)
holder.line3.setVisibility(View.GONE);
else
holder.line3.setVisibility(View.VISIBLE);
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent n = new Intent(getContext(), SearchActivity.class);
n.putExtra("keyword", currItem.getTitle());
n.putExtra("source", source);
/*searchby selalu title, karena meskipun user search by publisher, hasil di
library activity berfokus pada title*/
n.putExtra("categoryKeyword", "title");
n.putExtra("type", category);
getContext().startActivity(n);
}
});
return row;
}
请帮助我。
答案 0 :(得分:2)
每当你比较一个String时,你应该总是使用trim()方法来避免空格。
试试这个,
if(searchBy.trim().equalsIgnoreCase("title") == true)
{
holder.title.setText(currItem.getPublisher().getName());
}
else if(searchBy.trim().equalsIgnoreCase("publisher") == true)
{
holder.title.setText(currItem.getTitle());
}