如何显示上一个和下一个列表项的详细信息

时间:2014-08-08 11:18:52

标签: java android listview android-intent android-listview

在我的程序中,我从json获取数据并存储到arraylist然后填充到ListView中,一旦用户点击任何列表项目显示另一个活动中的记录,其中我有两个按钮,即: - previous&接下来,现在我希望每当用户点击上一个按钮(需要显示上一个列表项详细信息)和下一个按钮(需要显示下一个列表项详细信息).....

这是我用于获取数据并显示已点击项目详细信息的代码

DetailActivity.java:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Bundle bundle = getIntent().getExtras();
        // add cast to ArrayList<Actors>
        final ArrayList<Actors> arrayList= (ArrayList<Actors>) bundle.getSerializable("information");
        final int currentindex = bundle.getInt("index");
        final Actors actors = arrayList.get(currentindex);

        textName = (TextView) findViewById(R.id.textName);

        for(int i=0; i<arrayList.size(); i++)
        {             
           textName.setText(actors.getName());             
        }

        btnNext = (Button) findViewById(R.id.button1);
        btnPrev = (Button) findViewById(R.id.button2);

        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });

2 个答案:

答案 0 :(得分:3)

  int currentindex;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    Bundle bundle = getIntent().getExtras();
    // add cast to ArrayList<Actors>
    final ArrayList<Actors> arrayList= (ArrayList<Actors>)  bundle.getSerializable("information");
      currentindex = bundle.getInt("index");
    textName = (TextView) findViewById(R.id.textName);
    textName.setText(""+arrayList.arrayList.get(currentindex).getName());
    btnNext = (Button) findViewById(R.id.button1);
    btnPrev = (Button) findViewById(R.id.button2);

    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            currentindex=currentindex+1;
            if(currentindex<arrayList.size()){
                 textName.setText(""+arrayList.get(currentindex).getName());
            }
            else{
                currentindex=currentindex-1;
            }


        }
    });

    btnPrev.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            currentindex=currentindex-1;
            if(currentindex>=0){
                 textName.setText(""+arrayList.get(currentindex).getName());
            }
            else{
                currentindex=currentindex+1;
            }
        }
    });

答案 1 :(得分:1)

只需将带有选定索引的JSON收集的arraylist传递给下一个启动活动,然后根据当前选择获取-1和+1等位置,并从集合中获取值以显示特定位置信息

在lciking上执行类似下面的操作来启动nre活动

intent.putStringArrayListExtra("contactList ", contactList );
        intent.putExtra("current", pos);

在下一个活动中执行以下操作

ArrayList<String > contactList contactList = intent.getStringArrayListExtra("contactList ");
        int position = intent.getIntExtra("current", 0);
        contactList.get(position);

        //for previous do
        contactList.get(position-1);
        //for next do
        contactList.get(position+1);