我尝试创建自定义列表视图,然后出现错误,我也不知道为什么。
this, R.layout.list_events_item,listEvents
处有错误
,错误消息是:
EventsFragment.java
public class EventsFragment extends Fragment {
private View paramView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.fragment_events,container,false);
ListView listView = (ListView) paramView.findViewById(R.id.listViewEvents);
List<Events> listEvents = new ArrayList<Events>();
Events events = new Events(1,"12:00","A1221",
"Error at the same place","1.2.3.4","1.2.4.3","A212","123",443,1234);
listEvents.add(events);
events = new Events(2,"11:20","A1221",
"Error at the same place","1.2.3.4","1.2.4.3","A212","123",443,1234);
listEvents.add(events);
listView.setAdapter(new EventsAdapter(this, R.layout.list_events_item,listEvents));
return paramView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
}
EventsAdapter.java
public class EventsAdapter extends ArrayAdapter<Events> {
List<Events> listEvents;
Context context;
int layout;
public EventsAdapter(Context context, int layout, List<Events> listEvents){
super(context,layout,listEvents);
this.context=context;
this.layout=layout;
this.listEvents=listEvents;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
EventsHolder holder;
if (v == null){
LayoutInflater vi=((Activity)context).getLayoutInflater();
holder = new EventsHolder();
holder.time=(TextView) v.findViewById(R.id.time);
holder.signature = (TextView) v.findViewById(R.id.signatureId);
holder.sensor = (TextView) v.findViewById(R.id.sensor);
holder.attacker = (TextView) v.findViewById(R.id.attacker);
holder.attacked = (TextView) v.findViewById(R.id.attacked);
v.setTag(holder);
}else {
holder=(EventsHolder) v.getTag();
}
Events events = listEvents.get(position);
holder.time.setText(events.getTime());
holder.signature.setText(events.getSignature_id());
holder.sensor.setText(events.getSensor());
holder.attacker.setText(events.getAttacker_port());
holder.attacked.setText(events.getAttacked_port());
return v;
}
static class EventsHolder{
TextView time;
TextView signature;
TextView sensor;
TextView attacker;
TextView attacked;
}
}
我的事件模型
public class Events {
int id;
String time;
String signature_id;
String alert_message;
String ip_source;
String ip_destination;
String sensor;
String protocol;
int attacker_port;
int attacked_port;
public Events(int id, String time, String signature_id, String alert_message, String ip_source, String ip_destination, String sensor, String protocol, int attacker_port, int attacked_port) {
this.id = id;
this.time = time;
this.signature_id = signature_id;
this.alert_message = alert_message;
this.ip_source = ip_source;
this.ip_destination = ip_destination;
this.sensor = sensor;
this.protocol = protocol;
this.attacker_port = attacker_port;
this.attacked_port = attacked_port;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSignature_id() {
return signature_id;
}
public void setSignature_id(String signature_id) {
this.signature_id = signature_id;
}
public String getAlert_message() {
return alert_message;
}
public void setAlert_message(String alert_message) {
this.alert_message = alert_message;
}
public String getIp_source() {
return ip_source;
}
public void setIp_source(String ip_source) {
this.ip_source = ip_source;
}
public String getIp_destination() {
return ip_destination;
}
public void setIp_destination(String ip_destination) {
this.ip_destination = ip_destination;
}
public String getSensor() {
return sensor;
}
public void setSensor(String sensor) {
this.sensor = sensor;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public int getAttacker_port() {
return attacker_port;
}
public void setAttacker_port(int attacker_port) {
this.attacker_port = attacker_port;
}
public int getAttacked_port() {
return attacked_port;
}
public void setAttacked_port(int attacked_port) {
this.attacked_port = attacked_port;
}
}
答案 0 :(得分:0)
尝试将this
更改为getActivity()