0

因此,在编写您的应用程序时,我遇到了另一个“意外”问题。我知道有ListFragment,但是使用了Fragment。当我为一个 ListView 的片段添加标记时,当你调用适配器时,列表中的功能不会添加元素——这不是我在日志上跟踪的一种狡猾的方式,即你不调用 getView()。更有趣的是 - 如果我不使用片段列表而不是仅使用片段列表,并且您添加的适配器在所有内容都运行良好时 - 列表已填满。我不明白为什么会发生这种情况——为什么在作品的片段列表中不够用。

活动开始.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>    

开始活动.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";

}

片段列表.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;
}   
}
4

2 回答 2

1

该问题与MyListAdapter. 你有两个选择:

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

    例如,在您的适配器中:

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

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

     super(context, layoutResourceId, arrayList);
    

    代替

    super(context, layoutResourceId);
    
于 2012-12-12T13:56:55.913 回答
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 个地方。希望这会起作用。

于 2013-04-29T10:24:17.210 回答