2017-10-18 126 views
1

我想在一個片段中使用自定義列表視圖,並且嘗試了很多東西,我觀看了視頻,並且在此網站中查找了解決方案,但是我感到困惑,因爲它沒有「工作。 我的問題是,在代碼中,我無法使工作自定義適配器,它崩潰時,片段膨脹,並添加「(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的 原因:實際的和正式的參數列表長度

+1

請加錯誤日誌。 –

+0

你確定你的類「Record」編譯了嗎?構造函數看起來很奇怪。 – Thomas

+0

你可以添加fragment_record.xml文件嗎?因爲它可能是佈局問題。 – kimkevin

回答

1

不同根據你的問題,你都面臨這個問題

加入AppcompatActivity後您的應用程序沒有崩潰,但列表不顯示任何

您的適配器是要求從您的活動上下文是程序兼容性類型。所以,如果當你的主機活動不延長AppcompatActivity就會死機

因此,當你將其更改爲AppcompatActivity它不會崩潰

現在讓我們來解決顯示一個空白的ListView 你沒有覆蓋的問題父方法getCount()。在getCount()方法返回列表中的大小項目。

另一件事CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);我不認爲你的代碼與這條線作爲您的自定義AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; }適配器正在一個參數編譯,但你傳遞了兩個

你的構造應該是這樣的

AdaptadorHistorial(Context context , List<Record> scoreList){ 
    super(context, R.layout.record_fragment, scoreList); 
    this.context = context; 
    this.items = scoreList; 

} 



@override 
public int getCount(){ 
return items.size(); 
} 
+0

對不起,我不是故意寫兩個參數,它是一樣的,我已經改正了它 –

相關問題