petehallw
petehallw

Reputation: 1014

ListView within SherlockFragmentActivity (Android)

I'm developing an app and have created a custom ListView to display the main menu options, and the MainActivity relating to this extends SherlockActivity. The relevant code is shown below:

public class MainActivity extends SherlockActivity {

ListView list;
String[] web = {
        "Option1",
        "Option2",
        "Option3",
        "Option4",
        "Option5",
        "Option6",
        "Option7"
} ;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        CustomList adapter = new
                CustomList(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

                    }
                });

    }

This uses a custom list adapter as shown:

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.main_menu, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.main_menu, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}

I also want to display lists of options in another menu which extends SherlockFragmentActivity so the user can swipe between two lists of options. Should the array be defined within the SherlockFragmentActivity or the SherlockFragments themselves?

I thought placing the following code in the onCreateView method of the SherlockFragments would work however an error message tells me 'The constructor CustomList(SherlockFragment1, String[], Integer[]) is undefined':

    View view = inflater.inflate(R.layout.activity_frag1, container, false);
return view;

CustomList adapter = new
        CustomList(SherlockFragment1.this, web, imageId);
list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(SherlockFragment2.this, "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });
}

Here is the full code for SherlockFragment1:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;

public class SherlockFragment1 extends SherlockFragment {

ListView list;
String[] web = {
        "Option1",
        "Option2",
        "Option3",
        "Option4",
        "Option5",
        "Option6",
        "Option7"
} ;
Integer[] imageId = {
        R.drawable.ic_page,
        R.drawable.ic_page,
        R.drawable.ic_page,
        R.drawable.ic_page,
        R.drawable.ic_help,
        R.drawable.ic_settings,
        R.drawable.ic_web

};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
// Inflating the layout view  to this fragment
View view = inflater.inflate(R.layout.activity_frag1, container, false);
return view;

CustomList adapter = new
        CustomList(getActivity(), web, imageId);
list=(ListView)view.findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(SherlockFragment1.this, "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });
}
}

Could anyone please help me create these ListViews which the user can swipe between using similar code to the method I have used for MainActivity? Please let me know if I have not provided enough information.

Many thanks, Pete

Upvotes: 0

Views: 340

Answers (1)

Raghunandan
Raghunandan

Reputation: 133570

Change this

CustomList adapter = new
        CustomList(SherlockFragment1.this, web, imageId);
list=(ListView)findViewById(R.id.list);

To

 CustomList adapter = new
        CustomList(getActivity(), web, imageId);
 // can use getSherlockActivity() instead of getActivity()
list=(ListView)view.findViewById(R.id.list);

Also change

Toast.makeText(SherlockFragment2.this, "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

To

Toast.makeText(getActivity(), "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

Edit:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_frag1, container, false);
CustomList adapter = new
        CustomList(getActivity(), web, imageId);
list=(ListView)view.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getActivity(), "You clicked " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });
    return view;
}

Upvotes: 1

Related Questions