简体   繁体   中英

Array Adapter doesn't call GetView

I have

public class MainActivity extends FragmentActivity

I wrote Loader class which implements AsyncResponse:

public class Loader implements AsyncResponse {
public Activity contextActivity;

Loader(Activity context) {
    this.contextActivity = context;
}

Class "Loader" has method "DrawTabInfo" which calling from AsyncResponse callback.

public void drawTabInfo(String jsonBlob, int tabId)
{
    JSONObject dataJsonObj;
    PullToRefreshListView list = null;

    try {
        dataJsonObj = new JSONObject(jsonBlob);
        JSONArray jsonData = dataJsonObj.getJSONArray("steals");

        ArrayList<JSONObject> data = new ArrayList<JSONObject>();
        for (int i=0;i<jsonData.length();i++){
            data.add(jsonData.getJSONObject(i));

        }
        Adapter adapter = new Adapter(contextActivity, data);

        LayoutInflater inflater = LayoutInflater.from(contextActivity);

        if (tabId == 0) {
            View view = inflater.inflate(R.layout.listviewlayout, null, false);
            list = (PullToRefreshListView) view.findViewById(R.id.listView);
        }
        if (tabId == 1) {
            View view = inflater.inflate(R.layout.listviewlayout2, null, false);
            list = (PullToRefreshListView) view.findViewById(R.id.listView2);
        }

        list.setAdapter(adapter);
        adapter.setNotifyOnChange(true);

Log.d("COUNT") shows - "4", what means array is built fine.

public class Adapter extends ArrayAdapter<JSONObject> {
    private final Activity context;
    private final ArrayList<JSONObject> profiles;
    public String header;
    public String preText;
    public String imageURL;

    static class ViewHolder {
        public TextView header;
        public ImageView image;
        public TextView preText;
        public LinearLayout linearItemLayout;
    }

    public Adapter(Activity context, ArrayList<JSONObject> array) {
        super(context, R.layout.tab1_list_layout, array);
        Log.d("ADAPTER", "SETTING ADAPTER");
        this.context = context;
        this.profiles = array;
        Log.d("COUNT", "" + profiles.size());
    }

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

My getView method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    Log.i("GetView Log", "Adapters GetView hasbeen called");// thats never calling

    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.tab1_list_layout, null);

        ViewHolder viewHolder = new ViewHolder();
        viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
        viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
        viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
        viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    JSONObject item = null;

    try {
        item = profiles.get(position);
        header = item.getString("header");
        preText = item.getString("previewText");
        imageURL = item.getString("previewImageUrl");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d("ADAPTER LOG", header);

    holder.header.setText(header);
    holder.preText.setText(preText);

    int headerHeight = (int) Math.round(header.length() * 1.5);
    holder.linearItemLayout.setMinimumHeight(40 + headerHeight + preText.length());

    Picasso.with(context).setIndicatorsEnabled(true);
    Picasso.with(context).load(imageURL).resize(140,100).into(holder.image);

    return rowView;
}

In MainActivity i calling, where "this" = MainActivity;

    Loader loader = new Loader(this);
    loader.loadTabInfo(0);

Method getView never calling. What I'am doing wrong?

UPD:

Solved:

public void drawTabInfo(String jsonBlob, int tabId)
{
    JSONObject dataJsonObj;
    PullToRefreshListView list = null;

    try {
        dataJsonObj = new JSONObject(jsonBlob);
        JSONArray jsonData = dataJsonObj.getJSONArray("steals");

        ArrayList<JSONObject> data = new ArrayList<JSONObject>();
        for (int i=0;i<jsonData.length();i++){
            data.add(jsonData.getJSONObject(i));

        }
        Adapter adapter = new Adapter(contextActivity, data);

        //LayoutInflater inflater = LayoutInflater.from(contextActivity);

        if (tabId == 0) {
            //View view = inflater.inflate(R.layout.listviewlayout, null, false);
            list = (PullToRefreshListView) contextActivity.findViewById(R.id.listView);

Your code is fine .. just you need a little bit change in get view method add a return statement.. like return rowView;

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    Log.d("GETVIEaW", "GETVIEW");

        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.tab1_list_layout, null);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
            viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
            viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
            rowView.setTag(viewHolder);
            }
        else{
        viewHolder = (ViewHolder) rowView.getTag();
        }

        viewHolder.preText.setText("Type your name");

        return rowView;

    }

Are you sure that Log.d() is not called? You may want to change logging scope, usually default scope is info and it doesn't show debug messages. You can try to use Log.i() or change scope.

Try posting your adapter.setNotifyOnChange(true); to handler. Follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM