简体   繁体   English

SherlockFragmentActivity(Android)中的ListView

[英]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. 我正在开发一个应用程序,并创建了一个自定义ListView来显示主菜单选项,与此相关的MainActivity扩展了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. 我还想在扩展SherlockFragmentActivity的另一个菜单中显示选项列表,以便用户可以在两个选项列表之间滑动。 Should the array be defined within the SherlockFragmentActivity or the SherlockFragments themselves? 应该在SherlockFragmentActivity或SherlockFragments本身中定义数组吗?

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': 我以为将以下代码放在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();

            }
        });
}

Here is the full code for SherlockFragment1: 这是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? 谁能帮助我创建这些ListView,用户可以使用与我用于MainActivity的方法类似的代码在这些ListView之间滑动? Please let me know if I have not provided enough information. 如果我没有提供足够的信息,请告诉我。

Many thanks, Pete 非常感谢,皮特

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

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

相关问题 Android SherlockFragmentActivity调用SherlockFragment - Android SherlockFragmentActivity call SherlockFragment 具有多个ListFragments的Android SherlockFragmentActivity - Android SherlockFragmentActivity with multiple ListFragments Android SherlockFragmentActivity-选项卡 - Android SherlockFragmentActivity - Tab Android:在SherlockActivity中调用了onOptionsItemSelected(),但在SherlockFragmentActivity中未调用 - Android: onOptionsItemSelected() called in SherlockActivity, but not in SherlockFragmentActivity 在Android的SherlockFragmentActivity的SherlockFragment中未调用onResume()吗? - onResume() is not called in SherlockFragment of SherlockFragmentActivity in Android? 如何刷新SherlockFragmentActivity的FragmentTabHost中存在的listview? - How to Refresh listview present in FragmentTabHost of SherlockFragmentActivity? 在SherlockFragmentActivity上为ListView设置适配器时,nullPointerException - nullPointerException when setting adapter for a ListView on SherlockFragmentActivity 在SherlockFragmentActivity中更新FragmentPagerAdapter中的片段视图 - Update Fragment View within FragmentPagerAdapter in SherlockFragmentActivity Android应用程序中ListView中的ListView - ListView within ListView in android application ListView中的Android EditText - Android EditText within a ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM