2012-10-30 52 views
0

大家好我在我的應用程序中顯示多列listview(列表有3列),但在我的應用程序中它顯示所有列相同的vaue。我的列表視圖佈局是這樣的:所有列在多列列表視圖中顯示相同的數據

<?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; 
    } 
} 

這表明我輸出

enter image description here

現在,這裏顯示了所有列相同的數據,但我要的是展現所有列的不同數據,如何做到這一點?任何幫助/建議將不勝感激,提前致謝。

謝謝山姆,但現在有問題,如果名稱爲大,那麼我的輸出看起來像:假設維沙爾成爲Vishalllllllllllllllllllll

enter image description here

+1

我建議簡化您的佈局一個盒子和使用GridView。 – Sam

+0

使用gridview而不是列表視圖 – njzk2

+0

我可以在gridview中的那個框中使用三行​​嗎?如果是,你可以向我展示任何示例/教程。謝謝 – PPD

回答

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)); 
+0

非常感謝您提供的很好的解釋和簡化,因爲我現在無法實施一些技術問題,Tommarow在早上我會這樣做。再次感謝你=) – PPD

+0

哦,它完美的作品,非常感謝。 – PPD

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()); 

做一個調試/斷點上得到我相信你的代碼將意味着「位置」詮釋將是你的三個框的各行上相同。因此,它會返回相同的數據並將其放入該行的每個框中,這意味着您有重複項。

你會想這樣做

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()); 

但我同意上司的評價,網格佈局將得到更好的。

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()); 
} 
相關問題