gpt4 book ai didi

android - 适配器错误中的 TextView.setText()

转载 作者:行者123 更新时间:2023-11-29 21:13:05 25 4
gpt4 key购买 nike

我有一个自定义适配器扩展 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。每次代码输入第二个 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()); 时,它运行良好。但是,当它从第二个 if 调用时,错误出现了。 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;
}

请帮助我。

最佳答案

每当你比较一个字符串时,你应该总是使用 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());
}

关于android - 适配器错误中的 TextView.setText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22189132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com