0

I have a listview in a fragment here is my current code for it:

public static class AllSectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public AllSectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tasks_all,
                container, false);
        // /////////////////////////////////////////////////////////////////
        // Set up all components here:: ie text views , buttons lists etc.//
        // that correspond to the layout fragment xml file                //
        ////////////////////////////////////////////////////////////////////
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.fragment_tasks_all_textView);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));

        ArrayList<String> arrayList = new ArrayList<String>();
        ListView allList = (ListView) rootView
                .findViewById(R.id.fragment_tasks_all_list);
        MyCustomAdapter mAdapter = new MyCustomAdapter(getActivity(),
                arrayList); // Class to populate a ListView with an
                            // ArrayList
        allList.setAdapter(mAdapter);
        // Populate array list
        for (int i = 0; i < 5; i++) {
            arrayList.add(" All Task " + i);
        }
        mAdapter.notifyDataSetChanged();

        return rootView;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        System.out.println("pos: "+ position);
    }
}

and my layout.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TasksActivity$DummySectionFragment" >

<TextView
    android:id="@+id/fragment_tasks_all_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/fragment_tasks_all_list"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.41"
    android:cacheColorHint="#00000000"
    android:clickable="true"
    android:listSelector="@android:color/transparent"
    android:transcriptMode="alwaysScroll" >

</ListView>

First of all. how can i make it so that i can click on an item in the list,

and second of all how do i add the onclick listener for it.

1
  • 1
    Ok i have got the on click listener working.. I just added: allList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> p, View view, int position, long id) { System.out.println("pos: " + position); } }); but now when i click on the list view on the device it doesn't high light it or show it as being clicked. but my log cat shows that i clicked it. Commented Apr 3, 2013 at 15:23

3 Answers 3

2

Why dont you just use ListFragment:

  public static class ArrayListFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, Shakespeare.TITLES));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But then i can only put an array list in my fragment? I can i still add buttons and text etc ?
why wont you separate this fragment for 2 or 3 fragment with their responsibilities. meaning create a separate ListFragment and a normal Fragment for your buttons and text. that way you can reuse them in the future.
0

Use the alternative constructor for ArrayAdapter that allows you to specify a layout file to host your listview:

public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

Comments

0

I had a similar issue and was resolved by creating a interface with onClick method and implementing it in fragment. Try do this:

Create a interface:

MyOnViewClickListener.java

public interface MyOnViewClickListener {
    public void myOnViewClickListener(View v);
}

In your custom adapter class, create a constructor passing MyOnViewClickListener as argument:

private Context context;
private MyOnViewClickListener itemListener;
private List<String> items;

public MyCustomAdapter(Context context, List<String> items, MyOnViewClickListener itemListener) {

        this.itemListener = itemListener;
        this.context = context;
        this.items = items;
}

Inside getView() method, put:

LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.your_row_layout, parent, false);

TextView textView1 = (TextView) row.findViewById(R.id.quizchallenge);
    textView1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            itemListener.myOnViewClickListener(v);
        }
});

Finally, in your fragment class, implement the new interface:

public class ListFriendFragment extends Fragment implements MyOnViewClickListener  {
.
.
.
@Override
public void myOnViewClickListener(View v){
    //call your method
    quickQuiz();
}

Set up the adapter:

adapter = new MyCustomAdapter(getContext(), myItemsList, MyFragment.this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.