我正在尝试在自定义列表视图中添加两个textview。它显示数据但不合并它们。它显示为不同的布局。
这是自定义列表视图。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/rollnoText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"
android:background="@drawable/custom_stuadd_drawable"
android:paddingLeft="10dip"
android:textColor="@android:color/primary_text_light"
/>
<TextView
android:id="@+id/stunameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:background="#00dcdcdc"
android:paddingLeft="10dip"
android:textColor="#b9dcdcdc"
android:layout_below="@+id/rollnoText"
/>
</RelativeLayout>
这是CustomAdapter类
public CustomStuDataAdapter(Context context, int resource, List<String> objects) {
super(context, R.layout.custom_student_data, objects);
}
static class ViewHolder{
TextView Roll_No;
TextView Stu_Name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
LayoutInflater StuInflater = LayoutInflater.from(getContext());
convertView = StuInflater.inflate(R.layout.custom_student_data, parent, false);
viewHolder = new ViewHolder();
//String str = getItem(position);
viewHolder.Roll_No = (TextView) convertView.findViewById(R.id.rollnoText);
viewHolder.Stu_Name = (TextView)convertView.findViewById(R.id.stunameText);
// System.out.println(stnm);
// rno=getItem(position);
viewHolder.Roll_No.setText(getItem(position));
// stnm=getItem(position);
//viewHolder.Stu_Name.setText(getItem(position));
convertView.setTag(viewHolder);
return convertView;
}
这是主要的课程
List<String> details = new ArrayList<String>();
studbhandler = new CourseDbHandler(this, null, null, 1);
Cursor c = studbhandler.stuData(tbnm);
c.moveToFirst();
if(c.equals(null)){
System.out.println("NO DATA");
}
else {
if(c.moveToFirst()) {
do {
rn=c.getString(0);
details.add(rn);
nm=c.getString(1);
details.add(nm);
} while (c.moveToNext());
studlist = (ListView)findViewById(R.id.studentList);
ListAdapter stuAdapter = new CustomStuDataAdapter(this,R.layout.activity_add_student,details);
studlist.setAdapter(stuAdapter);
}
}
输出就是这样 here i want to combine two rows like first and second then third and fourth and so on...
答案 0 :(得分:0)
添加自定义布局Textview
android:layout_toLeftOf="@+id/rollnoText"
删除
android:layout_below="@+id/rollnoText"
TextView如下所示
<TextView
android:id="@+id/stunameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:background="#00dcdcdc"
android:paddingLeft="10dip"
android:textColor="#b9dcdcdc"
android:layout_toLeftOf="@+id/rollnoText"
/>
答案 1 :(得分:0)
像这样创建自定义模型类
public class Details {
private String rollNo;
private String stuName;
public Details(String rollNo, String surName){
this.rollNo = rollNo;
this.stuName = stuName;
}
public String getRollNo(){
return this.rollNo;
}
public String getStuName(){
return this.stuName;
}
}
并将List<String>
更改为List<Details> detailsList = new ArrayList<Details>();
你做的方法应该是这样的。
Details details;
do {
details = new Details(c.getString(0),c.getString(1));
detailsList.add(details);
} while (c.moveToNext());
你的CustomStuDataAdapter
应该这样开始,
public CustomStuDataAdapter(Context context, int resource, List<Details> objects) {
在getView
,
viewHolder.Roll_No.setText(getItem(position).getRollNo());
viewHolder.Stu_Name.setText(getItem(position).getStuName());
这就是全部。
答案 2 :(得分:0)
您需要首先创建模型类我假设您正在为学生创建模型类,因此将其命名为StudentModel:
public class StudentModel
{
public String name;
public String rollno;
public StudentModel(String _name,String _rollno)
{
name = _name;
rollno = _rollno;
}
}
现在您的客户列表适配器类
public class CustomStuDataAdapter extends ArrayAdapter<StudentModel> {
Context _context;
private ArrayList<StudentModel> objects;
LayoutInflater inflater;
public CustomStuDataAdapter (Context context,
ArrayList<StudentModel> objects) {
super(context,0, objects);
this.objects = objects;
_context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder{
TextView Roll_No;
TextView Stu_Name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = this.getItemViewType(position);
StudentModel item = objects.get(position);
// convertView = null;
View rootView = convertView;
final ViewHolder viewHolder;
if (rootView == null) {
viewHolder= new ViewHolder();
rootView = inflater.inflate(R.layout.you_list_view_row,
parent, false);
viewHolder.Roll_No = (TextView) rootView .findViewById(R.id.rollnoText);
viewHolder.Stu_Name = (TextView)rootView .findViewById(R.id.stunameText);
rootView.setTag(viewHolder);
}
else {
viewHolder= (ViewHolder) rootView.getTag();
}
viewHolder.Roll_No.setText(item.rollno);
viewHolder.Stu_Name.setText(item.name);
return rootview;
}
}
现在该怎么称呼它:
ArrayList<StudentModel> details = new ArrayList<StudentModel>();
studbhandler = new CourseDbHandler(this, null, null, 1);
Cursor c = studbhandler.stuData(tbnm);
c.moveToFirst();
while(c.isAfterLast() == false){
rn=c.getString(0);
nm=c.getString(1);
details.add(new StudentModel(nm,rn));
c.moveToNext();
}
CustomStuDataAdapter adapter = new CustomStuDataAdapter(this, details);
listview.setAdapter(adapter);
答案 3 :(得分:0)
使用此布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation:"horizontal" >
<TextView
android:id="@+id/rollnoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"
android:background="@drawable/custom_stuadd_drawable"
android:paddingLeft="10dip"
android:textColor="@android:color/primary_text_light"
/>
<TextView
android:id="@+id/stunameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="#00dcdcdc"
android:paddingLeft="10dip"
android:textColor="#b9dcdcdc"
android:layout_below="@+id/rollnoText"
/>
</LinearLayout>