我不熟悉如何将项目添加到片段的listview,但我试过了。它没有显示任何错误。但它也没有在应用程序上显示任何内容。 好吧,我从youtube学到了这样做的步骤。它也在主要活动类中完美地工作,但在片段类中它不起作用。 所以帮助。
项目列表视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1px"
android:gravity="left|top"
android:padding="2dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:textStyle="bold"/>
<TextView
android:gravity="left|top"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text=":" />
<TextView
android:textStyle="bold"
android:gravity="left|top"
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"/>
</LinearLayout>
</LinearLayout>
ussdz.java
:
package com.buckydroid.app.droidcpu;
/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdz
{
private int id;
private String USSD;
private String Description;
public ussdz(String description, String s) {
Description = description;
this.USSD = USSD;
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUSSD() {
return USSD;
}
public void setUSSD(String USSD) {
this.USSD = USSD;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
ussdadaper.java
:
package com.buckydroid.app.droidcpu;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdadaper extends BaseAdapter {
private Context ussds;
private List<ussdz> ussdlist;
public ussdadaper(Context ussds, List<ussdz> ussdlist) {
this.ussds = ussds;
this.ussdlist = ussdlist;
}
@Override
public int getCount() {
return ussdlist.size();
}
@Override
public Object getItem(int position) {
return ussdlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= View.inflate(ussds,R.layout.item_listview,null);
TextView ussdcode = (TextView)v.findViewById(R.id.textView1);
TextView ussddesc = (TextView)v.findViewById(R.id.textView2);
ussdcode.setText(ussdlist.get(position).getUSSD());
ussddesc.setText(ussdlist.get(position).getDescription());
return v;
}
}
ussd.java
(片段):
package layout;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.buckydroid.app.droidcpu.R;
import com.buckydroid.app.droidcpu.ussdadaper;
import com.buckydroid.app.droidcpu.ussdz;
import com.google.android.gms.tagmanager.Container;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ussd.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ussd#newInstance} factory method to
* create an instance of this fragment.
*/
public class ussd extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ListView listView;
private ussdadaper ussd;
private List<ussdz> ussdlist;
private OnFragmentInteractionListener mListener;
public ussd() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ussd.
*/
// TODO: Rename and change types and number of parameters
public static ussd newInstance(String param1, String param2) {
ussd fragment = new ussd();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
listView = (ListView)container.findViewById(R.id.listView);
ussdlist = new ArrayList<>();
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussd = new ussdadaper(getActivity().getApplicationContext(),ussdlist);
return inflater.inflate(R.layout.fragment_ussd, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteractionHome(Uri uri);
public void openHome(View view);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
答案 0 :(得分:1)
替换此行: List =(ListView) Container.findViewById(R.id.TaskListView);
将以上代码替换为以下代码: list =(ListView)getView()。findViewById(R.id.TaskListView);
答案 1 :(得分:0)
尝试将OnCreateView方法更改为;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View view = inflater.inflate( R.layout.fragment_ussd, container, false );
listView = (ListView)view.findViewById(R.id.listView);
ussdlist = new ArrayList<>();
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussd = new ussdadaper(getActivity(),ussdlist);
listView.setAdapter(ussd); //Set Adapter
return view;
}
答案 2 :(得分:0)
您没有将适配器设置为列表视图 像你这样改变onCreateView
View rootView = inflater.inflate(R.layout.fragment_offer_detail_item, container, false);
listView = (ListView)view.findViewById(R.id.listView);
ussdlist = new ArrayList<>();
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussd = new ussdadaper(getActivity(),ussdlist);
listView .setAdapter(ussd); //you should setthe adapter to the listview
return rootView;
此外,您的适配器类不遵循容易出错的视图模式,您最好将其更改为viewHolder模式
//编辑: 您可以通过粘贴Layout Finder中的list_item xml轻松创建Adapter类。您可以尝试简单并节省大量时间
答案 3 :(得分:0)
为listview设置适配器为 listview.setAdapter(mAapter);