自定义适配器永远不会

时间:2014-10-20 06:25:33

标签: android listview baseadapter custom-adapter onscrolllistener

custom adapter使用从服务器获取的数据填充自定义listview。我想要的是检查adapter是否为空,如果数据为空,则将数据附加到listview,否则将listview填入数据notifyDataSetChanged。我正在实施OnScrollListener以从服务器加载更多数据。但是adapter永远不会为空,并且始终会调用notifyDataSetChanged

我的列表活动

public class ListResultActivity extends Activity implements OnScrollListener{

private ArrayList<BusinessListData> businesses;
private ListView businessList;
private LayoutInflater layoutInflator;
private BusinessListIconTask imgFetcher;
BusinessListDataAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.businesslist);
    this.businessList = (ListView) findViewById(R.id.lvBusinesslist);
    this.adapter= new BusinessListDataAdapter(this,
            this.imgFetcher, this.layoutInflator, this.businesses);     
    getData();

    businessList.setOnScrollListener(this);
}

@Override
public Object onRetainNonConfigurationInstance() {
    Object[] myStuff = new Object[2];
    myStuff[0] = this.businesses;
    myStuff[1] = this.imgFetcher;
    return myStuff;
}

/**
 * Bundle to hold refs to row items views.
 * 
 */
public static class MyViewHolder {
    public TextView businessName, businessAddress, phoneNo;
    public Button btnProfile;
    public ImageView icon;
    public BusinessListData business;
}

public void setBusinesses(ArrayList<BusinessListData> businesses) {

    this.imgFetcher = new BusinessListIconTask(this);
    this.layoutInflator = LayoutInflater.from(this);
    this.businesses = businesses;
    if(adapter !=null){
        this.adapter.notifyDataSetChanged();
    }else{
        this.adapter= new BusinessListDataAdapter(this,
                this.imgFetcher, this.layoutInflator, this.businesses);
        businessList.setAdapter(adapter);

    }
}

private void getData() {
    // TODO Auto-generated method stub
    Intent myIntent = getIntent();

    // gets the arguments from previously created intent
    String metroTxt = myIntent.getStringExtra("key");
    String metroLoc = myIntent.getStringExtra("loc");
    String metroId = myIntent.getStringExtra("qt");

    BusinessListApiTask spTask = new BusinessListApiTask(
            ListResultActivity.this);

    try {
        spTask.execute(metroTxt, metroLoc, metroId);

    } catch (Exception e) {
        spTask.cancel(true);
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub
    if (businessList.getLastVisiblePosition() == totalItemCount - 1) {
        getData();          
        adapter.notifyDataSetChanged();
        Log.d("test count", "abc"+totalItemCount);
    }

}

}

从服务器获取数据并设置为适配器的类

public class BusinessListApiTask extends AsyncTask<String, Integer, String> {
private ProgressDialog progDialog;
private Context context;
private ListResultActivity activity;
private static final String debugTag = "sodhpuch";
HashMap<String, String> queryValues;

/**
 * Construct a task
 * 
 * @param activity
 */

public BusinessListApiTask(ListResultActivity activity) {
    // TODO Auto-generated constructor stub
    super();
    this.activity = activity;
    this.context = this.activity.getApplicationContext();
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progDialog = ProgressDialog.show(this.activity, "Search", this.context
            .getResources().getString(R.string.looking_for_business), true,
            false);
}

@Override
protected String doInBackground(String... params) {
    try {
        // Log.d(debugTag, "Background:" +
        // Thread.currentThread().getName());
        String result = BusinessListHelper.downloadFromServer(params);
        // try {
        //
        // updateSQLite(result);
        //
        // } catch (Exception e) {
        // return result;
        // }
        Log.d("result", result);
        return result;

    } catch (Exception e) {
        return new String();
    }
}

@Override
protected void onPostExecute(String result) {

    ArrayList<BusinessListData> businessData = new ArrayList<BusinessListData>();

    progDialog.dismiss();
    try {

        JSONObject respObj = new JSONObject(result);
        int success = respObj.getInt("success");
        Log.d("Success", "abc"+success);
        if (success == 1) {

            JSONArray tracks = respObj.getJSONArray("idioms");
            for (int i = 0; i < tracks.length(); i++) {
                JSONObject track = tracks.getJSONObject(i);
                String businessName = track.getString("name");
                String businessAddress = track.getString("address");
                String phone = track.getString("phone");
                String id = track.getString("id");
                String deals_in = track.getString("deals_in");
                businessData.add(new BusinessListData(businessName,
                        businessAddress, id, phone, deals_in));
            }   

        } else {

            Log.d("Success", "first"+success);
            // Log.d(debugTag, "Background:" + result);
            // DBController controller = new DBController(context);
            // businessData = controller.getBusinessList();
            return ;

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // }
    this.activity.setBusinesses(businessData);

}

我的适配器

public class BusinessListDataAdapter extends BaseAdapter implements
    OnClickListener {

private static final String debugTag = "BusinessListDataAdapter";
private ListResultActivity activity;
private BusinessListIconTask imgFetcher;
private LayoutInflater layoutInflater;
private ArrayList<BusinessListData> businesses;
BusinessListData business;

public BusinessListDataAdapter(ListResultActivity a,
        BusinessListIconTask i, LayoutInflater l,
        ArrayList<BusinessListData> data) {
    this.activity = a;
    this.imgFetcher = i;
    this.layoutInflater = l;
    this.businesses = data;

}


@Override
public int getCount() {
    return this.businesses.size();
}
public void clear()
{
    businesses.clear();
    notifyDataSetChanged();
}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

@Override
public Object getItem(int arg0) {
    return null;
}

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

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    MyViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.trackrow, parent,
                false);
        holder = new MyViewHolder();
        holder.businessName = (TextView) convertView
                .findViewById(R.id.tvBusinessName);
        holder.businessAddress = (TextView) convertView
                .findViewById(R.id.tvAddress);
        holder.phoneNo = (TextView) convertView.findViewById(R.id.tvPhone);
        holder.icon = (ImageView) convertView.findViewById(R.id.album_icon);
        holder.btnProfile = (Button) convertView
                .findViewById(R.id.btnProfile);
        holder.btnProfile.setTag(holder);
        convertView.setTag(holder);
    } else {
        holder = (MyViewHolder) convertView.getTag();
    }

    convertView.setOnClickListener(this);

    business= businesses.get(pos);
    holder.business = business;
    holder.businessName.setText(business.getName());
    holder.businessAddress.setText(business.getAddress());
    holder.phoneNo.setText(business.getPhone());
    holder.btnProfile.setOnClickListener(this);


    // if(track.getImageUrl() != null) {
    // holder.icon.setTag(track.getImageUrl());
    // Drawable dr = imgFetcher.loadImage(this, holder.icon);
    // if(dr != null) {
    // holder.icon.setImageDrawable(dr);
    // }
    // } else {
    holder.icon.setImageResource(R.drawable.filler_icon);
    // }

    return convertView;
}
@Override
public void onClick(View v) {
    String deals_in = business.getDeals().toString();
    Log.d("name", deals_in);
    MyViewHolder holder = (MyViewHolder) v.getTag();
    if (v instanceof Button) {

        Intent profile = new Intent(activity,
                ProfileActivity.class);
        profile.putExtra("deals_in", deals_in);
        profile.putExtra("phone", holder.business.getPhone());
        profile.putExtra("address", holder.business.getAddress());
        profile.putExtra("name", holder.business.getName());
        this.activity.startActivity(profile);

    } else if (v instanceof View) {
        Log.d("test","call testing");
        Intent intent = new Intent(Intent.ACTION_CALL);
           intent.setData(Uri.parse("tel:" +holder.business.getPhone()));
           this.activity.startActivity(intent);
    }
    Log.d(debugTag, "OnClick pressed.");

}

}

3 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public void setBusinesses(ArrayList<BusinessListData> businesses) {
    imgFetcher = new BusinessListIconTask(this);
    layoutInflator = LayoutInflater.from(this);
    if(this.businesses == null || adapter==null){
      this.businesses = new ArrayList<BusinessListData>();
      adapter= new BusinessListDataAdapter(this,imgFetcher,layoutInflator,this.businesses);
      businessList.setAdapter(adapter);
    }
    this.businesses.addAll(businesses);
    adapter.notifyDataSetChanged();
}

答案 1 :(得分:0)

您的setBusinesses方法中有适配器对象。您只需要检查适配器的大小,如下所示,这可以解决您的问题。

if(adapter !=null && adapter.getCount()>0)
{
this.adapter.notifyDataSetChanged();
}
else
{
   this.adapter= new BusinessListDataAdapter(this,
   this.imgFetcher, this.layoutInflator, this.businesses);
   businessList.setAdapter(adapter);

}

这将检查适配器中BusinessListData对象的大小,这不会反复初始化适配器。 希望这能解决你的问题。

谢谢!

答案 2 :(得分:0)

更改OnScrollListener的使用。使用Asynctask类和onPreExecute()将适配器设置为null。在doInBackground()方法中加载数据并在onPostExecute()中调用自定义适配器。我希望它能正常工作。