コード以下しようとすると、それが助けなければならない:
public class MainActivity extends AppCompatActivity {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
int no_of_days = 0;
List<Date> dates = null;
RecyclerView recycler_view;
Calendar calendar = null;
RecyAdapter recyAdapter = null;
LinearLayoutManager linearLayoutManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
dates = new ArrayList<>();
linearLayoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
recycler_view.setLayoutManager(linearLayoutManager);
no_of_days = getNoOfDays("20-06-2016", "20-08-2016");
calendar = Calendar.getInstance();
try {
calendar.setTime(new SimpleDateFormat("dd-MM-yyyy").parse("20-06-2016"));
} catch (ParseException e) {
e.printStackTrace();
}
for (int i = 0; i < no_of_days; i++) {
calendar.add(Calendar.DATE, 10);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
} else {
dates.add(calendar.getTime());
}
}
recyAdapter = new RecyAdapter(MainActivity.this, dates);
recycler_view.setAdapter(recyAdapter);
}
public int getNoOfDays(String Created_date_String, String Expire_date_String) {
Date Created_convertedDate = null, Expire_CovertedDate = null, todayWithZeroTime = null;
try {
Created_convertedDate = DATE_FORMAT.parse(Created_date_String);
Expire_CovertedDate = DATE_FORMAT.parse(Expire_date_String);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar createdDay = Calendar.getInstance();
createdDay.setTime(Created_convertedDate);
Calendar expireDay = Calendar.getInstance();
expireDay.setTime(Expire_CovertedDate);
long diff = createdDay.getTimeInMillis() - expireDay.getTimeInMillis();
long days = (diff/(24 * 60 * 60 * 1000));
return (Math.abs((int) days))/10;
}
}
そのアダプタクラスは以下の通りです:
public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.ViewHolder> {
Context context;
List<Date> dates = null;
public RecyAdapter(Context context, List<Date> dates) {
this.context = context;
this.dates = dates;
}
@Override
public RecyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyAdapter.ViewHolder holder, int position) {
holder.txt.setText(MainActivity.DATE_FORMAT.format(dates.get(position)));
}
@Override
public int getItemCount() {
return dates.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt = (TextView) itemView.findViewById(R.id.txt);
}
}
}
とそのレイアウトは以下のとおりです。activity_main 。このタスクを達成するために、XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fitness.vario.stacklistofdates.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
row_item.xmlは
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/txt"/>
</LinearLayout>
出典
2016-06-29 11:01:24
Drv
質問(**「なぜ、このコードが動作しない?」**は)* *希望の動作*、*特定の問題やエラーを含める必要がありますし、 *それを再現するために必要な最短コード** **質問自体に**。 **明確な問題文**のない質問は他の読者には役に立たない。参照:[最小限で完全で検証可能なサンプルの作成方法](http://stackoverflow.com/help/mcve) –
@chetan私のコードを確認してください、それは本当に動作します:) – Drv