0

Android OSのバージョンをどのように扱うかという奇妙な問題に遭遇しました。私の問題は、次の特定のコードが Android バージョン 4 で必要に応じて適切に機能しているが、Android バージョン 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();
}

Android 4.0では、setOnItemClickListener()の変数「位置」が適切に機能しています。つまり、0または1または2をクリックすると、適切な位置がそれぞれ0、1、または2として表示されますが、Android 2.3では同じことが逆に機能しています位置0は、0-8、1-7、2-5、4-4、5-4などのように8の位置を取っています...これを克服する方法を教えてください。

4

1 に答える 1

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

上記のコードを確認してください。

于 2012-11-22T12:17:12.150 に答える