如何在Android中的片段中实现ListView

时间:2016-06-23 14:39:43

标签: java android listview android-fragments android-activity

我有Fragment.java及其XML。在XML格式中我添加了ListView,我希望更新ListView中的Fragment并添加一些String。 我正在尝试制作自定义适配器。当我使用自定义适配器在ListView中运行Activity时,一切都很好,但是当我在Fragment中放入相同的代码时,没有任何反应。我正在使用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;
}
}

这是我的适配器

public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;

public ListAdapter(List<cources> listValue, Context context)
{
    this.context = context;
    this.valueList = listValue;
}

@Override
public int getCount() 
{
    return this.valueList.size();
}

@Override
public Object getItem(int position) 
{
    return this.valueList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewItem viewItem = null;
    if(convertView == null)
    {
        viewItem = new ViewItem();
        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater layoutInfiater = LayoutInflater.from(context);
        convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);

        viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
        viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
        viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
        convertView.setTag(viewItem);
    }
    else
    {
        viewItem = (ViewItem) convertView.getTag();
    }

    viewItem.txtNamePlanche.setText(valueList.get(position).name);
    viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
    viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
    return convertView;
}
}

这是我的ItemView

import android.widget.TextView;

class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}

这是cources:

public class cources
{
public String name;
public String price_plate;
public String price_baget;
}

1 个答案:

答案 0 :(得分:0)

更改extends Fragment以扩展ListFragment