我刚接触到android,并且我尝试在主要活动中添加一个简单的片段(包含listview)。
是假设在MainActivity中的片段吗?
我已经检查了,大多数问题和解决方案看起来像是一个版本问题(使用android.support.app.v4.Fragment),但它在这里看起来并不像。 我很确定它很简单,我很遗憾。
谢谢!
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
PlaceholderFragment phf = new PlaceholderFragment();
fragmentTransaction.add(R.id.main_fragment_container, phf);
fragmentTransaction.commit();
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, vg);
ArrayList<String> tempDataArr = new ArrayList<>();
tempDataArr.add("TEMP 1");
tempDataArr.add("TEMP 2");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_fnf,R.id.list_item_fnf_textview,tempDataArr);
ListView listView = (ListView)rootView.findViewById(R.id.listview_fnf);
listView.setAdapter(arrayAdapter);
return rootView;
}
}
}