我遇到了一些奇怪的问题,其中一些如何处理 android OS 版本。我的问题是以下给定的代码在 android 版本 4 中可以正常工作,但相同的程序在 android 版本 2.3 中以相反的方式工作
公共类FeturesList扩展活动{
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() 的变量“位置”工作正常,即如果我点击 0 或 1 或 2,而不是分别显示正确的位置为 0、1 或 2,但同样的事情在 android 2.3 中反向工作位置 0 它的位置为 8,如 0-8、1-7、2-5、4-4、5-4 等等......请建议我如何克服这个问题。