SherlockFragmentActivity中的ListView(Android)

时间:2013-12-01 18:29:58

标签: android listview android-listview actionbarsherlock

我正在开发一个应用程序,并创建了一个自定义ListView来显示主菜单选项,与此相关的MainActivity扩展了SherlockActivity。相关代码如下所示:

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();

                    }
                });

    }

这使用自定义列表适配器,如下所示:

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;
}
}

我还希望在另一个扩展SherlockFragmentActivity的菜单中显示选项列表,以便用户可以在两个选项列表之间滑动。 数组是应该在SherlockFragmentActivity还是SherlockFragments中定义的?

我认为将以下代码放在SherlockFragments的onCreateView方法中会有效但是错误消息告诉我'构造函数CustomList(SherlockFragment1,String [],Integer [])未定义':

    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();

            }
        });
}

以下是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();

            }
        });
}
}

有人可以帮我创建这些ListViews,用户可以使用类似的代码在我用于MainActivity的方法之间滑动吗?如果我没有提供足够的信息,请告诉我。

非常感谢, 皮特

1 个答案:

答案 0 :(得分:1)

更改此

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

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

同时更改

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

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

编辑:

@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;
}