1

我有一个创建 2 个选项卡的主要活动 (FragmentDemoActivity)。片段 A 和片段 B。我有另一个名为 CategoryList 的类,它从 sqlite 数据库加载自定义列表。我正在使用这个对 TAB 部分非常有用的示例。

我想将 CategoryList 加载到 Fragment 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);
        }
    });
} 

}

我的 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);


}

}

任何指针?

4

0 回答 0