我想在一個片段中使用自定義列表視圖,並且嘗試了很多東西,我觀看了視頻,並且在此網站中查找了解決方案,但是我感到困惑,因爲它沒有「工作。 我的問題是,在代碼中,我無法使工作自定義適配器,它崩潰時,片段膨脹,並添加「(AppCompatActivity)」,它不會崩潰,但列表視圖不顯示任何內容。將自定義列表視圖添加到片段中
有什麼我可以做的呢? 我應該嘗試另一件事嗎?
這是我想使用ListView的片段
public class RecordFragment extends Fragment {
private ArrayList<Record> scoreList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_record, container, false);
scoreList= new ArrayList<>();
scoreList.add(new Record("Math", 10, "9/1/2017 13:45"));
scoreList.add(new Record("Math", 8, "7/5/2017 10:50"));
scoreList.add(new Record("Marh", 4, "7/7/2017 16:30"));
CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);
ListView list1 = (ListView)view.findViewById(R.id.list1);
list1.setAdapter(adapter);
return view;
}
private class CustomAdapter extends ArrayAdapter<Record>{
AppCompatActivity appCompatActivity;
CustomAdapter(AppCompatActivity context){
super(context, R.layout.record_fragment, scoreList);
appCompatActivity = context;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = appCompatActivity.getLayoutInflater();
View item = inflater.inflate(R.layout.record_fragment, null);
TextView tv1, tv2, tv3;
tv1 = (TextView)item.findViewById(R.id.tv1);
tv2 = (TextView)item.findViewById(R.id.tv2);
tv3 = (TextView)item.findViewById(R.id.tv3);
tv1.setText(scoreList.get(position).getTest());
tv2.setText(scoreList.get(position).getScore());
tv3.setText(scoreList.get(position).getDate());
return item;
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
這是每個ListView項
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
的可視化界面的XML這是我創建表示每個班級ListView項目
class Record{
private String test, date;
private int score;
public Record(String test, int score, String date)
{
this.test= test;
this.score= score;
this.date = date;
}
public String getTest() {
return test;
}
public int getScore(){
return score;
}
public String getDate(){
return date;
}
}
如果還有別的東西我必須告訴我,酶。 事先,謝謝。
編輯:我修正了一些拼寫錯誤。而這條線的片段不編譯:
CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);
顯示此消息時,我嘗試運行它(我的客人是錯誤日誌):
Error:(39, 40) error: constructor CustomAdapter in class RecordFragment.CustomAdapter cannot be applied to given types;
要求:AppCompatActivity 發現: FragmentActivity,INT,ArrayList的 原因:實際的和正式的參數列表長度
請加錯誤日誌。 –
你確定你的類「Record」編譯了嗎?構造函數看起來很奇怪。 – Thomas
你可以添加fragment_record.xml文件嗎?因爲它可能是佈局問題。 – kimkevin