Android Fragment BaseAdapter 不工作
Android Fragment BaseAdapter not working
此代码在 Activity
中正常工作,我将所有 Context
更改为 getActivity()
但是在 adapter=new ListViewAdapter(getActivity(), arraylist);
之后将其复制到 Fragment
时出现错误 当 mylist.setAdapter(adapter)
为 运行 时,我的 class ListViewAdapter 错误
如何解决?
片段
public class Fragment1 extends Fragment {
DataBaseHelper myDbHelper;
ListView mylist;
ArrayList<String> iran,arr_intityid,arrshomare,primarykeyarrlist;
String[] fa,mysid,myshomare,primarykeyarr;
SQLiteDatabase db;
Cursor mCursor;
int translate;
EditText mysearch;
int [] favorit;
ArrayList<Integer> favor;
ListViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myview = inflater.inflate(R.layout.search_fragment, container, false);
mylist=(ListView) myview.findViewById(R.id.mylist);
return myview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDbHelper = new DataBaseHelper(getActivity().getApplicationContext());
myDbHelper = new DataBaseHelper(getActivity());
iran=new ArrayList<String>();
arr_intityid=new ArrayList<String>();
arrshomare=new ArrayList<String>();
primarykeyarrlist=new ArrayList<String>();
favor=new ArrayList<Integer>();
try {
myDbHelper.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
ArrayList<myitems> arraylist = new ArrayList<myitems>();
db = myDbHelper.getReadableDatabase();
mCursor= db.rawQuery("SELECT field_translation_id_value,title,translate,my_id,favorites FROM khotbe"
+ " WHERE translate =1 ORDER BY field_translation_id_value ASC ;",null);
int i = 1;
mCursor.moveToFirst();
while (mCursor.isAfterLast() == false) {
iran.add(mCursor.getString(1));
arr_intityid.add(mCursor.getString(0));
primarykeyarrlist.add(mCursor.getString(3));
favor.add(mCursor.getInt(4));
arrshomare.add(""+i++);
mCursor.moveToNext();
}
fa= new String [iran.size()];
fa=iran.toArray(fa);
mysid= new String [arr_intityid.size()];
mysid=arr_intityid.toArray(mysid);
primarykeyarr= new String [primarykeyarrlist.size()];
primarykeyarr=primarykeyarrlist.toArray(primarykeyarr);
myshomare=new String[arrshomare.size()];
myshomare=arrshomare.toArray(myshomare);
favorit =new int[favor.size()];
for (int t= 0; t < favor.size(); t++) {
favorit[t]=favor.get(t).intValue();
}
mCursor.moveToPosition(0);
for (int j = 0; j < fa.length; j++) {
myitems qqq= new myitems(fa[j],mysid[j],myshomare[j],primarykeyarr[j],favorit[j]);
arraylist.add(qqq);
}
Log.i("sssssss",getActivity()+"");
adapter=new ListViewAdapter(getActivity(), arraylist);
Log.i("sssssss", "2222222222222");
mylist.setAdapter(adapter);
}
class ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
LayoutInflater inflater;
private List<myitems> worldpopulationlist = null;
private ArrayList<myitems> arraylist;
int myfavorit =0;
public ListViewAdapter(Context context, List<myitems> worldpopulationlist) {
// mContext = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(getActivity());
this.arraylist = new ArrayList<myitems>();
this.arraylist.addAll(worldpopulationlist);
}
public class ViewHolder {
TextView rank;
TextView country;
TextView population;
Button btn;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public myitems getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.myitems_list, null);
holder.rank = (TextView) view.findViewById(R.id.txt25);
holder.btn=(Button) view.findViewById(R.id.btnfavor);
holder.country = (TextView) view.findViewById(R.id.txt26);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.rank.setText(worldpopulationlist.get(position).gettitle());
holder.country.setText(worldpopulationlist.get(position).getshomare()+"-");
if(myfavorit==0){
holder.btn.setBackgroundResource(R.drawable.ic_launcher);
}
else{
holder.btn.setBackgroundResource(R.drawable.star);
}
return view;
}
}
因为onCreate方法比onCreateView触发的早
onCreateView方法中一定是这样
mylist=(ListView) myview.findViewById(R.id.mylist);
adapter = new ListViewAdapter(getActivity(), arraylist);
mylist.setAdapter(adapter);
P.S。请访问 http://developer.android.com/guide/components/fragments.html
和
此代码在 Activity
中正常工作,我将所有 Context
更改为 getActivity()
但是在 adapter=new ListViewAdapter(getActivity(), arraylist);
之后将其复制到 Fragment
时出现错误 当 mylist.setAdapter(adapter)
为 运行 时,我的 class ListViewAdapter 错误
如何解决?
片段
public class Fragment1 extends Fragment {
DataBaseHelper myDbHelper;
ListView mylist;
ArrayList<String> iran,arr_intityid,arrshomare,primarykeyarrlist;
String[] fa,mysid,myshomare,primarykeyarr;
SQLiteDatabase db;
Cursor mCursor;
int translate;
EditText mysearch;
int [] favorit;
ArrayList<Integer> favor;
ListViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myview = inflater.inflate(R.layout.search_fragment, container, false);
mylist=(ListView) myview.findViewById(R.id.mylist);
return myview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDbHelper = new DataBaseHelper(getActivity().getApplicationContext());
myDbHelper = new DataBaseHelper(getActivity());
iran=new ArrayList<String>();
arr_intityid=new ArrayList<String>();
arrshomare=new ArrayList<String>();
primarykeyarrlist=new ArrayList<String>();
favor=new ArrayList<Integer>();
try {
myDbHelper.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
ArrayList<myitems> arraylist = new ArrayList<myitems>();
db = myDbHelper.getReadableDatabase();
mCursor= db.rawQuery("SELECT field_translation_id_value,title,translate,my_id,favorites FROM khotbe"
+ " WHERE translate =1 ORDER BY field_translation_id_value ASC ;",null);
int i = 1;
mCursor.moveToFirst();
while (mCursor.isAfterLast() == false) {
iran.add(mCursor.getString(1));
arr_intityid.add(mCursor.getString(0));
primarykeyarrlist.add(mCursor.getString(3));
favor.add(mCursor.getInt(4));
arrshomare.add(""+i++);
mCursor.moveToNext();
}
fa= new String [iran.size()];
fa=iran.toArray(fa);
mysid= new String [arr_intityid.size()];
mysid=arr_intityid.toArray(mysid);
primarykeyarr= new String [primarykeyarrlist.size()];
primarykeyarr=primarykeyarrlist.toArray(primarykeyarr);
myshomare=new String[arrshomare.size()];
myshomare=arrshomare.toArray(myshomare);
favorit =new int[favor.size()];
for (int t= 0; t < favor.size(); t++) {
favorit[t]=favor.get(t).intValue();
}
mCursor.moveToPosition(0);
for (int j = 0; j < fa.length; j++) {
myitems qqq= new myitems(fa[j],mysid[j],myshomare[j],primarykeyarr[j],favorit[j]);
arraylist.add(qqq);
}
Log.i("sssssss",getActivity()+"");
adapter=new ListViewAdapter(getActivity(), arraylist);
Log.i("sssssss", "2222222222222");
mylist.setAdapter(adapter);
}
class ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
LayoutInflater inflater;
private List<myitems> worldpopulationlist = null;
private ArrayList<myitems> arraylist;
int myfavorit =0;
public ListViewAdapter(Context context, List<myitems> worldpopulationlist) {
// mContext = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(getActivity());
this.arraylist = new ArrayList<myitems>();
this.arraylist.addAll(worldpopulationlist);
}
public class ViewHolder {
TextView rank;
TextView country;
TextView population;
Button btn;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public myitems getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.myitems_list, null);
holder.rank = (TextView) view.findViewById(R.id.txt25);
holder.btn=(Button) view.findViewById(R.id.btnfavor);
holder.country = (TextView) view.findViewById(R.id.txt26);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.rank.setText(worldpopulationlist.get(position).gettitle());
holder.country.setText(worldpopulationlist.get(position).getshomare()+"-");
if(myfavorit==0){
holder.btn.setBackgroundResource(R.drawable.ic_launcher);
}
else{
holder.btn.setBackgroundResource(R.drawable.star);
}
return view;
}
}
因为onCreate方法比onCreateView触发的早
onCreateView方法中一定是这样
mylist=(ListView) myview.findViewById(R.id.mylist);
adapter = new ListViewAdapter(getActivity(), arraylist);
mylist.setAdapter(adapter);
P.S。请访问 http://developer.android.com/guide/components/fragments.html
和