将ListView放在片段活动中

时间:2014-06-20 06:00:11

标签: java android xml android-fragments android-listview

在我的Android应用中,我正在尝试将ListView放入FragmentActivity中。不幸的是没有FragmentListActivity这样的东西。问题是我无法调用setListAdapter()和onListItemClick()方法。

所以我做了很多研究,然后我去了我的XML文件并在那里手动添加了ListView。然后我代替getListView(),声明了一个ListView变量,所以我可以在我的新ListView变量上调用方法。不幸的是,该方法仍然存在错误。该方法无法解决等。

这是我的java代码:

package com.spicycurryman.getdisciplined10.app;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.javatechig.listapps.ApplicationAdapter;

import java.util.ArrayList;
import java.util.List;


public class InstalledAppActivity extends FragmentActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private ApplicationAdapter listadaptor = null;


    //Implementing the Listview Programatically
    ListView InstalledAppList = (ListView) findViewById(R.id.Installed_List);


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        packageManager = getPackageManager();
        new LoadApplications().execute();
        return inflater.inflate(R.layout.installed_apps, container, false);


    }


    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.block, menu);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result = true;

        switch (item.getItemId()) {
            case R.id.main_text: {
                displayAboutDialog();

                break;
            }
            default: {
                result = super.onOptionsItemSelected(item);

                break;
            }
        }

        return result;
    }

    private void displayAboutDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.app_name));
        builder.setMessage(getString(R.string.slogan));


        builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://javatechig.com"));
                startActivity(browserIntent);
                dialog.cancel();
            }
        });
        builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        builder.show();
    }


    //here
    InstalledAppList.setOnItemClickListener(new OnItemClickListener()){

        @Override
        public void onItemClick(AdapterView <?> arg0, View view, int index, long id){
            ApplicationInfo app = applist.get(index);
            try {
                Intent intent = packageManager
                        .getLaunchIntentForPackage(app.packageName);

                if (null != intent) {
                    startActivity(intent);
                }
            } catch (ActivityNotFoundException e) {
                Toast.makeText(InstalledAppActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(InstalledAppActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });


    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return applist;
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
            listadaptor = new ApplicationAdapter(InstalledAppActivity.this,
                    R.layout.snippet_list_row, applist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result) {
            //setListAdapter(listadaptor);
            InstalledAppList.setAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(InstalledAppActivity.this, null,
                    "Loading application info...");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

这是我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >

    <ListView
        android:id="@+id/Installed_List"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

试试这个..

您错过了onCreate

ListView InstalledAppList;

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

    InstalledAppList = (ListView) findViewById(R.id.Installed_List);
    packageManager = getPackageManager();
    new LoadApplications().execute();

    InstalledAppList.setOnItemClickListener(new OnItemClickListener()){

    @Override
    public void onItemClick(AdapterView <?> arg0, View view, int index, long id){
        ApplicationInfo app = applist.get(index);
        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(InstalledAppActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(InstalledAppActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }
});
}

并删除onCreateView并在ItemClickListener

中添加onCreate

答案 1 :(得分:0)

public class MyAndroidVersionListFragment extends ListFragment{ 







    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){


        CustomDoctorsListAdapter adapter= new CustomDoctorsListAdapter(getActivity(), getResources().getStringArray(R.array.doctors_name), getResources().getStringArray(R.array.doctors_address),
                getResources().getStringArray(R.array.doctors_category), getResources().obtainTypedArray(R.array.doctors_rating), getResources().getIntArray(R.array.doctors_clinic_distance));
        getResources().obtainTypedArray(R.array.doctors_rating).recycle();

       android.R.layout.simple_list_item_multiple_choice, android_versions);
        setListAdapter(adapter);
        Toast.makeText(getActivity(), "Mypostion"+position+"", Toast.LENGTH_LONG).show();


        return super.onCreateView(inflater, container, savedInstanceState);

    }    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        position = getArguments().getInt("second");

    }

    @Override
    public void onStart() {     
        super.onStart();
       getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);



    }

尝试像这样的List片段希望它能帮到你