前提・実現したいこと
Javaでandroidアプリ作成の練習中です。
独自adapterを作成してListviewを表示していたコードを、
dateBindingを使用したmvvmパターンで書き直しています。
行き詰まっているところは、
MyAdapter内のgetView以下だけが呼ばれないことです。
発生している問題・エラーメッセージ
MyAdapter.java内のgetViewが呼ばれていないようなので、以下の"GetView_check"がLogに出力されません。
@Override public View getView(int position, View view, ViewGroup viewGroup) { System.out.println("GetView_check");
該当のソースコード
MyAdapter
1package com.websarva.wing.android.trylistviewbydatebinding; 2 3import android.content.Context; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import android.widget.BaseAdapter; 8import android.widget.TextView; 9 10import androidx.databinding.DataBindingUtil; 11 12import com.websarva.wing.android.trylistviewbydatebinding.databinding.FoodrowBinding; 13 14import java.util.ArrayList; 15import java.util.List; 16 17public class MyAdapter extends BaseAdapter { 18 Context context; 19 int resource; 20 LayoutInflater layoutInflater = null; 21 ArrayList<Food> foodList; 22 23 public MyAdapter(Context context) { 24 this.context = context; 25 this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 26 } 27 28 public void setFoodList(ArrayList<Food> foodList) { 29 this.foodList = foodList; 30 } 31 32 @Override 33 public int getCount() { 34 return foodList.size(); 35 } 36 37 @Override 38 public Object getItem(int position) { 39 return foodList.get(position); 40 } 41 42 @Override 43 public long getItemId(int position) { 44 return foodList.get(position).getId(); 45 } 46 47 @Override 48 public View getView(int position, View view, ViewGroup viewGroup) { 49 System.out.println("GetView_check"); 50 51 Food food = (Food) getItem(position); 52 FoodrowBinding foodrowBinding; 53 54 if (view == null) { 55 LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); 56 57 foodrowBinding = FoodrowBinding.inflate(inflater, viewGroup, false); 58 } else { 59 foodrowBinding = DataBindingUtil.getBinding(view); 60 } 61 final FoodrowViewModel foodrowViewModel = new FoodrowViewModel( 62 ); 63 64 foodrowBinding.setViewModel(foodrowViewModel) 65 66 return foodrowBinding.getRoot(); 67 } 68 69} 70 71
MainFragment
1package com.websarva.wing.android.trylistviewbydatebinding; 2 3 4import android.os.Bundle; 5import android.view.LayoutInflater; 6import android.view.View; 7import android.view.ViewGroup; 8import android.widget.ListView; 9 10import androidx.databinding.DataBindingUtil; 11import androidx.fragment.app.Fragment; 12 13import com.websarva.wing.android.trylistviewbydatebinding.databinding.ActivityMainBinding; 14 15import java.util.ArrayList; 16 17public class MainFragment extends Fragment { 18 MainViewModel fMainViewModel; 19 ActivityMainBinding activityMainBinding; 20 ListView listView; 21 MyAdapter myAdapter; 22 ArrayList<Food> list = new ArrayList<>(); 23 24 public static MainFragment newInstance() { 25 Bundle arguments = new Bundle(); 26 MainFragment fragment = new MainFragment(); 27 fragment.setArguments(arguments); 28 return fragment; 29 } 30 31 public void setViewModel(MainViewModel mainViewModel) { 32 fMainViewModel = mainViewModel; 33 } 34 35 36 @Nullable 37 @Override 38 public View onCreateView(LayoutInflater inflater, ViewGroup container, 39 Bundle savedInstanceState) { 40 activityMainBinding = DataBindingUtil.inflate(inflater, R.layout.activity_main, container, false); 41 activityMainBinding.setViewModel(fMainViewModel); 42 activityMainBinding.getViewModel(); 43 activityMainBinding.getRoot(); 44 45 return activityMainBinding.getRoot(); 46 } 47 48 @Override 49 public void onActivityCreated(Bundle savedInstanceState) { 50 super.onActivityCreated(savedInstanceState); 51 52 setupListAdapter(); 53 } 54 55 private void setupListAdapter(){ 56 57 this.listView = activityMainBinding.foodListView; 58 59 myAdapter = new MyAdapter(getContext()); 60 myAdapter.setFoodList(list); 61 listView.setAdapter(myAdapter); 62 63 setFood(); 64 setFood(); 65 setFood(); 66 67 }; 68 69 private void setFood() { 70 Food food = new Food(); 71 food.setName("りんご"); 72 food.setPrice(100); 73 list.add(food); 74 myAdapter.notifyDataSetChanged(); 75 } 76 77}
Food
1package com.websarva.wing.android.trylistviewbydatebinding; 2 3public class Food { 4 long id; 5 private String name; 6 private int price; 7 8 public long getId() { 9 return id; 10 } 11 12 public void setId(long id) { 13 this.id = id; 14 } 15 16 public String getName(){ 17 return name; 18 } 19 20 public void setName(String name){ 21 this.name=name; 22 } 23 24 public int getPrice(){ 25 return price; 26 } 27 28 public void setPrice(int price){ 29 this.price=price; 30 } 31 32} 33
MainActivity
1package com.websarva.wing.android.trylistviewbydatebinding; 2 3import androidx.annotation.NonNull; 4import androidx.appcompat.app.AppCompatActivity; 5import androidx.databinding.DataBindingUtil; 6import androidx.fragment.app.Fragment; 7import androidx.fragment.app.FragmentManager; 8import androidx.fragment.app.FragmentTransaction; 9 10import android.os.Bundle; 11 12import com.websarva.wing.android.trylistviewbydatebinding.databinding.ActivityMainBinding; 13 14public class MainActivity extends AppCompatActivity { 15 16 MainViewModel mainViewModel; 17 MainFragment mainFragment; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 System.out.println("appスタート"); 25 26 mainViewModel = new MainViewModel(); 27 mainFragment = new MainFragment(); 28 29 ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); 30 activityMainBinding.setViewModel(mainViewModel); 31 32 mainFragment.setViewModel(mainViewModel); 33 mainFragment = findOrCreateViewFragment(); 34 35 } 36 @NonNull 37 private MainFragment findOrCreateViewFragment() { 38 39 mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.container); 40 if (mainFragment == null) { 41 mainFragment = MainFragment.newInstance(); 42 addFragmentToActivity(getSupportFragmentManager(), 43 mainFragment, String.valueOf(R.id.container)); 44 } 45 return mainFragment; 46 } 47 public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager, 48 @NonNull Fragment fragment, String tag) { 49// checkNotNull(fragmentManager); 50// checkNotNull(fragment); 51 52 FragmentTransaction transaction = fragmentManager.beginTransaction(); 53 transaction.add(fragment, tag); 54 transaction.commit(); 55 56 } 57 58 59} 60
試したこと
MainFragmentからAdapterを呼び出していることは、確認済みです。
Adapter内のgetView以下のみが呼べていないので、Fragment内の記述が間違っているのかなと思って色々調べていますが、初心者なので、行き詰まっています。。。
なお、getView内はすべて呼び出せていないことも確認済みです。
よろしくお願いします。
あなたの回答
tips
プレビュー