Android适配器,用于基于类的自定义布局列表视图



我有一个类看起来像这样:

public class Vizita {
     public int icon;
        public String titlu;
        public String loc;
        public String idviz;
        public Vizita(){
            super();
        }
        public Vizita(int icon, String titlu, String loc, String idviz) {
            super();
            this.icon = icon;
            this.titlu = titlu;
            this.loc = loc;
            this.idviz = idviz;
        }
}

我使用AsyncTask从PHP中检索JSON数组。所有功能都非常适合列表视图的简单布局。但我想使用更复杂的布局,所以我设计了一个使用上面Vizita.class中所有4个值的布局。但我不知道如何为它设置适配器,因为我尝试它的方式会给我带来错误。

当然,我正试图在onPostExecute中做到这一点,就像这样:

public class MyActivity extends Activity {
{...}
JSONArray lststat;
{...}
public JSONArray liststatus() {
    JSONArray jdata=post.getserverdataX(url_connectX);
    return jdata;
}   
class asyncpost extends AsyncTask< String, String, String > {
Vizita weather_data[];
....
protected void onPostExecute(String result) {
VizitaAdapter adapter = new VizitaAdapter(MyActivity.this,R.layout.listview_item_row, weather_data);
myListView.setAdapter(adapter);   
...}

在doInBackground中,我处理的JSON如下:

lststat = liststatus();
JSONObject json_data; 
try {
     int length = lststat.length();
     Vizita weather_data[] = new Vizita[length];
     for (int i = 0; i < length; i++)
     {
        json_data = lststat.getJSONObject(i);
        weather_data[i].titlu =json_data.getString("clientnume");
        weather_data[i].idviz =json_data.getString("idvizite");
        weather_data[i].loc =json_data.getString("localitate");
     }
} catch (Exception e) {
Log.e(null, e.toString());
}            

其中liststatus()是我调用asyncTask以检索所需的JSONArray的位置。

但是我在for循环中收到了java.lang.NullPointerException(如果我对setAdapter行进行注释)。

我想我没有正确初始化weather_data数组?!?!?如果我(取消注释)在onPostExecute中设置适配器,我会收到一个崩溃错误"强制关闭"我在IDE中没有收到任何错误,看起来语法很好。

请告诉我我做错了什么?我该怎么修?

VizitaAdapter的代码如下

public class VizitaAdapter extends ArrayAdapter<Vizita>{
    Context context;
    int layoutResourceId;   
    Vizita data[] = null;
    public VizitaAdapter(Context context, int layoutResourceId, Vizita[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtLoco = (TextView)row.findViewById(R.id.txtLocalitate);
            holder.txtIdviz = (TextView)row.findViewById(R.id.txtIdVizite);
            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }
        Vizita weather = data[position];
        holder.txtTitle.setText(weather.titlu);
        holder.txtLoco.setText(weather.loc);
        holder.txtIdviz.setText(weather.idviz);
        holder.imgIcon.setImageResource(weather.icon);
        return row;
    }
    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtLoco;
        TextView txtIdviz;
    }    
}   

可能您忘记初始化循环中的Vizita对象:

json_data = lststat.getJSONObject(i);
weather_data[i] = new Vizita();
weather_data[i].titlu =json_data.getString("clientnume");
weather_data[i].idviz =json_data.getString("idvizite");
weather_data[i].loc =json_data.getString("localitate");

最新更新