我知道,这里有很多关于这个主题的问题,谷歌有很多关于它的教程,但我仍然有问题,所以请告诉我我错在哪里; /。有我的课程:
布局类(activity_finish.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">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="3dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
行布局(list_row.xml):
<?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:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="@string/desc"
android:src="@drawable/icon" />
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_alignBottom="@+id/destinationName"
android:gravity="right"
android:text="@string/time"
android:textColor="#10bcc9"
android:textSize="23dip" />
<TextView
android:id="@+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/destination"
android:textColor="#343434"
android:textSize="13dip" />
<TextView
android:id="@+id/destinationName"
android:layout_width="153dip"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/destination"
android:layout_below="@+id/destination"
android:text="@string/destinationName"
android:textColor="#040404"
android:textSize="20dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/stopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/thumbnail"
android:layout_below="@+id/thumbnail"
android:text="@string/stopName"
android:layout_marginLeft="3dip" />
<TextView
android:id="@+id/stopStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/thumbnail"
android:layout_below="@+id/stopName"
android:layout_marginTop="3dip"
android:text="@string/stopStreet"
android:layout_marginLeft="3dip"/>
<TextView
android:id="@+id/stopOneWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/time"
android:layout_below="@+id/stopName"
android:layout_marginTop="3dip"
android:text="@string/stopOneWay"
android:layout_marginLeft="3dip"/>
<TextView
android:id="@+id/lineNumber"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignLeft="@+id/thumbnail"
android:layout_alignTop="@+id/thumbnail"
android:gravity="center"
android:text="@string/lineNumber"
android:layout_marginLeft="3dip"
android:textStyle="bold"
android:textSize="28dip"
android:textColor="#DCF0F5"/>
</RelativeLayout>
StopAdapter:
public class StopAdapter extends ArrayAdapter<DisplayStop> {
private List<DisplayStop> stops;
private LayoutInflater inflater;
private Context context;
int layoutResourceId;
public StopAdapter(Context context, int layoutResourceId, List<DisplayStop> stops) {
super(context, layoutResourceId, stops);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.stops = stops;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder.image = (ImageView) row.findViewById(R.id.list_image);
holder.time = (TextView) row.findViewById(R.id.time);
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
holder.stopName = (TextView) row.findViewById(R.id.stopName);
holder.stopStreet = (TextView) row.findViewById(R.id.stopStreet);
holder.stopOneWay = (TextView) row.findViewById(R.id.stopOneWay);
holder.lineNumber = (TextView) row.findViewById(R.id.lineNumber);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
holder.time.setText(stops.get(position).getTime());
holder.destination.setText("Kierunek:");
holder.destinationName.setText(stops.get(position).getDestination());
holder.stopName.setText(stops.get(position).getName());
holder.stopStreet.setText(stops.get(position).getAddress());
holder.lineNumber.setText(stops.get(position).getLine());
if(stops.get(position).getOneWay()) holder.stopOneWay.setText("P. jednokierunkowy");
else holder.stopOneWay.setText("");
int line = Integer.parseInt(stops.get(position).getLine());
if(line >= 50){
holder.image.setImageResource(R.drawable.icon);
}
else{
holder.image.setImageResource(R.drawable.icon2);
}
return row;
}
}
FinishActivity:
public class FinishActivity extends Activity {
private ListView lv;
private List<DisplayStop> stops;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
lv = (ListView) findViewById(R.id.list);
stops = getStops();//create several objects in list
if(stops.size() > 0){
StopAdapter stopAdapter = new StopAdapter(this, R.layout.list_row, stops);
lv.setAdapter(stopAdapter);
}
else
{
Log.d("klop", "0 elementów");
}
}
请帮助,因为我不知道出了什么问题(我从几个教程中做了几个版本,但仍然没有; /)
答案 0 :(得分:1)
尝试不扩展ArrayAdapter类,并直接声明它:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrayListData);
lv.setAdapter(adapter);
如果你所做的只是从Array / ArrayList中放置项目,那么除了使用ArrayAdapter之外你不需要做更多的事情
答案 1 :(得分:0)
在activity_finish.xml中ListView的定义中,将height设置为fill_parent
android:layout_height="fill_parent"
列表视图内容在充气时没有高度,只添加一个项目。
此外,无需持有指向适配器中项目的指针
删除它:
private List<DisplayStop> stops;
然后,当您想要访问ListAdapter中的项目时,请改为使用:
DisplayStop item = this.getItem(position);
答案 2 :(得分:0)
<?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">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="3dp"
android:listSelector="@drawable/list_selector" />
</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:background="@drawable/list_selector"
android:padding="5dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="@string/desc"
android:src="@drawable/icon" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_alignBottom="@+id/destinationName"
android:gravity="right"
android:text="@string/time"
android:textColor="#10bcc9"
android:textSize="23dip" />
<TextView
android:id="@+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/destination"
android:textColor="#343434"
android:textSize="13dip" />
<TextView
android:id="@+id/destinationName"
android:layout_width="153dip"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/destination"
android:layout_below="@+id/destination"
android:text="@string/destinationName"
android:textColor="#040404"
android:textSize="20dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/stopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/thumbnail"
android:layout_below="@+id/thumbnail"
android:text="@string/stopName"
android:layout_marginLeft="3dip" />
<TextView
android:id="@+id/stopStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/thumbnail"
android:layout_below="@+id/stopName"
android:layout_marginTop="3dip"
android:text="@string/stopStreet"
android:layout_marginLeft="3dip"/>
<TextView
android:id="@+id/stopOneWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/time"
android:layout_below="@+id/stopName"
android:layout_marginTop="3dip"
android:text="@string/stopOneWay"
android:layout_marginLeft="3dip"/>
<TextView
android:id="@+id/lineNumber"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignLeft="@+id/thumbnail"
android:layout_alignTop="@+id/thumbnail"
android:gravity="center"
android:text="@string/lineNumber"
android:layout_marginLeft="3dip"
android:textStyle="bold"
android:textSize="28dip"
android:textColor="#DCF0F5"/>
</RelativeLayout>
public class StopAdapter extends ArrayAdapter<DisplayStop> {
private List<DisplayStop> stops;
public StopAdapter(Context context, int layoutResourceId, List<DisplayStop> stops) {
super(context, layoutResourceId, stops);
this.stops = stops;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_row, parent, false);
holder.image = (ImageView) row.findViewById(R.id.list_image);
holder.time = (TextView) row.findViewById(R.id.time);
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
holder.stopName = (TextView) row.findViewById(R.id.stopName);
holder.stopStreet = (TextView) row.findViewById(R.id.stopStreet);
holder.stopOneWay = (TextView) row.findViewById(R.id.stopOneWay);
holder.lineNumber = (TextView) row.findViewById(R.id.lineNumber);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
holder.time.setText(stops.get(position).getTime());
holder.destination.setText("Kierunek:");
holder.destinationName.setText(stops.get(position).getDestination());
holder.stopName.setText(stops.get(position).getName());
holder.stopStreet.setText(stops.get(position).getAddress());
holder.lineNumber.setText(stops.get(position).getLine());
if(stops.get(position).getOneWay())
holder.stopOneWay.setText("P.jednokierunkowy");
else
holder.stopOneWay.setText("");
int line = Integer.parseInt(stops.get(position).getLine());
if(line >= 50){
holder.image.setImageResource(R.drawable.icon);
}
else{
holder.image.setImageResource(R.drawable.icon2);
}
return row;
}
}
public class FinishActivity extends Activity {
private ListView lv;
private List<DisplayStop> stops;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
lv = (ListView) findViewById(R.id.list);
stops = getStops();//create several objects in list
StopAdapter stopAdapter = new StopAdapter(this, R.layout.list_row, stops);
lv.setAdapter(stopAdapter);
}
else
{
Log.d("klop", "0 elementów");
}
}
答案 3 :(得分:0)
好的,我发现问题 - 这真的很愚蠢 - 在StopAdapter中我犯了错误:
holder.destination = (TextView) row.findViewById(R.id.destination);
holder.destination = (TextView) row.findViewById(R.id.destinationName);
所以目标中的文本为null。它应该是:
holder.destination = (TextView) row.findViewById(R.id.destination)
holder.destinationName = (TextView) row.findViewById(R.id.destinationName);
一切都很好。感谢您的帮助 - 我知道最简单的错误是最难找到的:)