4

I'trying to display two lists inside my ScrollView. I found following solution: Using a ListAdapter to fill a LinearLayout inside a ScrollView layout (filling LinearLayout with custom adapter).

This solution works fine but I don't know how to handle click event on list items. It would be simple if I used ListView (onItemClick method with position parameter) but LinearLayout doesn't support the same functionality.

My layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:scrollbars="none"
android:background="@color/background_gray_light">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
// here should be view with infobox, not implemented yet //
<LinearLayout
    android:id="@+id/story_list_inbox"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/archive"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:background="@color/actionbar_bg"
    android:text="@string/archive"
    android:textSize="16dp"
    android:textColor="@color/text_gray"
    android:gravity="center_horizontal" />
<LinearLayout
    android:id="@+id/story_list_archive"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />     
</LinearLayout>         

Populating the lists:

public class StoryListFragment extends Fragment {   
public StoryListFragment() {}

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_story_list, container,
            false);

    TextView archiveText = (TextView) view.findViewById(R.id.archive);
    archiveText.setText(archiveText.getText().toString().toUpperCase()); 

    final List<Story> storyList = new ArrayList<Story>();
    // filling the list ....

    StoryListAdapter adapter = new StoryListAdapter(getActivity(), R.layout.story_list_item, storyList);        
    LinearLayout inbox = (LinearLayout) view.findViewById(R.id.story_list_inbox);

    final int inboxCount = adapter.getCount();

    for (int i = 0; i < inboxCount; i++) {
      View item = adapter.getView(i, null, null);
      inbox.addView(item);
    }   

    /* archive */   
    // filling the list ....    

    LinearLayout archive = (LinearLayout) view.findViewById(R.id.story_list_archive);

    final int archiveCount = adapter.getCount();

    for (int i = 0; i < archiveCount; i++) {
      View item = adapter.getView(i, null, null);
      archive.addView(item);
    }           

    return view;
}
}

Question:

How should I make the LinearLayout list items clickable to be able to get the index of the clicked item?

What I even want to achieve:

Maybe I'm not even using the right solution, what I'm trying to achieve here is to create something like a message inbox, which consists of two lists - one for newest messages and the second one for the archived messages. I also want to display some content on the top of the lists, so I put it all inside a ScrollView. It should look like this:

enter image description here

SOLUTION:

for (int i = 0; i < inboxCount; i++) {
    final int index = i;

    View item = adapter.getView(i, null, null);
    item.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            System.out.println("Clicked " + index);
            startActivity(new Intent(v.getContext(), StoryIntroActivity.class));
        }
    });
    inbox.addView(item);
}
Community
  • 1
  • 1
milanseitler
  • 765
  • 1
  • 7
  • 21

2 Answers2

0

How should I make the LinearLayout list items clickable to be able to get the index of the clicked item?

You can do as following in order to pass the clicked item's index to another activity where you might want to respond to the click.Inside your onCreate do something like this :

getListView().setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position, long   id)
{
//position is the item number in your listview
Intent intent = new Intent(getApplicationContext(),xyz.class);
intent.putExtra("itemNo.", position);
startActivity(intent);
}
});

Please note that if your class extends ListActivity then only you can use this.getListView() directly as above. If you extend Activity then refer to this answer.

The answer suggests :

Whenever you use Activity you set your_layout.xml as your Activity's ContentView. So the ListView should b in your_layout.xml.

That ListView should have an id attribute defined in xml file say: (android:id="@+id/list"). You get your ListView object some thing like this way:

setContentView(R.layout.your_layout);
ListView list = (ListView)findViewById(R.id.list);
list.addFooterView(view);
Community
  • 1
  • 1
Sash_KP
  • 5,551
  • 2
  • 25
  • 34
0

The solution is to set the onClickListener to dynamically created view like this:

    for (int i = 0; i < inboxCount; i++) {
        final int index = i;

        View item = adapter.getView(i, null, null);
        item.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                System.out.println("Clicked " + index);
                startActivity(new Intent(v.getContext(), StoryIntroActivity.class));
            }
        });
        inbox.addView(item);
    }
milanseitler
  • 765
  • 1
  • 7
  • 21