如何获得列表视图的所有项目以及它们的位置,并将它们用于其他活动,并可以转到下一个位置和返回位置

问题描述 投票:0回答:1
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            String name = mAdapter.getItem(position).getName();
            String title = mAdapter.getItem(position).getTitle();
            String url = mAdapter.getItem(position).getUrl();
            String link = mAdapter.getItem(position).getLink();
            int lastPosition = mAdapter.getCount();
           /* for(int i = position;i < lastPosition;i++){
                 nextName = mAdapter.getItem(i).getName();
                 nextTitle = mAdapter.getItem(i).getTitle();
                 nextMusicLink = mAdapter.getItem(i).getLink();
                 nextImageUrl = mAdapter.getItem(i).getUrl();

            }*/
          int pos = (int) mAdapter.getItemId(position);


            int pos1  = mAdapter.getPosition(imageRecord);
            String nextName = mAdapter.getItem(position+1).getName();
            String nextTitle =mAdapter.getItem(position+1).getTitle();
            String nextMusicLink = mAdapter.getItem(position+1).getLink();
            String nextImageUrl = mAdapter.getItem(position+1).getUrl();


            Bundle extras  = new Bundle();
            extras.putString("name",nextName);
            extras.putString("title",nextTitle);
            extras.putParcelableArrayList("list",records);
           // extras.putParcelable("list",imageRecord);
            //extras.putLong("id",id+1);
            extras.putInt("position",position);
            extras.putString("url",nextImageUrl);
            extras.putString("link",nextMusicLink);
            linksModel1.setArtistName(name);
            linksModel1.setSongName(title);
            linksModel1.setImageUrl(url);
            linksModel1.setMusicLink(link);


            linksSharedPrefManager = new LinksSharedPrefManager(Objects.requireNonNull(getContext()));
            linksSharedPrefManager.saveLInk(linksModel1);





            Intent intent = new Intent(getActivity(), music_player.class);


            intent.putExtras(extras);



            startActivity(intent);




        }
    });

这是我的列表视图,我想将带有值的下一个和后一个头寸发送给另一个活动使用您看到的下一个头寸值发送头寸+1

public class ImageRecord implements Serializable, Parcelable {
private String url;
private String title;
private String name;
private String duration;
private String link;

public ImageRecord(String url, String title, String name, String duration, String link) {
    this.url = url;
    this.title = title;
    this.name = name;
    this.duration = duration;
    this.link = link;
}

protected ImageRecord(Parcel in) {
    url = in.readString();
    title = in.readString();
    name = in.readString();
    duration = in.readString();
    link = in.readString();
}

public static final Creator<ImageRecord> CREATOR = new Creator<ImageRecord>() {
    @Override
    public ImageRecord createFromParcel(Parcel in) {
        return new ImageRecord(in);
    }

    @Override
    public ImageRecord[] newArray(int size) {
        return new ImageRecord[size];
    }
};

public String getUrl() {
    return url;
}

public String getTitle() {
    return title;
}

public String getName() {
    return name;
}

public String getDuration() {
    return duration;
}

public String getLink() {
    return link;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(url);
    parcel.writeString(title);
    parcel.writeString(name);
    parcel.writeString(duration);
    parcel.writeString(link);
}

}

这是我的模型课

public class ImageRecordsAdapter extends ArrayAdapter<ImageRecord> {
@Override
public int getPosition(@Nullable ImageRecord item) {
    return super.getPosition(item);
}

private final ImageLoader mImageLoader;
ImageRecord imageRecords;
ArrayList<ImageRecord> imageRecordArrayList;

public ImageRecordsAdapter(Context context) {
    super(context, R.layout.special_list_item);

    mImageLoader = new ImageLoader(VolleyApplication.getInstance().getRequestQueue(), new BitmapLruCache());
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public int getCount() {
    return super.getCount();
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.special_list_item, parent, false);
    }

    // NOTE: You would normally use the ViewHolder pattern here
    NetworkImageView imageView = (NetworkImageView) convertView.findViewById(R.id.image1);
    TextView textView = (TextView) convertView.findViewById(R.id.text1);
    TextView textView1 = (TextView) convertView.findViewById(R.id.text2) ;
    MediaPlayer m = new MediaPlayer();

    ImageRecord imageRecord = getItem(position);


    if (imageRecord != null) {
        imageView.setImageUrl(imageRecord.getUrl(), mImageLoader);
        textView.setText(imageRecord.getTitle());
        textView1.setText(imageRecord.getName());
        try {
            m.setDataSource(imageRecord.getLink());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    return convertView;
}

public void swapImageRecords(List<ImageRecord> objects) {
    clear();

    for (ImageRecord object : objects) {
        add(object);
    }

    notifyDataSetChanged();
}

}

这是我的适配器

 /*  nextUrl = (String)intent.getSerializableExtra("url");
    nextName = (String)intent.getSerializableExtra("name");
    nextTitle = (String)intent.getSerializableExtra("title");
    nextLink = (String)intent.getSerializableExtra("link");*/
    records = intent.getParcelableArrayListExtra("list");
    positionNext =Integer.parseInt(intent.getExtras().get("position").toString());
   // imageRecord = (ImageRecord)intent.getParcelableExtra("list");
   // id = (int)intent.getLongExtra("id",1);

我曾经在这里得到这个意图

  next_btn_music_player.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            nextLink = records.get(positionNext).getLink();
            nextName = records.get(positionNext).getName();
            nextTitle = records.get(positionNext).getTitle();
            nextUrl = records.get(positionNext).getUrl();

            linksModel1.setArtistName(nextName);
            linksModel1.setSongName(nextTitle);
            linksModel1.setImageUrl(nextUrl);
            linksModel1.setMusicLink(nextLink);

            linksSharedPrefManager = new LinksSharedPrefManager(Objects.requireNonNull(context));
            linksSharedPrefManager.saveLInk(linksModel1);
            try {
                backgroundAudioService.next();
            } catch (IOException e) {
                e.printStackTrace();
            }

           /* try {
                backgroundAudioService.next();
            } catch (IOException e) {
                e.printStackTrace();
            }*/


            Intent intent1 = new Intent(music_player.this , music_player.class);
            startActivity(intent1);
        }
    });


}

并使用下一个按钮将下一个位置值发送到下一个活动

android json listview android-intent position
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.