0

私のアプリでいくつかの部分を変更しようとしています。私は各タブが私のリストビュー内のデータを変更するように私のアプリを設定する方法を把握することはできません。どんな助けもありがたい。 私のタブセットアップクラス:TabHost onTabChangeListener - リストビューアダプタを設定する

public class WorkoutDaysActivity extends BaseActivity { 

    ListView mListView = new ListView(this); 
    ArrayList <CustomObject> w29w1m; 
    CustomListViewAdapter mCustomListViewAdapter; 


    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.workout_days); 
     mToolBar = activateToolbar(); 
     setUpNavigationDrawer(); 
     w29w1m.add(new CustomObject("Squat", "65%", "6", "150", false)); 

     final ArrayList <CustomObject> w29w1w = new ArrayList <CustomObject>(); 
     w29w1w.add(new CustomObject("Dead", "65%", "6", "150", false)); 

     final ArrayList <CustomObject> w29w1f = new ArrayList <CustomObject>(); 
     w29w1f.add(new CustomObject("Bench", "65%", "6", "150", false)); 

     TabHost tabHost = (TabHost) findViewById(R.id.TabHost1); 
     tabHost.setup(); 
     tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Monday").setContent(objects)); 
     tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Wednesday").setContent(objects)); 
     tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Friday").setContent(objects)); 


    } 


    private TabHost.OnTabChangeListener mOnTabChangeListener = new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String tabId) { 
      if (tabId.equalsIgnoreCase("1")) 
       mListView.setAdapter(mCustomListViewAdapter, w29w1m); 
     } 
    }; 

    private final TabHost.TabContentFactory objects = new TabHost.TabContentFactory() { 
     @Override 
     public View createTabContent(String tag) { 
      return mListView; 
     } 
    }; 
} 

customObjectクラス:

public class CustomObject implements Serializable { 

    private String exercise; 
    private String percent; 
    private String reps; 
    private String weight; 
    private boolean check1; 

    public CustomObject(String exercise, String percent, String reps, String weight, boolean check1) { 
     this.exercise = exercise; 
     this.percent = percent; 
     this.reps = reps; 
     this.weight = weight; 
     this.check1 = check1; 
    } 

    public String getExercise() { 
     return exercise; 
    } 

    public String getPercent() { 
     return percent; 
    } 

    public String getReps() { 
     return reps; 
    } 

    public String getWeight() { 
     return weight; 
    } 

アダプタクラス

public class CustomListViewAdapter extends BaseAdapter { 

    private LayoutInflater inflater; 
    public ArrayList <CustomObject> objects; 
    boolean[] checkBoxState; 
    private CustomObject[] dataToBePopulated; 

    private class ViewHolder { 
     TextView txtExercise; 
     TextView txtPercent; 
     TextView txtReps; 
     TextView txtWeight; 
     CheckBox check1; 
    } 

    public void addAdapterItem(CustomObject item) { 
     objects.add(item); 
    } 

    public CustomListViewAdapter(Context context, ArrayList <CustomObject> objects){ 
     inflater = LayoutInflater.from(context); 
     this.objects = objects; 
     checkBoxState = new boolean[objects.size()]; 
    } 

    public int getCount() { 
     return dataToBePopulated != null ? dataToBePopulated.length : 0; 
    } 

    public CustomObject getItem(int position) { 
     return objects.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.workout_item, null); 
      holder.txtExercise = (TextView) convertView.findViewById(R.id.txtExercise); 
      holder.txtPercent = (TextView) convertView.findViewById(R.id.txtPercentage); 
      holder.txtReps = (TextView) convertView.findViewById(R.id.txtReps); 
      holder.txtWeight = (TextView) convertView.findViewById(R.id.txtWeight); 
      holder.check1 = (CheckBox) convertView.findViewById(R.id.check1); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.txtExercise.setText(objects.get(position).getExercise()); 
     holder.txtPercent.setText(objects.get(position).getPercent()); 
     holder.txtReps.setText(objects.get(position).getReps()); 
     holder.txtWeight.setText(objects.get(position).getWeight()); 
     holder.check1.setChecked(checkBoxState[position]); 

     holder.check1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (((CheckBox) v).isChecked()) { 
        checkBoxState[position] = true; 
       } else { 
        checkBoxState[position] = false; 
       } 
      } 
     }); 

     return convertView; 
    } 
} 

問題は、私がカスタムを表示するために私のアダプタを設定する方法がわからないです配列はImを作る(例えばw29w1m)。どのような助けが大いに感謝!ありがとう!

答えて

0

アダプタ内のデータを変更する場合は、アダプタで設定したリストを変更してみてください。次に、アダプタのnotifyDataSetChanged()に電話してください。たとえば、LVAdapter lvAdapter = new LVAdapter(context, list);(またはアダプタで使用するパラメータ)のようなアダプタをインスタンス化した場合は、ListViewに含める内容にリストを変更してから、lvAdapter.notifyDataSetChanged();を呼び出します。

しかし、アダプタを変更する場合は、タブが変更されるたびに別のアダプタをリストビューに設定してみてください。

また、tabSpecのIDをそれぞれ固有のものにすることをおすすめします。

関連する問題