gpt4 book ai didi

java - 自定义 ListView 和 Sherlock 列表 fragment

转载 作者:行者123 更新时间:2023-11-30 09:28:56 28 4
gpt4 key购买 nike

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

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

自定义适配器和 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;
}
}

任何人都可以告诉我如何正确实现我的列表以及我的错误在哪里?非常感谢

最佳答案

不要从 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));
}

不要覆盖 onCreateView在你的 fragment 中,SherlockListFragment为您创建 ListView 布局。

如果你想设置适配器,删除你的onCreateView并在 onActivityCreated() 中设置适配器

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

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

否则确保你的布局有ListViewandroid:id="@android:id/list"否则 SherlockListView 将没有 View 来设置适配器。

关于java - 自定义 ListView 和 Sherlock 列表 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13802684/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com