0

I came across some odd problem which some how deals with android OS versions. My problem is that the following given code is working properly as I want in android version 4 but the same programme working in a reverse manner in android version 2.3

public class FeturesList extends Activity {

ImageView               imgFeture;
ListView                listview;
ArrayList<String>               values;
CustomListAdapter   adapter;
public static boolean itemOnView = false;

int tempPos;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fetures_list);


    listview    = (ListView) findViewById(R.id.listView1);
    values=new ArrayList<String>();
    values.add(" ESSENTIAL      ");
    values.add(" EMERGENCY     ");
    values.add(" ENTERTAINMENT  ");
    values.add(" VIDEO CALL     ");
    values.add(" DOCTORS     ");
    values.add(" SOCIETY      ");
    values.add(" FAMILY      ");
    values.add(" OTHERS      ");
    values.add(" WHAT'S NEW     ");

    imgFeture   = (ImageView) findViewById(R.id.imageFeatures);

    adapter     = new CustomListAdapter(this, values);
    listview.setAdapter(adapter);
    listview.setSelection(0);


    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v,
                int position, long id) {
            imgFeture.setBackgroundDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/feature_"+position,"drawable",getPackageName())));


            tempPos = position;
            for(int i = 0; i < arg0.getChildCount(); i++){
                if(i == position){
                    arg0.getChildAt(i).setBackgroundColor(Color.WHITE);
                    TextView text = (TextView) v.findViewById(R.id.rowListTextView);
                    text.setTextColor(Color.BLACK);

                    adapter.notifyDataSetChanged();
                }else{

                    arg0.getChildAt(i).setBackgroundColor(Color.MAGENTA);

                }
            }


        }


    });


}

public class CustomListAdapter extends ArrayAdapter<String>
{
    Activity context;
    ArrayList<String> row=null;
    LayoutInflater inflator=null;
    public CustomListAdapter(Activity context,ArrayList<String> row)
    {
        super(context,R.layout.listrow,row);
        this.context=context;
        this.row=row;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView=convertView;
        /*FilterItem item = (FilterItem)this.getItem(position);*/
        if(convertView==null)
        {
            inflator=context.getLayoutInflater();
            final ViewHolder holder=new ViewHolder();
            rowView=inflator.inflate(R.layout.listrow, null);
            holder.rowText=(TextView)rowView.findViewById(R.id.rowListTextView);
            rowView.setTag(holder);

        }
        final ViewHolder hold=(ViewHolder)rowView.getTag();
        hold.rowText.setText(row.get(position));

        if(tempPos != position){
            hold.rowText.setTextColor(Color.WHITE);
        }
        return rowView;
    }




}

static class ViewHolder
{
    TextView rowText;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_fetures_list, menu);
    return false;
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_out_left,R.anim.slide_out_right);
    this.finish();
}

In android 4.0 the variable "position" of setOnItemClickListener() is working proper i.e if I click on 0 or 1 or 2 than it showing proper position as 0, 1, or 2 respectivily but the same thing is working reverse in android 2.3 as for position 0 it is taking position as 8 like 0-8, 1-7, 2-5, 4-4, 5-4 and so on... Please suggest me how to overcome this.

Nitin Bathija
  • 800
  • 3
  • 12
  • 24

1 Answers1

0
public View getView(final int position, View convertView, ViewGroup parent){


                ViewHolder holder = null;
                View rowView = convertView;


                if (rowView == null) {

                    LayoutInflater inflater = context.getLayoutInflater();
                //here give your xml name...
                    rowView = inflater.inflate(R.layout.case_row, null, true);

                    holder = new ViewHolder();
                    //here get your ids...
                    holder.caseType=(TextView) rowView.findViewById(R.id.caseType);


                    rowView.setTag(holder);
                }
                else
                {
                    holder = (ViewHolder) rowView.getTag();
                }

                //If you want to do some functions with your views do here...
                //example...
                holder.caseType.setText("hello android");

                rowView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        //Handle the listview click event here...

                        }
                });

                return rowView;

            }

             class ViewHolder {
                 TextView caseType; 
            }

Check the above code.

Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19