我正在尝试在从文本文件中读取数据后显示位置的ListView,我在打开活动时创建了自定义适配器和新活动,我只得到一个白色屏幕。
以下是我添加的适配器的代码:
public class LocationAdapter extends BaseAdapter {
private List<Location> Locations;
int monLayout;
LayoutInflater inflater;
public LocationAdapter(Context context, int layout){
monLayout=layout;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Locations = new ArrayList<Location>();
}
private class Location{
public String name;
public String address;
public Long date;
public Location(String _name,String _address , Long _date){
name=_name;
address=_address;
date=_date;
}
}
private class ViewHolder{
TextView name_view;
TextView address_view;
TextView date_view;
public ViewHolder(View rowLayout){
name_view = (TextView)rowLayout.findViewById(R.id.name);
date_view = (TextView)rowLayout.findViewById(R.id.date);
address_view = (TextView)rowLayout.findViewById(R.id.address);
}
}
public void addLocation(String _name,String _address,Long _date){
//Création d'une nouvelle location avec les données en paramètres
Location new_loc = new Location(_name, _address,_date);
//Ajout de la location à la liste des locations
Locations.add(new_loc);
}
/*Méthodes de la classe mère*/
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = inflater.inflate(monLayout, parent, false);
ViewHolder holder = (ViewHolder)view.getTag();
if (holder == null)
{
holder = new ViewHolder(view);
view.setTag(holder);
}
Location location = (Location)getItem(position);
holder.name_view.setText(location.name);
holder.address_view.setText(location.address);
holder.date_view.setText("Test");
return view;
}
}
以下是我的活动的Oncreate方法的代码:
public class SortedLocationsListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_sorted_locations_list);
InputStream is = getResources().openRawResource(R.raw.locations);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
List<String> maListe = new ArrayList<String>();
String myligne;
LocationAdapter adapter = new LocationAdapter(this, R.layout.row_location);
try {
while((myligne = in.readLine()) != null)
{
String a[] = myligne.split(";");
Long _date=Long.parseLong(a[2], 36);
System.out.println(a[0]+" "+a[1]+" "+a[2]);
adapter.addLocation(a[0],a[1],_date);
}
setListAdapter(adapter);
}
catch(IOException e)
{
System.out.println("Error");
}
}
以下是我要用于列表中每个元素的布局的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="5dp"
android:paddingTop="10dp">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Faculté de Droit"
android:textStyle="bold"
android:textColor="#000"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"/>
<TextView android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7 nov. 2014"
android:layout_alignParentRight="true"/>
<TextView android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="41 Boulevard François Mitterand, Clermont-Ferrand"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
提前感谢您的帮助
答案 0 :(得分:1)
您的getCount()
在adapter
中返回0。应该回复:
Locations.size();
答案 1 :(得分:0)
修复适配器:
@Override
public int getCount() {
return Locations.size();
}
@Override
public Object getItem(int position) {
return Locations.get(position);
}
答案 2 :(得分:0)
要了解列表视图并优化它们,请阅读http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
您的问题的修复方法如下,但您也可能希望优化getView()方法(如下所示)。
适配器当前将getCount()返回为0,这意味着它告诉列表视图没有任何显示。这应该是列表的大小。
适配器也为getItem返回null,因此在getView()方法中,调用location.name时会得到一个空指针。 getItem()应该返回Locations.get(position)。
@Override
public int getCount() {
return Locations.size();
}
@Override
public Object getItem(int position) {
return Locations.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
convertView = inflater.inflate(monLayout, parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Location location = (Location) getItem(position);
holder.name_view.setText(location.name);
holder.address_view.setText(location.address);
holder.date_view.setText("Test");
return view;
}