Deve
Deve

Reputation: 181

Custom listview and Sherlock List Fragment

In my application I'm trying to make navigation between pages by Dropdown navigation menu, but when i'm running the application there is no items in my list

Main Activity

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;
}

Custom adapter and 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;
        }
}

Anyone can tell me how to implement my list right and where is my mistake ? thx a lot

Upvotes: 2

Views: 4719

Answers (1)

Chris.Jenkins
Chris.Jenkins

Reputation: 13129

Don't set the layout from your fragment big no no!

@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));
}

Don't override onCreateView in your Fragment, SherlockListFragment creates the ListView layout for you.

If you want to set the adapter, remove your onCreateView and set you adapter in onActivityCreated()

@Override
public void onActivityCreated(Bundle saveState){
    setListAdapter(new MyAdapter(getSherlockActivity(), downloadservice.downloads));
}

Setting your setListAdapter(listadapter ); in your Adapter getView is wrong too. If you update your List<?> of data make sure you call notifyDataSetChanged() from the mainThread after you update the list.

Otherwise make sure you layout has a ListView with android:id="@android:id/list" defined otherwise SherlockListView won't have a view to set the adapter too.

Upvotes: 2

Related Questions