确定由桑迪普的要求,我会给你简单的提示,但我不能给你完整的代码,您可以直接复制和粘贴。只要按照指示
创建,因为你需要这可能有一定的元素列表中的单个项目的XML文件,例如它的single_item.xml
其中有3 TextView
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sample Id"
android:textColor="#0414f4"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="10sp" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Sample Title"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sample body"
android:textSize="16sp"/>
</LinearLayout>
,并从主得到的listView
基准后,布局设置这样的适配器,并且这里modelList
是ArrayList
。
mAdapter = new CustomAdapter(MainActivity.this, R.layout.single_item, modelList);
mAdapter.notifyDataSetChanged();
mListView.setAdapter(mAdapter);
那么类CustomAdapter
看起来像这样
public class CustomAdapter extends ArrayAdapter<Model> {
ArrayList<Model> modelList;
Context context;
public CustomAdapter(Context context, int resource, ArrayList<Model> objects) {
super(context, resource, objects);
this.modelList = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.single_item,parent,false);
}
// Lookup view for data population
TextView tvId = (TextView) convertView.findViewById(R.id.tvId);
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
TextView tvBody = (TextView) convertView.findViewById(R.id.tvBody);
// Populate the data into the template view using the data object
tvId.setText(String.valueOf(model.getId()));
tvTitle.setText(model.getroomId());
tvBody.setText(model.getName());
// Return the completed view to render on screen
return convertView;
}
}
注:我评论,并使它尽可能简单,以帮助您。希望这可能有所帮助。如果出现任何问题,您可以评论。
问题一点也不清楚,请您详细说明 –
您正在询问代码,未修复**现有**代码。这个问题很快就会下降,不清楚,而且格式不正确。看看一些listView教程,他们会让你开始。 – Talha
你为什么要求我们这样做呢?你有什么尝试? –