com.handmark.pulltorefresh.library.PullToRefreshListView 无法转换为 android.widget.ListView
com.handmark.pulltorefresh.library.PullToRefreshListView cannot be cast to android.widget.ListView
我为这个片段页面制作了一个 Activity 页面,并对片段进行了必要的更改,但我遇到了这个错误,请帮助我解决这个问题。
我有一个 activity,它有两个片段,这个是 two.And 中的一个,虽然它是一个列表视图,如果我点击它,第二个显示在相同 [=24= 的另一侧] 页。这就是我想要做的所有主要功能。
public class FreagmentMainCategory extends Fragment{
private LinearLayout llLayoutmain;
private FragmentActivity faActivitymain;
private ListView lstCategoryList;
private SelectCategoryTask mAuthTask = null;
private View mServiceStatusView;
CategoryDetails mainCategory;
private categoryListAdapter adapterList;
static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
static final int MENU_DEMO = 3;
private View lstListView;
private PullToRefreshListView mPullRefreshListView;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//faActivitymain = (FragmentActivity) super.getActivity();
View view = inflater.inflate(R.layout.activity_fragment_main,container, false);
this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
FontUtils.setCustomFont(getActivity().findViewById(R.id.mainView), getActivity().getAssets());
lstListView= view.findViewById(R.id.lstCategoryView);
mPullRefreshListView = (PullToRefreshListView)view.findViewById(R.id.lstCategory);
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(getActivity().getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
}
});
lstListView=(View)view.findViewById(R.id.lstCategoryView);
lstCategoryList = (ListView)view.findViewById(R.id.lstCategory);//got error in this line
//lstCategoryList = (ListView) inflater.inflate(R.layout.activity_fragment_main, container, false);
lstCategoryList = mPullRefreshListView.getRefreshableView();
registerForContextMenu(lstCategoryList);
mServiceStatusView = view.findViewById(R.id.service_status);
//txtTitle=(TextView) getActivity().findViewById(R.id.serviceText);
//txtTitle.setText(getActivity().getResources().getString(R.string.title_select_category));
showProgress(true);
mAuthTask = new SelectCategoryTask();
mAuthTask.execute((Void) null);
lstCategoryList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position,long arg3) {
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
//bundle.putInt(key, value);
//Intent subCategoryIntent = new Intent(getActivity().getApplication(),FragmentSubCategory.class);
bundle.putString("MainCategoryId", mainCategory.masterinfo.get(position-1).maincatid);
bundle.putString("MainCategoryName", mainCategory.masterinfo.get(position-1).maincatname);
//subCategoryIntent.putExtra("MainCategoryId", mainCategory.masterinfo.get(position-1).maincatid);
//subCategoryIntent.putExtra("MainCategoryName", mainCategory.masterinfo.get(position-1).maincatname);
//getActivity().getApplication().startActivity(subCategoryIntent);
fragment.setArguments(bundle);
}
});
return view;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.activity_service, menu);
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
mServiceStatusView.setVisibility(View.VISIBLE);
mServiceStatusView.animate().setDuration(shortAnimTime)
.alpha(show ? 1 : 0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mServiceStatusView.setVisibility(show ? View.VISIBLE
: View.GONE);
}
});
lstListView.setVisibility(View.VISIBLE);
lstListView.animate().setDuration(shortAnimTime)
.alpha(show ? 0 : 1)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
lstCategoryList.setVisibility(show ? View.GONE
: View.VISIBLE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
mServiceStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
lstListView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
public class GetDataTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
// Simulates a background job.
ServerAccess sa=new ServerAccess();
mainCategory=sa.GetMainCategory();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(mainCategory==null){
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_check_network), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB01")){
adapterList=new categoryListAdapter(getActivity().getApplicationContext(), mainCategory.masterinfo);
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB02")){
lstCategoryList.setAdapter(null);
}else{
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_genric_error), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
public class SelectCategoryTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
ServerAccess sa=new ServerAccess();
mainCategory=sa.GetMainCategory();
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if(mainCategory==null){
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_check_network), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB01")){
adapterList=new categoryListAdapter(getActivity().getApplicationContext(), mainCategory.masterinfo);
lstCategoryList.setAdapter(adapterList);
}else if(mainCategory.code.equals("MOB02")){
lstCategoryList.setAdapter(null);
}else{
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_genric_error), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
class categoryListAdapter extends BaseAdapter {
List<Category> mainItems;
Context myContext;
public categoryListAdapter(Context context, List<Category> items) {
myContext = context;
mainItems = items;
}
public int getCount() {
return mainItems.size();
}
public Object getItem(int index) {
return mainItems.get(index);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent ) {
// TODO Auto-generated method stub
RelativeLayout layout = new RelativeLayout(myContext);
TextView tv = new TextView(myContext);
tv.setText(mainItems.get(position).maincatname);
tv.setGravity(Gravity.LEFT);
tv.setTextSize(16);
tv.setPadding(20, 20, 0, 20);
tv.setTextColor(myContext.getResources().getColor(R.color.BlackColor));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layout.addView(tv,lp);
return layout;
}
}
}
这是我的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg_main"
tools:context=".FreagmentMainCategory" >
<LinearLayout
android:id="@+id/lstCategoryView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/serviceHeaderBar"
android:orientation="vertical" >
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lstCategory"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</LinearLayout>
<RelativeLayout
android:id="@+id/service_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/progress_bg"
android:orientation="vertical"
android:visibility="visible" >
<ProgressBar
android:id="@+id/loadingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/service_status_message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:tag="normal"
android:text="@string/title_please_wait"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/BlackColor"
android:textSize="@dimen/MainSize" />
</LinearLayout>
</RelativeLayout>
PullToRefreshListView
是 ListView
的包装器,它不扩展 ListView
,所以你不能直接转换它。您可以将 findViewById
的 return 值转换为 PullToRefreshListView
并调用 getRefreshableView() 来检索底层 ListView
。这一行
lstCategoryList = (ListView)view.findViewById(R.id.lstCategory);
应该是
lstCategoryList = ((PullToRefreshListView)view.findViewById(R.id.lstCategory)).getRefreshableView();
Android 有自己的 SwipeToRefreshLayout 小部件。您确定仍要使用 PullToRefreshListView
吗?
我为这个片段页面制作了一个 Activity 页面,并对片段进行了必要的更改,但我遇到了这个错误,请帮助我解决这个问题。 我有一个 activity,它有两个片段,这个是 two.And 中的一个,虽然它是一个列表视图,如果我点击它,第二个显示在相同 [=24= 的另一侧] 页。这就是我想要做的所有主要功能。
public class FreagmentMainCategory extends Fragment{
private LinearLayout llLayoutmain;
private FragmentActivity faActivitymain;
private ListView lstCategoryList;
private SelectCategoryTask mAuthTask = null;
private View mServiceStatusView;
CategoryDetails mainCategory;
private categoryListAdapter adapterList;
static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
static final int MENU_DEMO = 3;
private View lstListView;
private PullToRefreshListView mPullRefreshListView;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//faActivitymain = (FragmentActivity) super.getActivity();
View view = inflater.inflate(R.layout.activity_fragment_main,container, false);
this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
FontUtils.setCustomFont(getActivity().findViewById(R.id.mainView), getActivity().getAssets());
lstListView= view.findViewById(R.id.lstCategoryView);
mPullRefreshListView = (PullToRefreshListView)view.findViewById(R.id.lstCategory);
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(getActivity().getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
}
});
lstListView=(View)view.findViewById(R.id.lstCategoryView);
lstCategoryList = (ListView)view.findViewById(R.id.lstCategory);//got error in this line
//lstCategoryList = (ListView) inflater.inflate(R.layout.activity_fragment_main, container, false);
lstCategoryList = mPullRefreshListView.getRefreshableView();
registerForContextMenu(lstCategoryList);
mServiceStatusView = view.findViewById(R.id.service_status);
//txtTitle=(TextView) getActivity().findViewById(R.id.serviceText);
//txtTitle.setText(getActivity().getResources().getString(R.string.title_select_category));
showProgress(true);
mAuthTask = new SelectCategoryTask();
mAuthTask.execute((Void) null);
lstCategoryList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position,long arg3) {
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
//bundle.putInt(key, value);
//Intent subCategoryIntent = new Intent(getActivity().getApplication(),FragmentSubCategory.class);
bundle.putString("MainCategoryId", mainCategory.masterinfo.get(position-1).maincatid);
bundle.putString("MainCategoryName", mainCategory.masterinfo.get(position-1).maincatname);
//subCategoryIntent.putExtra("MainCategoryId", mainCategory.masterinfo.get(position-1).maincatid);
//subCategoryIntent.putExtra("MainCategoryName", mainCategory.masterinfo.get(position-1).maincatname);
//getActivity().getApplication().startActivity(subCategoryIntent);
fragment.setArguments(bundle);
}
});
return view;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.activity_service, menu);
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
mServiceStatusView.setVisibility(View.VISIBLE);
mServiceStatusView.animate().setDuration(shortAnimTime)
.alpha(show ? 1 : 0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mServiceStatusView.setVisibility(show ? View.VISIBLE
: View.GONE);
}
});
lstListView.setVisibility(View.VISIBLE);
lstListView.animate().setDuration(shortAnimTime)
.alpha(show ? 0 : 1)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
lstCategoryList.setVisibility(show ? View.GONE
: View.VISIBLE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
mServiceStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
lstListView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
public class GetDataTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
// Simulates a background job.
ServerAccess sa=new ServerAccess();
mainCategory=sa.GetMainCategory();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(mainCategory==null){
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_check_network), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB01")){
adapterList=new categoryListAdapter(getActivity().getApplicationContext(), mainCategory.masterinfo);
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB02")){
lstCategoryList.setAdapter(null);
}else{
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_genric_error), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
public class SelectCategoryTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
ServerAccess sa=new ServerAccess();
mainCategory=sa.GetMainCategory();
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if(mainCategory==null){
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_check_network), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}else if(mainCategory.code.equals("MOB01")){
adapterList=new categoryListAdapter(getActivity().getApplicationContext(), mainCategory.masterinfo);
lstCategoryList.setAdapter(adapterList);
}else if(mainCategory.code.equals("MOB02")){
lstCategoryList.setAdapter(null);
}else{
Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_genric_error), Toast.LENGTH_LONG).show();
lstCategoryList.setAdapter(null);
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
class categoryListAdapter extends BaseAdapter {
List<Category> mainItems;
Context myContext;
public categoryListAdapter(Context context, List<Category> items) {
myContext = context;
mainItems = items;
}
public int getCount() {
return mainItems.size();
}
public Object getItem(int index) {
return mainItems.get(index);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent ) {
// TODO Auto-generated method stub
RelativeLayout layout = new RelativeLayout(myContext);
TextView tv = new TextView(myContext);
tv.setText(mainItems.get(position).maincatname);
tv.setGravity(Gravity.LEFT);
tv.setTextSize(16);
tv.setPadding(20, 20, 0, 20);
tv.setTextColor(myContext.getResources().getColor(R.color.BlackColor));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layout.addView(tv,lp);
return layout;
}
}
}
这是我的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg_main"
tools:context=".FreagmentMainCategory" >
<LinearLayout
android:id="@+id/lstCategoryView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/serviceHeaderBar"
android:orientation="vertical" >
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lstCategory"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</LinearLayout>
<RelativeLayout
android:id="@+id/service_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/progress_bg"
android:orientation="vertical"
android:visibility="visible" >
<ProgressBar
android:id="@+id/loadingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/service_status_message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:tag="normal"
android:text="@string/title_please_wait"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/BlackColor"
android:textSize="@dimen/MainSize" />
</LinearLayout>
</RelativeLayout>
PullToRefreshListView
是 ListView
的包装器,它不扩展 ListView
,所以你不能直接转换它。您可以将 findViewById
的 return 值转换为 PullToRefreshListView
并调用 getRefreshableView() 来检索底层 ListView
。这一行
lstCategoryList = (ListView)view.findViewById(R.id.lstCategory);
应该是
lstCategoryList = ((PullToRefreshListView)view.findViewById(R.id.lstCategory)).getRefreshableView();
Android 有自己的 SwipeToRefreshLayout 小部件。您确定仍要使用 PullToRefreshListView
吗?