0

大家好,在我的应用程序中,我正在显示多列列表视图(列表有 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

在此处输入图像描述

4

3 回答 3

1

我可以在gridview中的那个框中使用三行​​吗?如果是的话,你能告诉我任何样本/教程吗?

以下是如何修改现有代码示例以使用 GridView。

首先将您的布局简化为一个框,将其另存为grid_item.xml

<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" />

    <TextView
        android:id="@+id/perChange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/down" />

</LinearLayout>

(注意我合并了最后一个TextView和ImageView,你可能需要调整一下。)

其次简化您的getView方法:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        // Change the layout that is loaded
        convertView = mInflater.inflate(R.layout.grid_item, 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);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    System.out.println("position::" + position);

    SearchResult result = searchArrayList.get(position);
    holder.txtName.setText(result.getStockCode());
    holder.txtCityState.setText(result.getLtp());
    holder.txtPhone.setText(result.getPerChange());
    return convertView;
}

class ViewHolder {
    TextView txtName;
    TextView txtCityState;
    TextView txtPhone;
}

最后更改这些参考资料(以及您可能拥有的任何其他参考资料):

final GridView gv = (GridView) findViewById(R.id.srGridView);
gv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
于 2012-10-30T17:46:00.060 回答
0

看起来你的问题是问题。

holder.txtName2.setText(searchArrayList.get(position)
                .getStockCode());
        holder.txtCityState2
                .setText(searchArrayList.get(position).getLtp());
        holder.txtPhone2.setText(searchArrayList.get(position)
                .getPerChange());

holder.txtName1.setText(searchArrayList.get(position)
                .getStockCode());

在获取上做一个调试/断点我相信你的代码将意味着每行的三个盒子的“位置”int将是相同的。因此,它返回相同的数据并将其放入该行中的每个框中,这意味着您有重复项。

你会想做类似的事情

 holder.txtName2.setText(searchArrayList.get(position)
                    .getStockCode());
            holder.txtCityState2
                    .setText(searchArrayList.get(position).getLtp());
            holder.txtPhone2.setText(searchArrayList.get(position)
                    .getPerChange());

    holder.txtName1.setText(searchArrayList.get(position+column2dataoffset)
                    .getStockCode());

但我同意评论,网格布局会更好。

于 2012-10-30T17:32:42.233 回答
0

在您现有的代码中尝试此代码可能会对您有所帮助...

if((position * 3) <= getCount()){
    holder.txtName
            .setText(searchArrayList.get(position * 3).getStockCode());
    holder.txtCityState.setText(searchArrayList.get(position * 3).getLtp());
    holder.txtPhone.setText(searchArrayList.get(position * 3)
            .getPerChange());
}

if(((position * 3) + 1) <= getCount()){
    holder.txtName1.setText(searchArrayList.get((position * 3) + 1)
            .getStockCode());
    holder.txtCityState1
            .setText(searchArrayList.get((position * 3) + 1).getLtp());
    holder.txtPhone1.setText(searchArrayList.get((position * 3) + 1)
            .getPerChange());
}

if(((position * 3) + 2) <= getCount()){
    holder.txtName2.setText(searchArrayList.get((position * 3) + 2)
            .getStockCode());
    holder.txtCityState2
            .setText(searchArrayList.get((position * 3) + 2).getLtp());
    holder.txtPhone2.setText(searchArrayList.get((position * 3) + 2)
            .getPerChange());
}
于 2012-10-30T18:01:37.947 回答