列表视图未显示片段

时间:2016-10-18 04:04:41

标签: android listview

我创建了一个片段,我添加了一个listView。创建了一个适配器,但列表视图未显示在片段中。

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

// Inflate the layout for this

String[] songArray={"Song 1","Song 2","Song 3","Song 4","Song 5","Song 6","Song 7"};

    hindi= (ListView)getView().findViewById(R.id.hindiList);

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),R.layout.textdesign,R.id.textView2,songArray);

    hindi.setAdapter(adapter);


    return inflater.inflate(R.layout.fragment_hindi, container, false);
}

4 个答案:

答案 0 :(得分:0)

确保您添加列表视图ID ,如此

<ListView
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

设置适配器时,插入此行。例如:

 getListView().setAdapter(new ListAdapter(getActivity(), rowItems));

答案 1 :(得分:0)

使用以下代码段更新onCreateView实施:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this
    View view =  inflater.inflate(R.layout.fragment_hindi, container, false);
    String[] songArray={"Song 1","Song 2","Song 3","Song 4","Song 5","Song 6","Song 7","Song 8"};
    hindi= (ListView) view.findViewById(R.id.hindiList);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),R.layout.textdesign,R.id.textView2,songArray);
    hindi.setAdapter(adapter);
    return view;
}

如果有帮助请告诉我

答案 2 :(得分:0)

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    String[] songArray={"Song 1","Song 2","Song 3","Song 4","Song 5","Song 6","Song 7"};

    View rootView = inflater.inflate(R.layout.fragment_hindi, container, false);
    ListView hindiListView = (ListView) rootView1.findViewById(R.id.hindiList);
    ListAdapter hindiAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.textDesign, songArray);
    hindiListView.setAdapter(hindiAdapter);
    return rootView;
}

答案 3 :(得分:-1)

尝试以下代码

                 $html = "<a href=''><button>Like</button></a>
                            <a href=''><button>View</button></a>";
                if($user_id == $row_post['user_id']){
                   $html .= "<a href=''><button>Delete</button></a>
                         <a href=''><button>Edit</button></a>"
                }            
  echo $html;