ListView操作根据android版本而有所不同

时间:2012-11-22 11:51:22

标签: android android-listview android-adapter android-version

我遇到了一些奇怪的问题,一些如何处理Android操作系统版本。我的问题是,以下给定的代码正常工作,因为我想在Android版本4,但相同的程序在Android版本2.3中以相反的方式工作

public class FeturesList扩展了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();
}

在android 4.0中,setOnItemClickListener()的变量“position”正常工作,即如果我点击0或1或2比它显示正确的位置分别为0,1或2,但同样的事情在android中反向工作2.3对于位置0,它的位置为8,如0-8,1-7,2-5,4-4,5-4等...... 请建议我如何克服这一点。

1 个答案:

答案 0 :(得分: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; 
            }

检查上面的代码。