我为listview创建了一个自定义适配器,我想为一行显示徽章。我没有收到任何错误,但徽章也没有出现。当我查看logcat时,有一条消息“BadgeView:需要ParentView”。但我已经提供了一个。
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.jauker.widget.BadgeView;
import java.util.ArrayList;
import java.util.List;
public class AdminViewAdapter extends BaseAdapter {
List<String> result = new ArrayList<String>();
List<Integer> resnum = new ArrayList<Integer>();
public static Context context;
private static LayoutInflater inflater=null;
public AdminViewAdapter(Context mainActivity, List<String> ADMINFUNCTIONS, List<Integer> NUMBERS) {
// TODO Auto-generated constructor stub
result=ADMINFUNCTIONS;
resnum = NUMBERS;
context=mainActivity;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class Holder
{
TextView os_text;
BadgeView badgeView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
Holder holder =new Holder();
if(v == null) {
v = inflater.inflate(R.layout.list_functions, null);
holder.os_text = (TextView) v.findViewById(R.id.receptionistfunctions);
if (result.toArray()[position].toString().contains("ACCOUNT")) {
holder.os_text.setTextAlignment(v.TEXT_ALIGNMENT_TEXT_START);
holder.os_text.setText(result.toArray()[position].toString());
holder.badgeView = new BadgeView(context);
holder.badgeView.setTargetView(holder.os_text);
holder.badgeView.setBadgeGravity(Gravity.BOTTOM);
holder.badgeView.setBadgeMargin(0, 0, 0, 0);
holder.badgeView.setBadgeCount(22);
}
else if (result.toArray()[position].toString().contains("LEAVE")) {
/* holder.os_text.setTextAlignment(v.TEXT_ALIGNMENT_TEXT_START);
holder.os_text.setText(result.toArray()[position].toString());
BadgeView badge = new BadgeView(context);
badge.setTag(target);
badge.setText(resnum.toArray()[1].toString());
badge.show();*/
}
else {
holder.os_text.setText(result.toArray()[position].toString());
}
v.setTag(holder);
}
else {
holder = (AdminViewAdapter.Holder)convertView.getTag();
}
return v;
}}
没有其他错误。我已经尝试使用另一个View变量作为徽章。
答案 0 :(得分:0)
您没有将BadgeView
添加到任何视图组,因此您的BadgeView
没有父级。这就是你看到那条消息的原因。观点应该有父母!
我认为您可以通过使用ViewGroup#addView()方法以编程方式将BadgeView
添加到适配器布局来解决此问题,例如此((ViewGroup) v).addView(holder.badgeView)
。或者更好的是,修改适配器布局以使其中包含BadgeView
。
编辑:
您可以像这样修改getView()
方法:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder =new Holder();
if(v == null) {
v = inflater.inflate(R.layout.list_functions, null);
// ...
holder.badgeView = new BadgeView(context);
holder.badgeView.setTargetView(holder.os_text);
holder.badgeView.setBadgeGravity(Gravity.BOTTOM);
holder.badgeView.setBadgeMargin(0, 0, 0, 0);
holder.badgeView.setBadgeCount(22);
// Add this line
((ViewGroup) v).addView(holder.badgeView);
}
// ...
}}