Java 列表视图正确显示数据,但选择错误

Java 列表视图正确显示数据,但选择错误,java,android,listview,android-arrayadapter,Java,Android,Listview,Android Arrayadapter,我已经创建了一个连接到适配器的ListView(我从值/字符串获取当前数据)。我添加了一个SeekBar,以便可以过滤我的对象。一切正常,对象在搜索栏更改时显示正确。但是,即使数据显示正确,只要我单击任何对象,我的下一个活动就会显示错误的对象 更新: 代码现在进行了不同的调整。只有ListView的第一行是可选的,如果选中,错误的对象详细信息将传输到下一个活动 下面是我的PhotoListActivity和我的Adapter类 public class PhotoListActivity ext

我已经创建了一个连接到适配器的
ListView
(我从值/字符串获取当前数据)。我添加了一个
SeekBar
,以便可以过滤我的对象。一切正常,对象在搜索栏更改时显示正确。但是,即使数据显示正确,只要我单击任何对象,我的下一个
活动
就会显示错误的对象

更新: 代码现在进行了不同的调整。只有ListView的第一行是可选的,如果选中,错误的对象详细信息将传输到下一个活动

下面是我的
PhotoListActivity
和我的
Adapter

public class PhotoListActivity extends AppCompatActivity {

static SeekBar seekbar;
static TextView seekBarTextView;
HotelAdapter hotelAdapter;

int progressValue;
ListView listView;
String[] hotelTitle;
String[] hotelAddress;
int[] hotelPrice;
String[] hotelDescription;
public static int[] hotelsPics = {
        R.drawable.villagehotel,
        R.drawable.britanniahotel,
        R.drawable.cockedhotel,
        R.drawable.qualityhotel,
        R.drawable.dayshotel
};
ArrayList<Hotels> hotels = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_list);


    hotelTitle = getResources().getStringArray(R.array.hotelTitle);
    hotelAddress = getResources().getStringArray(R.array.hotelAddress);
    hotelPrice = getResources().getIntArray(R.array.hotelPrice);
    hotelDescription = getResources().getStringArray(R.array.hotelDescription);


    hotelAdapter = new HotelAdapter(this, R.layout.activity_hotels, hotels);
    generateHotels();
    seekBar();

    listView = findViewById(R.id.listViewComplex);
    listView.setAdapter(hotelAdapter);
}


public void seekBar() {

    seekbar = findViewById(R.id.seekBar);
    seekBarTextView = findViewById(R.id.seekBarText);

    seekbar.getThumb().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
    seekbar.setProgress(60);

    seekbar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    progressValue = progress;
                    seekBarTextView.setText("Show hotels under £" + progress);
                    listView.setAdapter(null);
                    hotels.clear();
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    sortBy();
                    hotelAdapter.notifyDataSetChanged();



                }
            }
    );

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

public void generateHotels() {

    progressValue = 60;
    for (int i = 0; i < hotelPrice.length; i++) {
        if (hotelPrice[i] < progressValue) {
            hotels.add(new Hotels(hotelTitle[i], hotelAddress[i], hotelPrice[i], hotelsPics[i], hotelDescription[i]));
        }
    }
}

private void sortBy() {

    boolean sort = true;
    while (sort) {
        for (int i = 0; i < hotelPrice.length; i++) {
            Log.d("MYINT", "value: " + hotelPrice.length);
            if (progressValue > hotelPrice[i]) {
                hotels.add(new Hotels(hotelTitle[i], hotelAddress[i], hotelPrice[i], hotelsPics[i], hotelDescription[i]));
            }
        }
        sort = false;
        listView.setAdapter(hotelAdapter);
    }
}
公共类PhotoListActivity扩展了AppCompatActivity{
静态SeekBar-SeekBar;
静态文本视图seekBarTextView;
HotelAdapter HotelAdapter;
int值;
列表视图列表视图;
字符串[]hotelTitle;
字符串[]hotelAddress;
国际[]酒店价格;
字符串[]hotelDescription;
公共静态int[]hotelsPics={
R.drawable.villagehotel,
R.drawable.不列颠酒店,
R.drawable.Cocked酒店,
R.drawable.qualityhotel,
R.可拉拔dayshotel
};
ArrayList hotels=新建ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u photo\u list);
hotelTitle=getResources().getStringArray(R.array.hotelTitle);
hotelAddress=getResources().getStringArray(R.array.hotelAddress);
hotelPrice=getResources().getIntArray(R.array.hotelPrice);
hotelDescription=getResources().getStringArray(R.array.hotelDescription);
hotelAdapter=新hotelAdapter(此,R.layout.activity\u hotels,hotels);
生成酒店();
seekBar();
listView=findViewById(R.id.listViewComplex);
setAdapter(hotelAdapter);
}
公共无效seekBar(){
seekbar=findViewById(R.id.seekbar);
seekBarTextView=findviewbyd(R.id.seekBarText);
seekbar.getThumb().setColorFilter(0xFF00FF00,PorterDuff.Mode.MULTIPLY);
seekbar.setProgress(60);
seekbar.setonseekbarchaneglistener(
新的SeekBar.OnSeekBarChangeListener(){
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser){
进步价值=进步;
参见kbartextview.setText(“在“+progress”下显示酒店);
setAdapter(空);
酒店;
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
}
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
sortBy();
hotelAdapter.notifyDataSetChanged();
}
}
);
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
重写转换(android.R.anim.fade\u-in,android.R.anim.fade\u-out);
}
公共发电酒店(){
进程值=60;
对于(int i=0;ihotelPrice[i]){
添加(新酒店(hotelTitle[i]、hotelAddress[i]、hotelPrice[i]、hotelsPics[i]、hotelDescription[i]);
}
}
排序=假;
setAdapter(hotelAdapter);
}
}
}

适配器:

public class HotelAdapter extends ArrayAdapter<Hotels>  {


ImageView imageView;
TextView textViewTitle;
TextView textViewAddress;
TextView textViewPrice;

HotelAdapter adapter;
int progressValue;
ListView listView;
String[] hotelTitle;
String[] hotelAddress;
int[] hotelPrice;
String[] hotelDescription;
public static int[] hotelsPics = {
        R.drawable.villagehotel,
        R.drawable.britanniahotel,
        R.drawable.cockedhotel,
        R.drawable.qualityhotel,
        R.drawable.dayshotel
};
private int resource;
private ArrayList<Hotels> hotels;
Context context;

public HotelAdapter(Context context, int resource, ArrayList<Hotels> hotels) {
    super(context, resource, hotels);
    this.resource = resource;
    this.hotels = hotels;
    this.context = context;

}


public void updateHotelList() {
    this.notifyDataSetChanged();
}


@Override
public long getItemId(int position) {
    return position;
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    try {
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(resource, parent, false);
        }


            hotelTitle = context.getResources().getStringArray(R.array.hotelTitle);
            hotelAddress = context.getResources().getStringArray(R.array.hotelAddress);
            hotelPrice = context.getResources().getIntArray(R.array.hotelPrice);
            hotelDescription = context.getResources().getStringArray(R.array.hotelDescription);

            listView = v.findViewById(R.id.listViewComplex);
            imageView = v.findViewById(R.id.imageView);
            textViewTitle = v.findViewById(R.id.textViewName);
            textViewAddress = v.findViewById(R.id.textViewDetail);
            textViewPrice = v.findViewById(R.id.textViewDetail1);

            imageView.setImageResource(hotels.get(position).getPhoto());
            textViewTitle.setText(hotels.get(position).getTitle());
            textViewAddress.setText(hotels.get(position).getAddress());
            textViewPrice.setText("£" + String.valueOf(hotels.get(position).getPrice()));


            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    String hotelTit = hotelTitle[position];
                    String hotelDescription1 = hotelDescription[position];
                    Integer imgPath = hotelsPics[position];
                    String hotelAdd = hotelAddress[position];

                    Bundle extras = new Bundle();
                    extras.putString("hotelTit", hotelTit);
                    extras.putString("hotelDescr", hotelDescription1);
                    extras.putInt("hotelImg", imgPath);
                    extras.putString("hotelAdd", hotelAdd);
                    context = ApplicationContextProvider.getContext();

                    Intent intent = new Intent(ApplicationContextProvider.getContext(), BookHotel.class);
                    ActivityOptions options = ActivityOptions.makeCustomAnimation(context, android.R.anim.fade_in, android.R.anim.fade_out);
                    intent.putExtras(extras);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent, options.toBundle());
                }

            });

    } catch (Exception e) {
        e.printStackTrace();
        e.getCause();
    }

    return v;
}
公共类HotelAdapter扩展了ArrayAdapter{
图像视图图像视图;
文本视图文本视图标题;
文本视图文本视图地址;
text查看text查看价格;
酒店适配器;
int值;
列表视图列表视图;
字符串[]hotelTitle;
字符串[]hotelAddress;
国际[]酒店价格;
字符串[]hotelDescription;
公共静态int[]hotelsPics={
R.drawable.villagehotel,
R.drawable.不列颠酒店,
R.drawable.Cocked酒店,
R.drawable.qualityhotel,
R.可拉拔dayshotel
};
私有int资源;
私人ArrayList酒店;
语境;
公共HotelAdapter(上下文、内部资源、ArrayList酒店){
超级(背景、资源、酒店);
这个资源=资源;
这个。hotels=hotels;
this.context=上下文;
}
public void updateHotelList(){
this.notifyDataSetChanged();
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@非空
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图v=转换视图;
试一试{
如果(v==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
v=充气机。充气(资源,父项,false);
}
hotelTitle=context.getResources().getStringArray(R.array.hotelTitle);
hotelAddress=context.getResources().getStringArray(R.array.hotelAddress);
hotelPrice=context.getResources().getIntArray(R.array.hotelPrice);
hotelDescription=context.getResources().getStringArray(R.array.hotelDescription);
listView=v.findViewById(R.id.listViewComplex);
imageView=v.findViewById(R.id.imageView);
textViewTitle=v.findviewbyd(R.id.textViewName);
textViewAddress=v.findViewById(R.id.textViewDetail);
textViewPrice=v.findViewById(R.id.textViewDetail1);
setImageResource(hotels.get(position.getPhoto());
textViewTitle.setText(hotels.get(position.getTitle());
textViewAddress.setText(hotels.get(position.getAddress());
textViewPrice.setText(“£”+String.valueOf(hotels.get(position).get
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    try {
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(resource, parent, false);

            holder = new CellHolder();
            holder.imageView = v.findViewById(R.id.imageView);
            holder.textViewTitle =  v.findViewById(R.id.textViewName);
            holder.textViewAddress =  v.findViewById(R.id.textViewDetail);
            holder.textViewPrice = v.findViewById(R.id.textViewDetail1);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String hotelTit = hotelTitle[position];
                    String hotelDescription1 = hotelDescription[position];
                    Integer imgPath = hotelsPics[position];
                    String hotelAdd = hotelAddress[position];

                    Bundle extras = new Bundle();
                    extras.putString("hotelTit",hotelTit);
                    extras.putString("hotelDescr",hotelDescription1);
                    extras.putInt("hotelImg", imgPath);
                    extras.putString("hotelAdd", hotelAdd);

                    Context context = PhotoListActivity.this;
                    Intent intent = new Intent(context, BookHotel.class);
                    ActivityOptions options = ActivityOptions.makeCustomAnimation(context, android.R.anim.fade_in, android.R.anim.fade_out);
                    intent.putExtras(extras);
                    context.startActivity(intent, options.toBundle());
                }
            });

            v.setTag(holder);
        } else {
            holder = (CellHolder)v.getTag();
        }

        // You can set the data here
        setData(id,holder);

    } catch (Exception e) {
        e.printStackTrace();
        e.getCause();
    }

    return v;
}