0

我遇到了一些奇怪的問題,其中一些如何處理Android操作系統版本。我的問題是,下面給出的代碼工作正常,因爲我想在Android版本4,但同樣的程序以相反的方式在Android版本工作2.3ListView操作根據Android版本變化

公共類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(); 
} 

在安卓4.0 setOnItemClickListener()的變量「position」正在正常工作,也就是說,如果我點擊0或1或2,而不是顯示正確位置爲0,1或2,但同樣的東西在android 2.3中與位置0相反它的位置爲8-8,如0-8,1-7,2-5,4-4,5-4等...... 請給我建議如何克服這一點。

+0

您在Adapter類中的getView()方法是錯誤的。請再次檢查代碼。 –

+0

你好Venkata克里希納..你可以指出什麼是getView()中的錯誤。 –

回答

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; 
      } 

檢查上面的代碼。