I am unable to extend my activity to listactivity. help needed

被刻印的时光 ゝ 提交于 2019-12-02 05:24:51

问题


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

回答1:


if you're gonna use a ListActivity then you don't need this line:

ListView lView = (ListView) findViewById(R.id.lvApps);

BUT that specific ListView being referred to right now (provided it's in the corresponding xml layout) must have it's id changed to

<ListView
  android:id="@android:id/list"
.....



回答2:


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 !!




回答3:


implement OnItemClickListener

public class MainActivity extends ListActivity implements OnItemClickListener
{
   //your code;

    @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    // TODO Auto-generated method stub
    results.get(pos);  //this will give you the value in the clicked list item as per your code
}
}


来源:https://stackoverflow.com/questions/16563051/i-am-unable-to-extend-my-activity-to-listactivity-help-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!