我为奇数和偶数行创建了不同的XML文件,并使用数组适配器制作了自定义适配器,并使用链接列表添加数据。这是我的java文件,我在条件中以交替行打印..帮助我通过它.. .it给力close.Thanks ..
public ViewHolder(View v) {
gender = (ImageView) v.findViewById(R.id.imgSmile);
name = (TextView) v.findViewById(R.id.tvName);
}
}
public RecycleView(Activity activity, int resource,List<Info> objects) {
super(activity, resource,objects);
// TODO Auto-generated constructor stub
this.activity=activity;
}
public int getViewTypeCount() {
return 2;
}
public int getItemViewType(int position) {
return position % 2;
}
public View getView(int pos,View convertView,ViewGroup parent) {
ViewHolder holder=null;
LayoutInflater inflater=(LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
int layoutResourse = 0;
int viewType=getItemViewType(pos);
switch (viewType) {
case 0:
layoutResourse=R.layout.even_row;
break;
case 1:
layoutResourse=R.layout.odd_row;
break;
}
if (convertView!=null) {
holder=(ViewHolder) convertView.getTag();
} else {
convertView=inflater.inflate(layoutResourse, parent,false);
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}
if (getItem(pos).isGender()) {
holder.gender.setImageResource(R.drawable.smile1);
} else {
holder.gender.setImageResource(R.drawable.smile2);
}
holder.name.setText(getItem(pos).getName());
return convertView;
}
我的 MainActivity.java 就是这样..
private void bindView() {
// TODO Auto-generated method stub
listView=(ListView) findViewById(R.id.listView1);
}
private void addListener() {
// TODO Auto-generated method stub
listView.setAdapter(new RecycleView(this,R.layout.odd_row,message));
}
private void setData() {
// TODO Auto-generated method stub
message=new LinkedList<Info>();
message.add(new Info("hi",true));
message.add(new Info("hello",false));
message.add(new Info("hi",true));
message.add(new Info("hello",false));
message.add(new Info("hi",true));
message.add(new Info("hello",false));
message.add(new Info("hi",true));
message.add(new Info("hello",false));
message.add(new Info("hi",true));
message.add(new Info("hello",false));
message.add(new Info("hi",true));
message.add(new Info("hello",false));
}
01-28 11:56:17.485: E/AndroidRuntime(339): FATAL EXCEPTION: main
01-28 11:56:17.485: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oddandeven/com.example.oddandeven.MainActivity}: java.lang.NullPointerException
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.os.Looper.loop(Looper.java:123)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-28 11:56:17.485: E/AndroidRuntime(339): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 11:56:17.485: E/AndroidRuntime(339): at java.lang.reflect.Method.invoke(Method.java:507)
01-28 11:56:17.485: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-28 11:56:17.485: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-28 11:56:17.485: E/AndroidRuntime(339): at dalvik.system.NativeStart.main(Native Method)
01-28 11:56:17.485: E/AndroidRuntime(339): Caused by: java.lang.NullPointerException
01-28 11:56:17.485: E/AndroidRuntime(339): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.widget.ListView.setAdapter(ListView.java:454)
01-28 11:56:17.485: E/AndroidRuntime(339): at com.example.oddandeven.MainActivity.addListener(MainActivity.java:41)
01-28 11:56:17.485: E/AndroidRuntime(339): at com.example.oddandeven.MainActivity.onCreate(MainActivity.java:26)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-28 11:56:17.485: E/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
答案 0 :(得分:0)
<强> info.java 强>
package com.example.oddandeven;
public class Info {
private String name;
private boolean gender;
public Info(String name,boolean gender)
{
this.name=name;
this.gender=gender;
}
public String getName()
{
return name;
}
public boolean isGender()
{
return gender;
}}
<强> MainActivity.java 强>
公共类MainActivity扩展了ActionBarActivity {
private ListView listView;
ArrayList<Info> list = new ArrayList<Info>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindView();
addListener();
setData();
}
private void bindView() {
// TODO Auto-generated method stub
listView=(ListView) findViewById(R.id.listView1);
}
private void addListener() {
// TODO Auto-generated method stub
listView.setAdapter(new RecycleView(this,R.layout.odd_row,list));
}
private void setData() {
// TODO Auto-generated method stub
Info item1= new Info("hi",true);
list.add(item1);
Info item2= new Info("hello",false);
list.add(item2);
Info item3= new Info("hi",true);
list.add(item3);
Info item4= new Info("hello",false);
list.add(item4);
Info item5= new Info("hi",true);
list.add(item5);
Info item6= new Info("hello",false);
list.add(item6);
Info item7= new Info("hi",true);
list.add(item7);
Info item8= new Info("hello",false);
list.add(item8);
}
<强> RecyclerView.java 强>
公共类RecycleView扩展了ArrayAdapter {
private Activity activity;
private class ViewHolder {
private ImageView gender;
private TextView name;
public ViewHolder(View v) {
gender = (ImageView) v.findViewById(R.id.imgSmile);
name = (TextView) v.findViewById(R.id.tvName);
}
}
public RecycleView(Activity activity, int resource,List<Info> objects) {
super(activity, resource,objects);
// TODO Auto-generated constructor stub
this.activity=activity;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position % 2;
}
public View getView(int pos,View convertView,ViewGroup parent) {
ViewHolder holder=null;
LayoutInflater inflater=(LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
int layoutResourse = 0;
int viewType=getItemViewType(pos);
switch (viewType) {
case 0:
layoutResourse=R.layout.even_row;
break;
case 1:
layoutResourse=R.layout.odd_row;
break;
}
if (convertView!=null) {
holder=(ViewHolder) convertView.getTag();
} else {
convertView=inflater.inflate(layoutResourse, parent,false);
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}
if (getItem(pos).isGender()) {
holder.gender.setImageResource(R.drawable.smile1);
} else {
holder.gender.setImageResource(R.drawable.smile2);
}
holder.name.setText(getItem(pos).getName());
return convertView;
}