简体   繁体   English

单击列表项,在详细信息活动上浏览列表视图活动中的下一个和上一个列表项

[英]List item click, on detail activity navigate through next and previous list items from list view activity

I have 2 activities, 1st containing the list view, and when user click on that list view's list item, the detail view activity will be launched (2nd activity). 我有2个活动,第一个活动包含列表视图,当用户单击该列表视图的列表项时,将启动详细信息视图活动(第二个活动)。

Now on 2nd activity, I have 2 buttons, NEXT button and PREVIOUS button, so when user click on NEXT button, the next list item from the 1st activity must be shown. 现在,在第二个活动中,我有2个按钮:“ 下一个”按钮和“上一个”按钮,因此,当用户单击“下一个”按钮时,必须显示第一个活动中的下一个列表项。 And on clicking previous button, the previous list item must be shown. 并且在单击上一个按钮时,必须显示上一个列表项。

I have extended ArrayAdapter on 1st activity, to display the custom list item. 我在第一个活动中扩展了ArrayAdapter,以显示自定义列表项。 And on adapter, there are 2 items, ie 2 textviews. 在适配器上,有2个项目,即2个textview。

And when user clicks and 2nd activity is launched, those 2 textview's details will be shown in detail. 当用户单击并启动第二项活动时,将详细显示这两个textview的详细信息。 And to make the navigation through the items, going back to the 1st activity, and then again 2nd activity is launched per item, that is hectic scenario for user, so I want to navigate the items on detail view activity itself. 并要在项目之间进行导航,请回到第一个活动,然后再次为每个项目启动第二个活动,这对于用户来说是忙碌的情况,因此我想在详细视图活动本身上导航这些项目。 So what I am supposed to do? 那我该怎么办?

What I have done so far is, I have extended the ArrayAdapter, so from getView() method, I have taken the position value and then on 2nd activity I have done is, 到目前为止,我已经完成了对ArrayAdapter的扩展,因此从getView()方法中获取了位置值,然后在第二个活动中,

 next = (ImageView) findViewById(R.id.next);
    previous = (ImageView) findViewById(R.id.previous);

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

            position++;

            Log.e("position", String.valueOf(position));

        }
    });

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

            position--;

            Log.e("position", String.valueOf(position));

        }
    });

But I don't know what should I do next? 但是我不知道下一步该怎么做?

The position value is taken from 1st activity, with intent.putExtra("position", position) and fetched on 2nd activity using intent.getStringExtra("position"). 位置值是从第一个活动中获取的,具有intent.putExtra(“ position”,position),并在第二个活动中使用intent.getStringExtra(“ position”)获取。

I am not even able to understand this and that , being just theoretical. 只是理论上,我什至无法理解 一点 Thanks in advance. 提前致谢。


EDIT-1: 编辑-1:

I have custom ArrayAdapter in 1st activity, and on onclickListener , I have applied, 我在第一个活动中有自定义ArrayAdapter ,并且在onclickListener上我已经应用了,

 convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(fActivity.this, sActivity.class);

                intent.putExtra("txt1", "name");

                intent.putExtra("txt2", "address");

                intent.putExtra("position", position);

                startActivity(intent);
            }
        });

this position is retrieved from 这个位置是从

public View getView(final int position, View convertView, ViewGroup parent) {
....
}

this method, is in 这种方法在

 class homeAdapter extends ArrayAdapter<list>

as I have written before on this same panel. 如我之前在同一面板上所写。 And now on 2nd activity I have to get txt1 and txt2 on 2nd activity with next and previous buttons. 现在在第二个活动中,我必须使用下一个和上一个按钮在第二个活动中获取txt1txt2

For this you have to keep your Array/List global so that both activity can access it. 为此,您必须保持阵列/列表全局,以便两个活动都可以访问它。 You can do this by passing whole array/list to next activity by bundle also. 您也可以通过捆绑将整个数组/列表传递给下一个活动来实现。

You have to do followings: 您必须执行以下操作:

  1. Put clicked position and whole array/list in bundle and send it to second activity. 将单击的位置和整个数组/列表放在捆绑中,然后将其发送到第二个活动。
  2. Get the current position and array/list from bundle and show the detail of current position. 从包中获取当前位置和数组/列表,并显示当前位置的详细信息。
  3. Increase the position on click of Next button, and get the item of this position from list.array and show the detail of this position. 单击“下一步”按钮,增加位置,并从list.array获取该位置的项目,并显示该位置的详细信息。
  4. Decrease the position on click of Previous button, and get the item of this position from list.array and show the detail of this position. 单击“上一个”按钮时减小位置,然后从list.array获取该位置的项目并显示该位置的详细信息。

It would have been really helpful if you would have pasted the code you used to populate your ListView. 如果您粘贴了用于填充ListView的代码,那将真的很有帮助。 what kind Object you are using to create the List? 您要使用哪种对象创建列表? none the less, here is some code and approach. 尽管如此,这是一些代码和方法。 I am assuming a couple of things here, if they do not match your code, comment on the answer. 我在这里假设了几件事,如果它们与您的代码不匹配,请对答案进行评论。

  1. Create an ArrayList, now this can be as simple as a String ArrayList<String> or some other object that you want, like ArrayList<Items> // sample Array<Items> itemsList = new ArrayList<Items>(); 创建一个ArrayList,现在它可以像String ArrayList<String>或所需的其他对象一样简单,例如ArrayList<Items> // sample Array<Items> itemsList = new ArrayList<Items>();

  2. Now in your itemClickListener on the list view, pass the position and the whole array list to your 2nd activity using Intents. 现在,在列表视图的itemClickListener中,使用Intent将位置和整个数组列表传递给第二个活动。

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

     @override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Items item = (Items) parent.getItemAtPosition(position); Intent startDetailActivity = new Intent(Activity1.this, Activity2.class); startDetailActivity.putExtra("position", position); startDetailActivity.putExtra("itemList", itemList); startActivity(startDetailActivity); } 

    });

  3. In your activity 2, resolve the intent data: 在活动2中,解决意图数据:

    Intent i = this.getIntent(); int clickedPosition = i.getIntExtra(position, 0); ArrayList<Items> itemsList = i.getSerializableExtra("itemList");

  4. Set data on your text views by getting the object at position in the itemsList: 通过使对象位于itemsList中的位置来在文本视图上设置数据:

    textView1.setText(itemsList.get(clickedPosition).getSomeText()); textView2.setText(itemsList.get(clickedPosition).getSomeOtherText());

And lastly, when you click the next & previous buttons on your activity 2, then increase or decrease the size of the clickedPosition variable, so that it points to the right object in the list. 最后,当您单击活动2上的下一个和上一个按钮时,请增大或减小clickedPosition变量的大小,以使其指向列表中的正确对象。 Just be sure that the index you are requesting from the array list is valid, otherwise you will get an index out of bounds exception. 只要确保您从数组列表中请求的索引是有效的,否则您将获得索引超出范围的异常。 This can be done by checking the size of the array list before requesting for the object at that index. 这可以通过在请求该索引处的对象之前检查数组列表的大小来完成。

Now, this answers assumes a lot of things, like you have an array list of some object. 现在,这个答案假设了很多事情,例如您有一些对象的数组列表。 The variables in those objects have at least getter methods, and that the class in which these variables are implements a Serializable interface. 这些对象中的变量至少具有getter方法,并且这些变量所在的类实现了Serializable接口。 If you have any trouble implementing this code, put it in comments. 如果您在执行此代码时遇到任何麻烦,请将其放在注释中。 I'll be happy to help in any way i can. 我将竭尽所能为您提供帮助。

EDIT-1: 编辑-1:

Now, I guess you were able to resolve the intent in your sActivity, but you are facing problems with displaying data on sActivity from the position and List. 现在,我想您已经能够解决sActivity中的意图,但是在从位置和列表显示sActivity上的数据时遇到了问题。 So, firstly just make sure u have passed the clicked position and also the entire list in the intent. 因此,首先,只需确保您已经通过了点击位置以及意图中的整个列表。 Resolve them in the sActivity. 在sActivity中解决它们。 Now, to display data in sActivity from the list, write the below lines of code(After resolving intent on sActivity). 现在,要从列表中显示sActivity中的数据,请编写以下代码行(解决sActivity的意图后)。

yourTextView.setText(yourList.get(clickedPosition).getSomething());
yourTextView2.setText(yourList.get(clickedPosition).getSomeOtherThing());

Don't be confused by getSomething(), it is just an illustration. 不要被getSomething()所迷惑,它只是一个例子。 You should get the data what you want to display here. 您应该获得想要在此处显示的数据。 I have not seen the list you are making, so i've put this code. 我没有看到您要创建的列表,所以我已经输入了这段代码。 Now, to deal with next & previous button clicks. 现在,处理下一个和上一个按钮的单击。 Simply add to clicked position variable on next click like: 只需在下次点击时添加到点击的位置变量即可,例如:

nextButton.setOnClickListener(new View.OnClickListener() {
@Override
        public void onClick(View v) {
           clickedPosition++;
yourTextView.setText(yourList.get(clickedPosition).getSomething());
yourTextView2.setText(yourList.get(clickedPosition).getSomeOtherThing());

        }
    });

On the previous button onClick, just decrement the clickedPosition variable like: 在上一个按钮onClick上,只需减小clickedPosition变量即可,例如:

clickedPosition--;
//set text views here then

This should do it my friend. 这应该做我的朋友。

If you have static lists and do not want to change the list data dynamically, the most simple thing would be to store the list data in your strings.xml file like 如果您有静态列表,并且不想动态更改列表数据,那么最简单的方法是将列表数据存储在您的strings.xml文件中,例如

<string-array name="textview1_items> 
<item>head1</item>
<item>head2</item>
<item>head3</item>
.
.
.
</string-array>

likewise for textview 2 items. 同样适用于textview 2项目。

and then retrieve the list items from these string arrays in both your list adapter and in your second activity. 然后从列表适配器和第二个活动中的这些字符串数组中检索列表项。

To access these arrays from java, you need to do the following : 要从Java访问这些数组,您需要执行以下操作:

String[] headers = context.getResources.getStringArray(R.array.textview1_items); //use context in adapter and this in second activity //在适配器中使用上下文,在第二个活动中使用

Now when the user clicks list item just pass the position of selected item to second activity and retrieve the information of that index from your string arrays. 现在,当用户单击列表项时,只需将选定项的位置传递给第二个活动,然后从您的字符串数组中检索该索引的信息。

Hope it helps! 希望能帮助到你! In case of any doubts, please ask! 如有任何疑问,请询问!

You simply just need to do, 您只需要做,

in 2nd activity, 在第二活动中

str1 = fActivity.listAdapter.getItem(position).data1;
str2 = fActivity.listAdapter.getItem(position).data2;

and in 1st activity, 在第一个活动中

public static ArrayAdapter<List> listAdapter;

and there you go, you can have your answer. 然后您就可以得到答案。

Cheers! 干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据下一个活动中列表视图更新中的更改来更新上一个活动中的列表视图项? - How to update a list View Item in previous activity basing on the changes in the list view update in the next activity? 列表视图项目点击时的Android活动过渡 - Android activity transition on list view item click 如何在Android中使用列表项单击导航到另一个活动 - How to navigate in to another activity using list item click in Android 单击列表视图项以在单独的活动中显示另一个列表视图 - Click the list view item to show an another list view in separate activity 单击列表视图项并将数据库项ID传递给下一个活动? - Clicking a list view item and passing a database item ID to the next activity? 在另一个活动的列表视图中添加项目 - Add an item in list view from another activity 从另一个活动添加列表视图项目 - adding a list view Item from another activity 将列表视图图像从一个活动传递到android中的下一个活动 - pass list view image from one activity to next activity in android 返回上一个活动时,列表视图中的项目超载 - items overloading in list view while coming back to previous activity 当我单击 recyclerview 中的项目时,如何从 firebase 检索数据并将其显示在下一个活动的列表视图中? - When I click on item in recyclerview, how can I retrieve data from firebase and show it in a list view in the next activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM