簡體   English   中英

在Fragment中使用ListView時,適配器不調用getView()

[英]The adapter does not call getView () when using ListView in Fragment

因此,在編寫您的應用程序時,我面臨另一個“驚喜”問題。 我知道那里有ListFragment,但是使用了Fragment。 當我為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;
}   
}

該問題與MyListAdapter有關。 您有兩種選擇:

  1. 覆蓋getCount()並讓它返回數據集中的項目數
  2. 傳遞給您的數據集super()

    例如,在您的適配器中:

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

    或在MyListAdapter構造函數中,您可以調用:

      super(context, layoutResourceId, arrayList); 

    代替

     super(context, layoutResourceId); 

嗨,如下所示修改您的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個地方。希望這能完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM