Android - Viewpager - 获取列表数组项的ItemId

时间:2016-08-03 14:23:03

标签: android android-viewpager

我试图获取我创建的列表数组中的项目的itemid。

我使用视图寻呼机和寻呼机适配器来显示我可以滑动到下一条记录的每条记录的数据。我知道我的viewpager适配器在第一次访问时创建3个页面,这样当我将id传递回我的活动时,如果向右滑动则它是下一条记录或上一条记录。因为这是最后添加的记录的ID

GamesDetail.idforgame = (nesListItems.getItemId());

我尝试访问数组,就像我在使用列表视图的页面上显示记录的名称一样,但我不知道如何在不使用onclick监听器时实现这一点。

public void editgame(){
    pos = viewPager.getCurrentItem();
    nesListItems = (NesItems) arg0.getItemAtPosition(pos);
    idforgame = nesListItems.getItemId();
    Log.d("Pixo", "Value: " + idforgame);
    Intent intent = new Intent(GamesDetail.this, EditOwnedGame.class);//opens a new screen when the shopping list is clicked
    intent.putExtra("editgameid", idforgame);
    startActivity(intent);//start the new screen
}

我使用菜单编辑每条记录的各个部分,因此需要一种简单的方法来获取当前显示的记录的项目ID。我可以将其用于其他编辑功能。

这是我的寻呼机适配器

public class NesPagerAdapter extends PagerAdapter {

Context context; //sets up a variable as context
ArrayList<NesItems> nesList; //sets up  an array called shoppingList
String gameimage, synop, gen, subgen, pub, dev, gname, img, theyear;
int owned, carttrue, boxtrue, manualtrue, gameid, editgameid, pos, idforgame;

public NesPagerAdapter(Context context, ArrayList<NesItems> list) {
    this.context = context;//sets up the context for the class
    nesList = list; //sets up a variable as a list
}

@Override
public int getCount() {
    return nesList.size();
} //returns the number of items in the array

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ScrollView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    NesItems nesListItems = nesList.get(position);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //sets up the layout inflater
    View convertView = inflater.inflate(R.layout.content_games_detail, null); //use the layout to diaplay the array data

    TextView gamename = (TextView) convertView.findViewById(R.id.lblGameName);
    ImageView cover = (ImageView) convertView.findViewById(R.id.imgGameCover);
    TextView genre = (TextView) convertView.findViewById(R.id.lblGenre);
    TextView subgenre = (TextView) convertView.findViewById(R.id.lblSubgenre);
    TextView publisher = (TextView) convertView.findViewById(R.id.lblPublisher);
    TextView developer = (TextView) convertView.findViewById(R.id.lblDeveloper);
    TextView year = (TextView) convertView.findViewById(R.id.lblYear);
    ImageView cart = (ImageView) convertView.findViewById(R.id.imgCart);
    ImageView box = (ImageView) convertView.findViewById(R.id.imgBox);
    ImageView manual = (ImageView) convertView.findViewById(R.id.imgManual);
    TextView synopsis = (TextView) convertView.findViewById(R.id.lblSynopsis);
    synopsis.setText(nesListItems.getSynopsis());
    publisher.setText(nesListItems.getPublisher());
    genre.setText(nesListItems.getGenre());
    subgenre.setText(nesListItems.getSubgenre());
    gamename.setText(nesListItems.getName()); //sets the textview name with data from name
    developer.setText(nesListItems.getDeveloper());
    year.setText(nesListItems.getYear());
    Log.d("Pixo", "Item id: " + idforgame);
    gameimage = nesListItems.getImage();
    owned = nesListItems.getOwned();
    carttrue = nesListItems.getCart();
    boxtrue = nesListItems.getBox();
    manualtrue = nesListItems.getManual();
    int coverid=context.getResources().getIdentifier(gameimage, "drawable", context.getPackageName());
    cover.setImageResource(coverid);

    if(owned == 1){
        if(carttrue == 1){
            cart.setVisibility(View.VISIBLE);
        }else{}
        if(boxtrue == 1){
            box.setVisibility(View.VISIBLE);
        }else{}
        if(manualtrue == 1){
            manual.setVisibility(View.VISIBLE);
        } else{}
    }

    ((ViewPager) container).addView(convertView);
    //nesListItems = nesList.get(pos);
    GamesDetail.idforgame = (nesListItems.getItemId());
    Log.d("Pixo","Current position: " + position + " game id: " + idforgame);
    return convertView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((ScrollView) object);

}

}

0 个答案:

没有答案