我正在为我的大学项目尝试制作一个通知应用程序。
我正在尝试为ListView创建一个自定义适配器,但包含ListView的活动没有显示任何内容。我认为在CustomAdapter.java的getView()方法中做错了什么。
CustomAdapter类用于为ListView创建子视图。listviewchild.xml定义了ListView中单行的布局,并与xml文件listactivity.xml一起使用。
package com.example.client_nic;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter {
public Context context =null;
public ArrayList<String> nam = null;
public ArrayList<String> date= null;
public ArrayList<String> time = null;
LayoutInflater inflater =null;
public CustomAdapter(Context context, ArrayList<String> nam,
ArrayList<String> date, ArrayList<String> time) {
super();
nam = new ArrayList<String>();
date = new ArrayList<String>();
time = new ArrayList<String>();
this.context = context;
this.nam = nam;
this.date = date;
this.time = time;
inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return nam.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int pos, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
Log.e("name",nam.get(pos));
Log.e("date",date.get(pos));
Log.e("time", time.get(pos));
view = inflater.inflate(R.layout.listviewchild, arg2,false);
if(view.isActivated()){
Toast.makeText(context, "yes",Toast.LENGTH_SHORT).show();
}
TextView nametv = (TextView)view.findViewById(R.id.textView1);
TextView datetv = (TextView)view.findViewById(R.id.textView2);
TextView timetv = (TextView)view.findViewById(R.id.textView3);
nametv.setText(nam.get(pos));
datetv.setText(date.get(pos));
timetv.setText(time.get(pos));
return view;
}
}
listviewchild.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="30dp" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:layout_below="@+id/textView1"
/>
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/textView2"
android:layout_below="@+id/textView1"
/>
</RelativeLayout>
listactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>