KrishnaJ KrishnaJ - 4 months ago 9
Android Question

android I want to display different odd and even row in custom listview

I made different XML file for odd and even row and made custom adapter using array adapter and i add data using linked list.Here is my java file where i give condition to print in alternate row ..help me through it...it gives force 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;

}


My MainActivity.java is like this..

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)




Answer

I solved my problem using array list instead off linked list in showing output image

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

public class MainActivity extends 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

public class RecycleView extends 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;

}