正如标题所说,我想在点击RecyclerView
后在Activity中创建一个新列表。我使用JSON
来获取数据,里面的数据显然有一个名称,注释和日期时间列表。我可以将它们设置为TextView
,但这不是我想要的。我需要将它们显示为列表。该列表应具有名称,注释和日期时间。谢谢!
答案 0 :(得分:0)
您需要使用自定义适配器而不是ArrayAdapter
。这样您就可以根据需要自定义视图。
例如在您的情况下
row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<!-- Name -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Marshmallow"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<!-- Comments-->
<TextView
android:id="@+id/comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="5dp"
android:text="Android 6.0"
android:textColor="@android:color/black" />
<!-- Date and Time-->
<TextView
android:id="@+id/dateandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/comments"
android:layout_marginTop="5dp"
android:text="11th August"
android:textColor="@android:color/black" />
</RelativeLayout>
DataModel.java
public class DataModel {
String name;
String comment,
String date;
public DataModel(String name, String type, String comment, String date) {
this.name=name;
this.comment=comment;
this.date=date;
}
public String getName() {
return name;
}
public String getComment() {
return comment;
}
public String getDate() {
return date;
}
}
最后,您的自定义适配器应如下所示
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
TextView txtComment;
TextView txtDate;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
// your on click code
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.txtComment = (TextView) convertView.findViewById(R.id.comments);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateandtime);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getName());
viewHolder.txtComment.setText(dataModel.getComment());
viewHolder.txtDate.setText(dataModel.getDate());
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
您必须像这样设置适配器。
ArrayList<DataModel> dataModels; // then add your data into it
CustomAdapter adapter= new CustomAdapter(dataModels,getApplicationContext());
listView.setAdapter(adapter);
请查看此example以获得更好的信息。