2013-06-29 173 views
0

我看不到添加到列表视图的行,并且想知道它是否与我的布局或ListViewAdapter有关。在调试中,我可以看到我的数组充满了传递给我的ArrayList playerList的数据,并且我看到了Toast中的数据,但屏幕上没有显示。 MainActivity XML:Android自定义行布局不在列表视图中显示

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/txtV_battingOrder_Title" 
     android:textSize="20sp" /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/btn_AddButton_title" /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.89" > 
    </ListView> 

</LinearLayout> 

新行布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@android:color/transparent" 
    android:padding="5dp" > 

    <TextView 
     android:id="@+id/txtV_player_input_position_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="25dp" 
     android:layout_marginTop="21dp" 
     android:width="180dp" /> 

    <Button 
     android:id="@+id/btn_Edit_Title" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="@dimen/btn_edit_player_width" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/txtV_player_input_position_title" 
     android:layout_alignBottom="@+id/txtV_player_input_position_title" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="14dp" 
     android:padding="3dp" 
     android:text="@string/bnt_Edit_title" /> 

</RelativeLayout> 

定制ListViewAdapter

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 

public class CustomListViewAdapter extends BaseAdapter 
{ 
    LayoutInflater inflater; 
    List<String> playerList; 

    public CustomListViewAdapter(Activity context, List<String> playerList) { 
     super(); 

     this.playerList = playerList; 
     this.inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return playerList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    static class ViewHolder { 
     public TextView text; 
     public Button edit; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     String player = playerList.get(position); 
     View vi=convertView; 

     if(convertView==null) 
      vi = inflater.inflate(R.layout.new_player_view, null); 
      ViewHolder holder = new ViewHolder(); 
//  assign components from new_player_view 
//  TextView playerInfo = (TextView)vi.findViewById(R.id.txtV_player_input_position_title); 
     holder.text = (TextView)vi.findViewById(R.id.txtV_player_input_position_title); 
     holder.edit = (Button)vi.findViewById(R.id.btn_Edit_Title); 
//  Button editButton = (Button) vi.findViewById(R.id.btn_Edit_Title); 
     holder.edit.setOnClickListener(MainActivity.editPlayerButtonListener); 
     holder.text.setText(player); 
     //playerInfo.setText("this is a test"); 
     vi.setTag(holder); 

     return vi; 
    } 
+1

要扩展哪种适配器? – Blackbelt

+0

公共类CustomListViewAdapter扩展BaseAdapter { – user2533762

+1

发布整个适配器 – Blackbelt

回答

1

首先,您使用的是您的xml太多的重量属性..你应该明智地使用它,我不知道为什么RelativeLayoutweight 1,但无论如何,我认为你可能会遇到的问题之一荷兰国际集团:

  • 确保getCount()返回> 0
  • 确保您使用此方法做充气:inflater.inflate(R.layout.new_player_view, parent, false);
  • 改变该行布局的follwing:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/transparent" 
        android:padding="5dp" > 
    
        <TextView 
         android:id="@+id/txtV_player_input_position_title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentTop="true" 
         android:layout_marginLeft="25dp"/> 
    
        <Button 
         android:id="@+id/btn_Edit_Title" 
         style="?android:attr/buttonStyleSmall" 
         android:layout_width="@dimen/btn_edit_player_width" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_marginRight="14dp" 
         android:padding="3dp" 
         android:text="@string/bnt_Edit_title" /> 
    

而主布局ListView到:

<ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

我希望事情会帮助你,否则,请发表适配器的全部代码。

编辑:

请尝试看看ViewHolder模式实现的this例如,工作正常,它应该帮助你实现你的好。

+0

我尝试了所有更改。仍然是相同的输出(没有任何可见的)我在那里从我以前的尝试在同一个项目中增加了一个表格行到tablelayout(我认为)的权重。我一直试图让这个工作很长一段时间。 – user2533762

+0

我更新了我的答案,你确定getCount()是> 0吗?或者你也可以看到屏幕上的按钮btn_AddButton_title?由于您使用的适配器在我看来似乎很好.. – Cata

+0

我从您建议的tut实施了对适配器的更改。它没有改变输出,但它给了我一个想法,在我的mainActivity中创建一个循环,以添加到我的列表和成功!我的列表显示了所有添加的项目。现在我只需要弄清楚为什么。但我正在慢慢解决这个问题。我感谢您的帮助。在我创建onCreate循环之前 - 没有按钮没有出现。 – user2533762

1

您已将layout_weight设置为0.89,但未设置父布局的weightSum。为您的母公司LinearLayout设置android:weightSum = "1"

相关问题