在Fragment中使用ListView时,适配器不会调用getView()

时间:2012-12-12 13:54:14

标签: java android listview android-fragments adapter

因此,在编写应用程序时,我遇到了另一个“惊喜”问题。我知道有ListFragment,但使用片段。当我为ListView的一个片段添加标记时,当你在列表中调用适配器时,无法添加元素 - 这不是我在日志中跟踪的一种狡猾的方法,你没有调用getView()。更有趣的是 - 如果我不使用而不只是片段的列表,并且添加适配器在所有事情都很好 - 列表已填满。我无法理解为什么会发生这种情况 - 为什么在片段列表中的作品不够充分。

activity_start.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:scrollbars="vertical"
    android:scrollbarStyle="insideInset"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:background="#e8e8e8" 
    >

<Button 
    android:id="@+id/button_add_url"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:drawableRight="@drawable/green_add"
    android:drawablePadding="5dp"
    android:text="ADD URL"
    />    

 <LinearLayout
    android:id="@+id/linear_layout_list_show_monitor_url"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_background"
    android:orientation="vertical"
    android:padding="1dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    >


   <!--  <ListView
        android:id="@+id/list_view_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> -->    

     <fragment
         android:id="@+id/fragment_list"
         android:name="pront.android.exservice.FragmentList"
         android:layout_width="match_parent"
         android:layout_height="300dp"/>           

 </LinearLayout>    

StartActivity.java

import java.util.ArrayList;
import pront.android.exservice.FragmentDialogAddNewUrl.FragmentDialogAddNewUrlConectActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout.LayoutParams;
import android.widget.Toast;

public class StartActivity 
                        extends FragmentActivity 
                        implements OnClickListener,
                                   FragmentDialogAddNewUrlConectActivity
{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    this.getWindow().addFlags
    (WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_start);
    listViewUrl = (ListView) findViewById(R.id.list_view_fragment);
    screenWidthPx = this.getResources().getDisplayMetrics().widthPixels;
    screenHeightPx = this.getResources().getDisplayMetrics().heightPixels;
    layoutMarginPx = 16;
    screenDp = this.getResources().getDisplayMetrics().density;
    layoutMarginDp = (int)(layoutMarginPx*screenDp);
    typefaceRoboto = Typeface.createFromAsset(getAssets(),
            "Roboto-Thin.ttf");
    LinearLayout linearLayout = (LinearLayout) 
            findViewById(R.id.linear_layout_list_show_monitor_url);
    LayoutParams layoutParams = 
            new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);        
    layoutParams.setMargins(layoutMarginDp, layoutMarginDp,
            layoutMarginDp, layoutMarginDp);
    linearLayout
        .setLayoutParams(layoutParams); 
    buttonAddUrl = (Button) findViewById(R.id.button_add_url);
    buttonAddUrl.setTypeface(typefaceRoboto, Typeface.BOLD);
    buttonAddUrl.setOnClickListener(this);        
    broadcastReceiver = new MyBroadcastReceiver();
    intentFilter = new IntentFilter(BROADCAST);
    registerReceiver(broadcastReceiver, intentFilter);
}

public void onStart(){
    super.onStart();
}

public void onRestart(){
    super.onRestart();
}

public void onResume(){
    super.onResume();
}

public void onPause(){
    super.onPause();
}

public void onStop(){
    super.onStop();
}

public void onDestroy(){
    unregisterReceiver(broadcastReceiver);
    super.onDestroy();
}

@Override
public void onBackPressed(){
    super.onBackPressed();

}

public void onClick(View view) {
    if(view.getId() == R.id.button_add_url){
        buttonAddUrl.setClickable(false);
        fragmentDialogAddNewUrl = new FragmentDialogAddNewUrl();
        fragmentDialogAddNewUrl.show(this.getSupportFragmentManager(),
                "fragmentDialogAddNewUrl");
        buttonAddUrl.setClickable(true);
    }        
} 

public void fragmentDialogClickButtonListener(String url, 
                                                String pathFavIcon)
{
    fragmentDialogAddNewUrl.onDestroyView();        
    this.url = url;
    Toast toastInfo = Toast
                    .makeText(this.getApplicationContext(),
                    "Service start monitor\n"+url,
                    Toast.LENGTH_LONG);
    toastInfo.setGravity(Gravity.TOP, 0, 0);
    toastInfo.show(); 
    arrayListUrl.add(new UrlBox(this.getApplicationContext(),
            url,
            pathFavIcon));
    Log.d("StartActivity", "fragmentDialogClick...() listViewUrl.setAdapter()");
    FragmentList fragmentList = (FragmentList) this.getSupportFragmentManager().findFragmentById(R.id.fragment_list);
    fragmentList.setAdapter(arrayListUrl);
//        MyListAdapter adapter = new MyListAdapter(this, R.layout.expandable_list_view_child_item, arrayListUrl);
//        adapter.notifyDataSetChanged();
//        listViewUrl.setAdapter(adapter);                
}

public static class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("", intent.getIntExtra("RESULT", 0)+"");                      
    }

}   

private Button buttonAddUrl;
private ListView listViewUrl;

private MyBroadcastReceiver broadcastReceiver;    
private IntentFilter intentFilter;
private Typeface typefaceRoboto;
private DialogFragment fragmentDialogAddNewUrl;

private MyListAdapter myListAdapter = null;
private ArrayList<UrlBox> arrayListUrl = new ArrayList<UrlBox>();
private int layoutMarginPx;    
private float screenDp;
private int screenWidthPx;
private int screenHeightPx;
private int layoutMarginDp;
private ArrayList<String> childUrl = new ArrayList<String>();
private ArrayList<Drawable> childFavIcon = new ArrayList<Drawable>();
private String url;
private final static String BROADCAST = "pront.android.exservice";

}

FragmentList.java

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class FragmentList extends Fragment {

@Override
public void onAttach(Activity activity){ 
    super.onAttach(activity);
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);     
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    listView = (ListView) inflater.inflate(R.layout.layout_fragment_list, null, false).findViewById(R.id.list_view_fragment);
    return inflater.inflate(R.layout.layout_fragment_list, null, false);
}

public void setAdapter(ArrayList<UrlBox> arrayList){
    listAdapter = new MyListAdapter(getActivity(), R.layout.expandable_list_view_child_item, arrayList);
    listAdapter.notifyDataSetChanged();
    if(listView != null){
        listView.setAdapter(listAdapter);           
        System.out.println("set adapter");
    }        
}

MyListAdapter listAdapter;
ListView listView;  

}

MyListAdapter.java

import java.util.ArrayList;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyListAdapter extends ArrayAdapter{

MyListAdapter(Activity context, int layoutResourceId, ArrayList<UrlBox> arrayList) {
    super(context, layoutResourceId);
    Log.d("MyListAdapter", "constructor");
    this.context = context;
    this.arrayListUrlBox = arrayList;
    this.layoutResourceId = layoutResourceId;
}

@Override
public View getView (int position, View convertView, ViewGroup parent){
    Log.d("MyListAdapter", "getView");
    View returnView = convertView;
    ListItemTagHolder tag  = null;
    if(convertView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        returnView = inflater.inflate(layoutResourceId, null, false);
        tag = new ListItemTagHolder();
        tag.ImageViewFavicon = (ImageView) returnView.findViewById(R.id.image_view_favicon);
        tag.ImageViewStatus = (ImageView) returnView.findViewById(R.id.image_view_status);
        tag.TextViewUrl = (TextView) returnView.findViewById(R.id.text_view_url);
        returnView.setTag(tag);
    }
    else{
        tag = (ListItemTagHolder) returnView.getTag();
    }
    tag.ImageViewFavicon.setImageDrawable(arrayListUrlBox.get(position).getDrawableFavIcon());
    tag.ImageViewStatus.setImageDrawable(arrayListUrlBox.get(position).getDrawableFavIcon()); // !
    tag.TextViewUrl.setText(arrayListUrlBox.get(position).getUrl());
    return returnView;      
}

 @Override
 public int getCount() {
     return arrayListUrlBox.size();
 }

 public UrlBox getItem(int position) {
     return arrayListUrlBox.get(position);
 }

private ArrayList<UrlBox> arrayListUrlBox;
private Activity context;
private int layoutResourceId;

static class ListItemTagHolder{
    protected ImageView ImageViewFavicon;
    protected ImageView ImageViewStatus;
    protected TextView TextViewUrl;
}   
}

2 个答案:

答案 0 :(得分:1)

该问题与MyListAdapter有关。您有两种选择:

  1. 覆盖getCount()并让它返回数据集中的项目数
  2. 传递给您的数据集super()

    例如,在您的适配器中:

    @Override
    public int getCount() {
       return arrayListUrlBox.size();
    }
    

    或在MyListAdapter构造函数中,您可以调用:

     super(context, layoutResourceId, arrayList);
    

    而不是

    super(context, layoutResourceId);
    

答案 1 :(得分:0)

您可以修改MyListAdapter,如下所示

public class MyListAdapter extends ArrayAdapter**<UrlBox>** {

  MyListAdapter(Activity context, int layoutResourceId, ArrayList<UrlBox> arrayList) {
 super(context, layoutResourceId, arrayList);
 Log.d("MyListAdapter", "constructor", **arrayList**);
 this.context = context;
 this.arrayListUrlBox = arrayList;
 this.layoutResourceId = layoutResourceId;
}
....<continue with rest part of your code>...

有2个地方我已经改变了......希望这可以完成这项工作。