大家好,在我的应用程序中,我正在显示多列列表视图(列表有 3 列),但在我的应用程序中,它显示的所有列都相同。我的列表视图布局如下:
<?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="fill_parent"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingTop="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@drawable/listbackground"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ltp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/down" />
<TextView
android:id="@+id/perChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@drawable/listbackground"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/Name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ltp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/down" />
<TextView
android:id="@+id/perChange1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@drawable/listbackground"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/Name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ltp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/down" />
<TextView
android:id="@+id/perChange2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
final ListView lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
public class MyCustomBaseAdapter extends BaseAdapter {
private ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context,
ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.Name);
holder.txtCityState = (TextView) convertView
.findViewById(R.id.ltp);
holder.txtPhone = (TextView) convertView
.findViewById(R.id.perChange);
holder.txtName1 = (TextView) convertView
.findViewById(R.id.Name1);
holder.txtCityState1 = (TextView) convertView
.findViewById(R.id.ltp1);
holder.txtPhone1 = (TextView) convertView
.findViewById(R.id.perChange1);
holder.txtName2 = (TextView) convertView
.findViewById(R.id.Name2);
holder.txtCityState2 = (TextView) convertView
.findViewById(R.id.ltp2);
holder.txtPhone2 = (TextView) convertView
.findViewById(R.id.perChange2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
System.out.println("position::" + position);
holder.txtName
.setText(searchArrayList.get(position).getStockCode());
holder.txtCityState.setText(searchArrayList.get(position).getLtp());
holder.txtPhone.setText(searchArrayList.get(position)
.getPerChange());
holder.txtName1.setText(searchArrayList.get(position)
.getStockCode());
holder.txtCityState1
.setText(searchArrayList.get(position).getLtp());
holder.txtPhone1.setText(searchArrayList.get(position)
.getPerChange());
holder.txtName2.setText(searchArrayList.get(position)
.getStockCode());
holder.txtCityState2
.setText(searchArrayList.get(position).getLtp());
holder.txtPhone2.setText(searchArrayList.get(position)
.getPerChange());
return convertView;
}
class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
TextView txtName1;
TextView txtCityState1;
TextView txtPhone1;
TextView txtName2;
TextView txtCityState2;
TextView txtPhone2;
}
}
它显示我的输出为
现在在这里它显示所有列相同的数据,但我想要的是显示所有列不同的数据,如何做到这一点?任何帮助/建议将不胜感激,在此先感谢。
谢谢山姆,但现在如果名称很大,那么我的输出看起来会出现问题:假设 Vishal 变为 Vishallllllllllllllllllllll