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

时间:2018-02-24 17:54:44

标签: java android listview android-arrayadapter

我创建了一个连接到Adapter的ListView(我从值/字符串中获取当前数据)。我添加了SeekBar,因此我可以过滤我的对象。一切正常,当搜索栏被更改时,对象正确显示。但是,即使数据显示正确,每当我点击任何对象时,我的下一个Activity都会显示错误的对象。

更新: 代码现在改编了不同。只有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);
    }
}

}

适配器:

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

1 个答案:

答案 0 :(得分:0)

您需要从public void listView()移除Activity功能,并将onClickListener放在getView的{​​{1}}功能中。

因此Adapter的最终getView函数看起来应该是这样的。我这里只是伪代码。您需要优雅地实现Adapter

onClickListener

希望有所帮助!