0

嗨,我正在尝试将 aButton插入EmptyView. 我似乎唯一可以开始工作的是EmptyText当列表为空时显示文本值的位置。此代码仅用于显示textview. 如何成功地将按钮添加到视图中?

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.format.DateFormat;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.robertrichardson.crimeproofassistant.R.string;

public class crimeListFragment extends ListFragment {

    private ArrayList<crime> mcrime;
    private boolean mSubtitleVisible;
    public static final String TAG = "crimeListFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.app_name);
        mcrime = crimeLab.get(getActivity()).getcrime();
        crimeAdapter adapter = new crimeAdapter(mcrime);
        setListAdapter(adapter);
        setRetainInstance(true);
        mSubtitleVisible = false;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view,  savedInstanceState);

        setEmptyText(getResources().getString(string.new_crime_attack_empty_desc));

        registerForContextMenu(getListView());
        setHasOptionsMenu(true);
}


    @TargetApi(11)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
          View v = super.onCreateView(inflater,  parent,  savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (mSubtitleVisible) {
                getActivity().getActionBar().setSubtitle(R.string.subtitle);
            }
        }

        ListView listView = (ListView)v.findViewById(android.R.id.list);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            // Use floating point context menus on Froyer and Gingerbread
            registerForContextMenu(listView);
        } else {
            // Use contextual action bar on Honeycomb and higher
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            listView.setMultiChoiceModeListener(new MultiChoiceModeListener()  {

            // ActionMode.Callback methods
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.crime_list_item_context, menu);
                return true;
            }

            public void onItemCheckedStateChanged(ActionMode mode, int position,
                    long id, boolean checked) {
            // required but not used in this implementation
             }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menu_item_delete_crime:
                    crimeAdapter adapter = (crimeAdapter)getListAdapter();
                    crimeLab crimeLab = crimeLab.get(getActivity());
                    for (int i = adapter.getCount() - 1; i >= 0; i--) {
                        if (getListView().isItemChecked(i)) {
                            crimeLab.deletecrime(adapter.getItem(i));
                        }
                    }
                    mode.finish();
                    adapter.notifyDataSetChanged();
                    return true;
                default:
                    return false;
                }

            }

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            // Required, but not used in this implementation
            }

            public void onDestroyActionMode(ActionMode mode) {

            }
        }); 
    }
        return v;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        crime c = ((crimeAdapter)getListAdapter()).getItem(position);
        // Start crime Activity
        Intent i = new Intent(getActivity(), crimePagerActivity.class);
        i.putExtra(crimeFragment.EXTRA_crime_ID, c.getId());
        startActivity(i);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        ((crimeAdapter)getListAdapter()).notifyDataSetChanged();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_list, menu);
        MenuItem showSubtitle = menu.findItem(R.id.menu_item_show_subtitle);
        if (mSubtitleVisible && showSubtitle != null) {
            showSubtitle.setTitle(R.string.hide_subtitle);
        }
     }

    @TargetApi(11)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_new_crime:
            crime crime = new crime();
            crimeLab.get(getActivity()).addcrime(crime);
            Intent i = new Intent(getActivity(), crimePagerActivity.class);
            i.putExtra(crimeFragment.EXTRA_crime_ID,  crime.getId());
            startActivityForResult(i,0);
            return true;
        case R.id.menu_item_show_subtitle:
            if (getActivity().getActionBar().getSubtitle() != null) {
                getActivity().getActionBar().setSubtitle(R.string.subtitle);
                mSubtitleVisible = true;
                item.setTitle(R.string.hide_subtitle);
            } else {
                getActivity().getActionBar().setSubtitle(null);
                mSubtitleVisible = false;
                item.setTitle(R.string.show_subtitle);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
     }

    @Override
    public void onCreateContextMenu(ContextMenu menu,View v, ContextMenuInfo menuInfo) {
        getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        int position = info.position;
        crimeAdapter adapter = (crimeAdapter)getListAdapter();
        crime crime = adapter.getItem(position);

        switch (item.getItemId()) {
           case R.id.menu_item_delete_crime:
               crimeLab.get(getActivity()).deletecrime(crime);
               adapter.notifyDataSetChanged();
               return true;
        }
        return super.onContextItemSelected(item);
    }

    private class crimeAdapter extends ArrayAdapter<crime> {
        public crimeAdapter(ArrayList<crime> crime) {
            super(getActivity(), 0, crime);
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // If we weren't given a view, inflate one
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater()
                    .inflate(R.layout.list_item_crime, null);
        }  

        // Enter the view of this crime Attack
        crime c = getItem(position);

        TextView titleTextView =
                (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
        titleTextView.setText(c.getTitle());
        TextView dateTextView =
                (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);
        dateTextView.setText(DateFormat.format("EEEE, MMM dd, yyyy  hh : mm a", c.getDate()).toString());

        CheckBox resolvedCheckBox =
                (CheckBox)convertView.findViewById(R.id.crime_list_item_resolvedCheckBox);
        resolvedCheckBox.setChecked(c.isResolved());

        return convertView;
    }
}

    @Override
    public void onResume() {
        super.onResume();
        ((crimeAdapter)getListAdapter()).notifyDataSetChanged();
    }
 }    


Here's my XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="#f2f3f4"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
 <ListView
  android:id="@android:id/list"
  android:background="#f2f3f4"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  >
 </ListView>
 <linearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:background="#f2f3f4"
  android:orientation="vertical"
  >
 <TextView
  android:id="@android:id/empty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:paddingLeft="4dp"
  android:paddingRight="4dp"
  android:text="@string/new__empty_desc" 
  />
 <Button
  android:id="@+id/addButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:paddingLeft="4dp"
  android:paddingRight="4dp"
  android:text="@string/new_attack_empty" 
  />
 </linearLayout>
</FrameLayout>
4

2 回答 2

1
public class MainActivity extends Activity {

    private CustomBaseAdapter customBaseAdapter;
    private ListView lv;
    private Button btn;



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

        FrameLayout fl = new FrameLayout(this);
        btn = new Button(this);
        btn.setText("Hit me");

        lv = new ListView(this);

        customBaseAdapter = new CustomBaseAdapter(this);
        lv.setAdapter(customBaseAdapter);

        fl.addView(lv);
        fl.addView(btn);
        setContentView(fl);

        handleVisbility();
    }

    public void handleVisbility() {
        if (customBaseAdapter.getCount() == 0) {
            lv.setVisibility(View.GONE);
            btn.setVisibility(View.VISIBLE);
        } else {
            lv.setVisibility(View.VISIBLE);
            btn.setVisibility(View.GONE);
        }
    }

    public class CustomBaseAdapter extends BaseAdapter {

        private Context context;
        View superView;

        public CustomBaseAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int arg0) {
            return new Object();
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            FrameLayout fl = new FrameLayout(context);

            Button btn = new Button(context);
            btn.setText("Hello, im a Button");

            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Yay hit me again", Toast.LENGTH_LONG).show();
                }
            });

            fl.addView(btn);

            return fl;
        }

    }

}

好的,这有点像你想在这里做的。如果列表中有项目,它会显示一个 ListView,如果列表为空,它将显示按钮。您唯一需要做的就是在每次更改 ListViews 内容时调用 handleVisbility()。特别是在您将某些内容添加到列表之后。

于 2013-10-13T22:45:48.527 回答
0

的默认实现ListFragment涉及将 textView 设置为列表的空视图。

http://developer.android.com/reference/android/app/ListFragment.html#setEmptyText(java.lang.CharSequence)

您可以通过扩展 a 来更改它Fragment并在其中创建自己的ListViewListFragment如果你喜欢它提供的一些东西,你可以使用它的代码。

作为替代方案,您可以尝试执行getListView().setEmptyView()并设置您需要的视图。再次查看它们如何在 ListFragment 中的代码中执行此操作并使用不同的视图重现它(而不是 TextView 使用 Button 或 ImageButton 任何你需要的)

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/app/ListFragment.java

于 2013-10-13T18:17:07.870 回答