将动态 ArrayList 添加到 ArrayAdapter 并显示在屏幕上

Add Dynamic ArrayList to ArrayAdapter and display on screen

我很难用 ArrayList 中的动态项填充 ListView 并将它们显示在屏幕上。

我有 2 个 xml 个文件:
这是我要用来填充 ListView 的 activity 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/activity_local_reps"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="airbornegamer.com.grgr4.ActivityLocalReps">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

还有我的 xml 文件,我想用它来将 image/text 添加到列表视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
    android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/unknown_representative" />
<TextView
    android:id="@+id/Itemname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingTop="5dp"/>

在我的文件中 ActivityLocalReps.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_reps);
    //setting up data and populating repData.
    DisplayData(repData);
}    

public void DisplayData(LocalRepData repData){    
    allRepData = repData.queryAllNeededRepData();
    adapter=new ArrayAdapter<String>(this, R.layout.mylist, R.id.Itemname, allRepData);
}    

我的 allRepData 中有多个元素,但是适配器似乎没有将数据连接到我的 listView。任何帮助都会很棒,谢谢!

**编辑 1

public class LocalRepData {

Context mContext;

public LocalRepData(Context mContext) {
    this.mContext = mContext;
}    
//other methods and stuff........
}    

我主要使用这个 class 进行数据检索,所以目前我没有在那里扩展任何东西。

改为

 adapter=new ArrayAdapter<String>(this, R.layout.mylist, R.id.Itemname, allRepData);

使用您的 LocalRepData 类型的自定义 ArrayAdapter,并根据自定义适配器的需要实施 getView() 方法和其他方法。那行得通。

您似乎在使用自定义适配器,您在适配器 getview 中扩充了项目。您将在哪里将数据附加到第二个 xml 您的展示。所以你得到了一个 LocalRepData 列表,每个 localrepdata 都有一个字符串和图像。将它们绑定到适配器的 getView 方法中。 你的 oncreate 没有显示你正在建立你的 repData。必须确保它是空的。

如果您要在列表视图的列表项中使用多个小部件,例如 EditText 和 ImageView,则必须使用 ArrayAdapter 进行扩展并在 getView() 方法中填充它们。

如果 LocalRepData 是您创建的包含信息的 class,那么我会说它很好。

你想了解的好像是ListView和它的Adapter之间的关系。所以你建立了一个List<LocalRepData> theList;。然后你想为你的 ListView 创建一个自定义适配器,所以扩展 BaseAdapter 并且当你实例化适配器时,你会想要传入你的 LocalRepData 列表。在你的 BaseAdapter 中,你将从列表中获取每个项目并获取其图像和字符串,然后将它们附加到你的第二个 xml,最后通过返回它来膨胀视图。所以我将在这里向您展示我的意思的示例。

我创建了自己的简单 BaseAdapter,并且只使用标准 ListView。我使用一个名为 WeatherLocation 的容器 class 来存放我的物品。 WeatherLocation 只是一个 class,它存储一个区域的本地天气(天气、邮政编码、解析时的天气描述等)。我要做的是构建一个 List<WeatherLocations> weatherLocations,然后将其传递到我的适配器中。一旦适配器被实例化,它将抓取每个 WeatherLocation(取决于显示的行)并对其进行膨胀。

List<WeatherLocation> savedList = dbHelper.getAllWeatherLocations();
    //savedList.add(new WeatherLocation(72, "04005", "Biddeford"));
    if(savedList.size() > 0){
        mWeatherLocationAdapter = new WeatherLocationAdapter(savedList, this, R.layout.view_weather_location);
        lvLocations.setAdapter(mWeatherLocationAdapter);
    }

你可以在上面看到我建立了我的 WeatherLocation 列表并使用我的自定义 xml 抛出它们,当我将列表视图中的每个视图填充到我的适配器中时实例化它。下面是我的自定义适配器,只有您关心的 3 个方法,getCount、getItem 和 getView()。 GetCount 和 getView 是您绝对需要关注的。

public class WeatherLocationAdapter extends BaseAdapter{
private List <WeatherLocation> mLocations;
private Context mContext;
private int rowForLocationToInflate;
private LayoutInflater inflater;
private SettingsManager mSettingsMananger;

public WeatherLocationAdapter(List<WeatherLocation> mLocations, Context mContext, int rowForLocationToInflate) {
    this.mLocations = mLocations;
    this.mContext = mContext;
    this.rowForLocationToInflate = rowForLocationToInflate;
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // to grab the saved zipcode (selected zipcode)
    mSettingsMananger = new SettingsManager(mContext);
}

private void addLocation(WeatherLocation newLocation){
    mLocations.add(newLocation);
}

@Override
public int getCount() {
    return mLocations.size();
}

@Override
public WeatherLocation getItem(int position) {
    return mLocations.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        //TODO build a viewholder

        // Inflate up
        View rowView = inflater.inflate(rowForLocationToInflate, parent, false);

        // Link up
        TextView tvZipcode = (TextView) rowView.findViewById(R.id.tvZipCode);
        TextView tvCity = (TextView) rowView.findViewById(R.id.tvCity);
        TextView tvTemp = (TextView) rowView.findViewById(R.id.tvDegree);

        // we will do logic here to pick the image
        TextView tvDesc = (TextView) rowView.findViewById(R.id.tvDesc);
        TextView tvTime = (TextView) rowView.findViewById(R.id.tvTimeStamp);
        ImageView ivWeather  =  (ImageView) rowView.findViewById(R.id.ivWeather);
        TextView tvSelected = (TextView) rowView.findViewById(R.id.tvCurrentlySelected);

        tvZipcode.setText(mLocations.get(position).getZipcode());
        tvCity.setText(mLocations.get(position).getCity());
        tvTemp.setText(String.valueOf(mLocations.get(position).getTemperature()));
        tvDesc.setText(mLocations.get(position).getDesc());

        // whats currently selected.
        if(mSettingsMananger.getZipCode().equals(mLocations.get(position).getZipcode())){
            tvSelected.setText("Currently Selected");
        }

        // Format the saved time.
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(mLocations.get(position).getTime_stamp());
        String time = checkForMilitaryTime(cal.getTime().getHours(), cal.getTime().getMinutes());
        tvTime.setText(time);

        // Decide what image to display - based off the description we got back.
        String desc = mLocations.get(position).getDesc();
        if(desc.equals("sky is clear")){
            ivWeather.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_action_sun));
        }
        else if(desc.equals("few clouds")){
            ivWeather.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_action_cloudy));
        }
        else if(desc.equals("broken clouds")){
            ivWeather.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_action_cloudy));
        }
        else if(desc.equals("light intensity shower rain")){
            ivWeather.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_action_rain));
        }



        return rowView;
}

您可以在 getView() 中看到所有工作发生的地方,每次显示列表视图上的项目时,getview() 都需要扩充该视图。因此,您可以看到 XML 中的每个视图都被抓取,并且我将我的字符串附加到它们受尊重的项目上。