我正在尝试为我的大学项目制作一个通知应用程序。
我正在尝试为列表视图创建自定义适配器,但包含ListView的活动不显示任何内容。我认为我在CustomAdapter.java中的getView()方法中做错了。
CustomAdapter类用于为listview创建子视图。 listviechild.xml在listview tith 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>
答案 0 :(得分:2)
尝试以下方法:
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) {
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.from(context);
}
@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;
}
}
这应该适当地夸大你的观点和工作。如果还有其他问题,您可能会将空数据传递给适配器。
几个附注
1)您正在传递三个ArrayLists来填充ListView的数据。为什么不直接使用自定义Java对象传递单个数组列表? java对象的字段是名称,数据,时间。
即:
public class MyObject{
public String name;
public String date;
public String time;
MyObject(String name, String date, String time){
this.name = name;
this.date = date;
this.time = time;
}
//define getters and setters...
}
和
public CustomAdapter(Context context, ArrayList<MyObject> objs) {
...
...
}
2)每次都在膨胀视图。相反,使用ViewHolder Pattern来保存视图数据,而不是每次在应用程序中重新绘制和查找资源。 Android使用回收视图,因此您希望最大限度地利用这一点并节省资源并减少每次加载视图的延迟(大量开销!)。我不会为您提供一个示例,但请查看该链接以了解如何自己完成此操作!
答案 1 :(得分:1)
通过查看你的代码,我认为类CustomAdapter(ArrayList s)nam,date和time的实例varibles没有填充传递给构造函数的相应ArrayList的内容,因为包含ListView的Activity正在显示没有。 用以下代码替换构造函数
public CustomAdapter(Context context, ArrayList<String> nam,
ArrayList<String> date, ArrayList<String> time) {
super();
this.context = context;
this.nam.addAll(nam);
this.date.addAll(date);
this.time.addAll(time);
inflater =LayoutInflater.from(context);
}
我认为这可以解决您的问题。