使用选项卡在片段中加载Listview(Actionbarsherlock)

时间:2013-05-11 14:56:51

标签: android-listview android-fragments

我有一个主要活动(FragmentDemoActivity),可以创建2个标签。片段A和片段B. 我有另一个名为CategoryList的类,它从sqlite数据库加载一个自定义列表。 我正在使用this example,这对TAB部分很有用。

我想将CategoryList加载到片段A中。

我的CategoryList类如下

// All the imports
public class CategoryList extends BaseActivity{

ImageView imgFeedback;
ImageView imgAbout;
ListView listCategories;
ProgressBar prgLoading;
TextView txtAlert;
AdView ads;
static String[] CategoryName ;
ListAdapter la;
static int[] id;
List<String> catList;
static DBHelper dbhelper;

class ListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    @SuppressWarnings("unused")
    private Context ctx;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        ctx = context;
    }

    @Override
    public int getCount() {
        return CategoryName.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.imgPreview = (ImageView) convertView.findViewById(R.id.imgPreview);
            holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }


        holder.imgPreview.setImageResource(getResources().
                getIdentifier(CategoryName[position], "drawable", getPackageName()));

        String categoryName = CategoryName[position];
        categoryName = categoryName.toLowerCase();
        categoryName = Character.toString(categoryName.charAt(0)).toUpperCase()+categoryName.substring(1);
        holder.txtRecipeName.setText(categoryName);


        return convertView;
    }

    class ViewHolder {
        ImageView imgPreview;
        TextView txtRecipeName;
    }
}

final Context context = this ;
   /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categories_list);
  AppRater.displayAsk2Rate(this, 1, 1, false);
  getSupportActionBar().setHomeButtonEnabled(true);

    dbhelper = new DBHelper(this);

    try {
        dbhelper.createDataBase();
    }catch(IOException ioe){
        throw new Error("Unable to create database");
    }

    /** then, the database will be open to use */
    try{
        dbhelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }

    CategoryName = dbhelper.getAllCategories();
    dbhelper.close();

    catList = Arrays.asList(CategoryName);


   /* imgAbout = (ImageView) findViewById(R.id.imgAbout);
    imgAbout.setOnClickListener(new ButtonListner(CategoryList.this));

    imgFeedback = (ImageView) findViewById(R.id.imgFeedback);
    imgFeedback.setOnClickListener(new ButtonListner(CategoryList.this));*/

    listCategories = (ListView) findViewById(R.id.listCategories);
    la = new ListAdapter(CategoryList.this);
    listCategories.setAdapter(la);

    txtAlert = (TextView) findViewById(R.id.txtAlert);
    ads = (AdView) findViewById(R.id.ads);
    Ads.loadAds(ads);

    listCategories.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            String category = catList.get(position);

            Intent i = new Intent(CategoryList.this, RecipesList.class);
            i.putExtra("Category_Name", category);
            startActivity(i);
        }
    });
} 

}

My FragmentA如下

public class FragmentA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
{
    View view = inflater.inflate(R.layout.categories_list, container, false);
       ListView listCategories = (ListView) getActivity().findViewById(R.id.listCategories);
       ListAdapter lav = new ListAdapter(CategoryList.this);
       listCategories.setAdapter(lav);
       return view;
}
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);


}

}

任何指针?

0 个答案:

没有答案