簡體   English   中英

將自定義列表視圖添加到片段

[英]Add custom listview to a fragment

我正在嘗試在片段中使用自定義列表視圖,我嘗試了很多東西,我觀看了視頻並在此站點中尋找解決方案,但我感到困惑,因為它不起作用。 我的問題是,在代碼中,我無法使用自定義適配器,它會在片段膨脹時崩潰,並且添加了“(AppCompatActivity)”,它不會崩潰,但列表視圖沒有顯示任何內容。

有什么我可以做的嗎? 我應該嘗試另一件事嗎?

這是我要使用 ListView 的 Fragment

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 項的可視化界面的 XML

<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="" />

這是我創建的用於表示每個 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;
  }
}

如果還有什么我要展示的,請告訴我。 先謝謝了。

編輯:我修正了一些拼寫錯誤。 Fragment 中的這一行無法編譯:

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 原因:實際和形式參數列表的長度不同

根據您的問題,您正面臨這個問題

添加 AppcompatActivity 后,您的應用沒有崩潰,但列表沒有顯示任何內容

您的適配器正在從您的 Activity 請求 Context ,它是 AppCompat Type 。 因此,如果當您的主機活動未擴展 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(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; } 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM