我看不到添加到列表视图中的行,我想知道它是否与我的布局或 ListViewAdapter 有关。在调试中,我可以看到我的数组充满了传递给我的 ArrayList playerList 的数据,我在 Toast 中看到了数据,但屏幕上没有显示任何内容。主要活动 xml:
<?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;
}