gpt4 book ai didi

安卓 : getView does not called in custom adapter

转载 作者:行者123 更新时间:2023-11-30 04:04:35 50 4
gpt4 key购买 nike

在我的 Activity 中,我有一个按钮,当我单击该按钮时,它会显示包含标题和 ListView 的自定义对话框.

我为 ListView 设置适配器但是getView()未调用方法。我的数组是 ArrayList<String> & 它的大小是 3。

这是我的代码。

在 Activity.java::

    notification_btn = (Button) findViewById(R.id.notifications_btn);
notification_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Dialog dialog = new Dialog(Home.this,android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.notifications_layout);


ListView list = (ListView) dialog.findViewById(R.id.listView_notifications);
TextView title = (TextView) dialog.findViewById(R.id.dialogTitle);
TextView footer = (TextView) dialog.findViewById(R.id.notification_footer);

title.setText("Title");


NotificationAdapter adapter = new NotificationAdapter(Home.this,array);
list.setAdapter(adapter);


dialog.show();
}
});

NotificationAdapter.java::

public class NotificationAdapter extends BaseAdapter{

ArrayList<String> items;
private Context context;
private LayoutInflater mInflater;

static class ViewHolder {
TextView text;
}

public NotificationAdapter(Context context, ArrayList<String> items) {
this.context = context;
this.items = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int arg0) {
return items.get(arg0);
}

@Override
public long getItemId(int arg0) {
return arg0;
}


// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
convertView = mInflater.inflate(R.layout.notifiation_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text_notification);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {

holder.text.setText(items.get(position));


} catch (Exception e) {
VLogger.getLogger().info("Exception occured :: "+e);
}

return convertView;
}
}

为什么 getView()方法没有被调用?我没有找到原因。请帮忙。

最佳答案

一旦将适配器设置为列表:

listview.setAdapter(adapter)

您必须通知适配器有关更改的数据集,以便它重新填充列表:

adapter.notifyDataSetChanged()

关于安卓 : getView does not called in custom adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951448/

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