我无法对我的活动扩展到listactivity。帮助需要我的、扩展到、listactivity

2023-09-07 15:13:18 作者:- 谎言大师i

我无法对我的活动扩展到listactivity。我希望把它扩大到listactivity和onclicklistener添加到列表中的项目。

 公共类MainActivity延伸活动{

    私人的ListView LVIEW;
    私人ArrayList的结果=新的ArrayList();

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        LVIEW =(ListView控件)findViewById(R.id.lvApps);
        PackageManager下午= this.getPackageManager();

        意向意图=新的意图(Intent.ACTION_MAIN,NULL);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        名单< ResolveInfo>表= pm.queryIntentActivities(意向,PackageManager.PERMISSION_GRANTED);
        对于(ResolveInfo rInfo:名单){
            results.add(rInfo.activityInfo.applicationInfo
                .loadLabel(下午)的ToString());
            Log.w(已安装的应用程序,rInfo.activityInfo.applicationInfo
                .loadLabel(下午)的ToString());
        }
        lView.setAdapter(新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,结果));
    }
}
 

解决方案

使用下面这段code:

 公共类MainActivity扩展ListActivity实现OnItemClickListener {

    私人ArrayList的结果=新的ArrayList();

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
//的setContentView(R.layout.activity_main);

        PackageManager下午= this.getPackageManager();

        意向意图=新的意图(Intent.ACTION_MAIN,NULL);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        名单< ResolveInfo>表= pm.queryIntentActivities(意向,PackageManager.PERMISSION_GRANTED);
        对于(ResolveInfo rInfo:名单){
            results.add(rInfo.activityInfo.applicationInfo
                .loadLabel(下午)的ToString());
            Log.w(已安装的应用程序,rInfo.activityInfo.applicationInfo
                .loadLabel(下午)的ToString());
        }
                getListView()setOnItemClickListener(本)。

        setListAdapter(新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,结果));
    }

@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
    // TODO自动生成方法存根

}

}
 
两个代码一模一样,为什么上面的报错下面的可以运行

释解:

ListActivity有一个默认的布局,由一个单一的,全屏幕列表中,你可以直接设置适配器screen.so的中心。

看一看文档仅供参考

我希望这会有所帮助!

I am unable to extend my activity to listactivity. I want to extend it to listactivity and add onclicklistener to the list items.

public class MainActivity extends Activity {

    private ListView lView;
    private ArrayList results = new ArrayList();

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

        lView = (ListView) findViewById(R.id.lvApps);
        PackageManager pm = this.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List < ResolveInfo > list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo: list) {
            results.add(rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
        }
        lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }
}

解决方案

Use following piece of code:

    public class MainActivity extends ListActivity implements OnItemClickListener{

    private ArrayList results = new ArrayList();

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

        PackageManager pm = this.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List < ResolveInfo > list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo: list) {
            results.add(rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
        }
                getListView().setOnItemClickListener(this);

        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

}

Explaination:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen.so you can directly set the adapter.

Have a look at docs for reference

I hope it will be helpful !!