Java 如何在Android中在片段中实现ListView
我有Java 如何在Android中在片段中实现ListView,java,android,listview,android-fragments,android-activity,Java,Android,Listview,Android Fragments,Android Activity,我有Fragment.java及其XML。在XML中,我放置了列表视图,我想更新片段中的列表视图,并添加一些字符串。 我正在尝试制作一个自定义适配器。当我使用自定义适配器运行活动中的ListView时,一切都很好,但是当我将相同的代码放入片段中时,什么也没有发生。我正在使用EventBus3 这是我的活动发送事件 EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt])); 这是我的片段: public class
Fragment.java
及其XML。在XML中,我放置了列表视图
,我想更新片段
中的列表视图
,并添加一些字符串
。
我正在尝试制作一个自定义适配器。当我使用自定义适配器运行活动中的ListView
时,一切都很好,但是当我将相同的代码放入片段中时,什么也没有发生。我正在使用EventBus3
这是我的活动发送事件
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
这是我的片段:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
@Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
这是cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
将extends Fragment更改为extends ListFragment是否收到错误?或者listView只是没有填充任何内容?没有发生任何错误扫描请发布完整代码?我有几个问题,但看一下会更容易。看,我会给其他答案添加代码。你应该编辑你的问题。添加代码。我以前试过,但片段在OnCreateView中给我错误。如果删除此类,应用程序将崩溃。这是因为我对选项卡使用了材质设计,当MainActivity创建选项卡和他的片段时,他是这样做的:MainActivity.ViewPagerAdapter adapter=new MainActivity.ViewPagerAdapter(getSupportFragmentManager());addFragment(newmainfragment(),getResources().getString(R.string.Main));
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}