自定义列表视图和Sherlock列表片段

时间:2012-12-10 14:15:40

标签: java android actionbarsherlock

在我的应用程序中,我试图通过下拉导航菜单在页面之间进行导航,但是当我运行应用程序时,我的列表中没有项目

主要活动

public class Main extends SherlockFragmentActivity implements OnNavigationListener{
//----------------------------------New Variables
private static final String KEY_MODELS="models";
private static final String KEY_POSITION="position";
private static final String[] labels= { "All", "Downloads","Completed","Later" };
private CharSequence[] models=new CharSequence[4];
private DownloadList frag=null;
private int lastPosition=-1;  
//----------------------------------
ActionBar act;
ViewPager myviewpager;
int mSelectedPageIndex=1;
DownloadService downloadservice;
Intent serviceIntent ;


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

    frag=(DownloadList)getSupportFragmentManager().findFragmentById(android.R.id.content);
    if (frag==null) {
        frag=new DownloadList();
        getSupportFragmentManager().beginTransaction()
 .add(android.R.id.content, frag).commit();
      }

    if (state != null) {
        models=state.getCharSequenceArray(KEY_MODELS);
      }

    if (downloadservice==null)
        downloadservice= new DownloadService();
    ArrayAdapter<String> nav=null;
    act=getSupportActionBar();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    {
      nav= new ArrayAdapter<String>(act.getThemedContext(),
     android.R.layout.simple_spinner_item,labels);
    }
    else 
    {
      nav=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,labels);
    }

    nav.setDropDownViewResource(android.R.layout.simple_list_item_1);
    act.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    act.setDisplayShowTitleEnabled(false);
    act.setHomeButtonEnabled(false);

    act.setListNavigationCallbacks(nav, this);
    if (state != null) {
        act.setSelectedNavigationItem(state.getInt(KEY_POSITION));
      }

    serviceIntent= new Intent(Main.this,downloadservice.getClass());
    if (startService(serviceIntent)==null)
        startService(serviceIntent);

    act.setTitle("");

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater infalter = getSupportMenuInflater();
    infalter.inflate(R.menu.mymenu, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.sub1)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Downlaod New File");
        alert.setMessage("Enter URL");
        final EditText input = new EditText(this);
        alert.setView(input);
        input.setText(  "http://205.196.123.184/ddpha7c5b8lg/616c36j0d1xbztf
 /Lecture+ppt+Ch2.ppt");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString();
                downloadservice.addandstart(value);
                Log.d("value",value);
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });

        alert.show();
    }   

    return super.onOptionsItemSelected(item);
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    lastPosition=itemPosition;

                                                                                             frag=(DownloadList)getSupportFragmentManager().findFragmentById(android.R.id.content);

    return false;
}

自定义适配器和SherlockFragmentList

public  class DownloadList extends SherlockListFragment {
int index=0;
Button downloadButton;
Button pauseButton;
Button resumeButton;
TextView textLink;
TextView progressbar;
DownloadService downloadservice= new DownloadService();
MyAdapter listadapter;
Intent serviceIntent ;
String state;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.downloadlist);
    setListAdapter(new MyAdapter(getSherlockActivity(), downloadservice.downloads));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
 savedInstanceState) {
    View rootView = inflater.inflate(R.layout.downloadlist, container, false);

    setListAdapter(new MyAdapter(getSherlockActivity(), downloadservice.downloads));
    return rootView;
}
int getIndex()
{
    return index;
}
public class MyAdapter extends ArrayAdapter<NewDownload>
{
     private final List<NewDownload> list;
      //private final Activity context;
      Thread t;

    public MyAdapter(Context context,List<NewDownload> list) {
        super(context, R.layout.list_item,  list);
    //    this.context = (Activity) context;
        this.list = list;
    }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) 
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_item, null);

            pauseButton = (Button) row.findViewById(R.id.btn1);
            resumeButton = (Button) row.findViewById(R.id.btn2);
            textLink = (TextView) row.findViewById(R.id.tv1);
            progressbar = (TextView) row.findViewById(R.id.progressBar1);


textLink.setText(downloadservice.downloads.get(position).getName());

progressbar.setBottom(downloadservice.downloads.get(position).getDownloadedSize());

progressbar.setTop(downloadservice.downloads.get(position).getTotalSize());

            final int p = position;
            final NewDownload r = list.get(p);
            if (r.state=="downloading")
                {
                    progressbar.setText("DownLoading...");
                    pauseButton.setEnabled(true);
                    resumeButton.setEnabled(false);
                }
            else if (r.state=="pause")
                {
                pauseButton.setEnabled(false);
                resumeButton.setEnabled(true);
                progressbar.setText(r.state);
                }
            pauseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                downloadservice.pause(p);
                setListAdapter(listadapter );

                }
            });

            resumeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

if     (getActivity().startService(serviceIntent)==null)

getActivity().startService(serviceIntent);
                        downloadservice.resume(p);
                        setListAdapter(listadapter );
                }
            });

            return row;
        }
}

任何人都可以告诉我如何正确实施我的清单以及我的错误在哪里? 很多

1 个答案:

答案 0 :(得分:2)

不要从你的片段设置布局大不了!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // getActivity().setContentView(R.layout.downloadlist); // Never do this!
    // Also this will do nothing remove this! This should be in your `onActivityCreated()`
    // setListAdapter(new MyAdapter(getSherlockActivity(), downloadservice.downloads));
}

不要覆盖片段中的onCreateViewSherlockListFragment会为您创建ListView布局。

如果您要设置适配器,请移除onCreateView并在onActivityCreated()

中设置适配器
@Override
public void onActivityCreated(Bundle saveState){
    setListAdapter(new MyAdapter(getSherlockActivity(), downloadservice.downloads));
}

在适配器setListAdapter(listadapter );中设置getView也是错误的。如果您更新了List<?>数据,请确保在更新列表后从mainThread中调用notifyDataSetChanged()

否则确保布局的ListView定义为android:id="@android:id/list",否则SherlockListView也无法设置适配器。