Array Adapter不会调用GetView

时间:2015-05-05 04:02:32

标签: java android arrays

我有

public class MainActivity extends FragmentActivity

我编写了实现AsyncResponse的Loader类:

public class Loader implements AsyncResponse {
public Activity contextActivity;

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

Class" Loader"有方法" DrawTabInfo"从AsyncResponse回调调用。

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(&#34; COUNT&#34;)显示 - &#34; 4&#34;,表示数组构建正常。

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();
    }

我的getView方法:

@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;
}

在MainActivity中我打电话,在哪里&#34;这个&#34; = MainActivity;

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

方法getView从不调用。我做错了什么?

UPD:

解决:

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);

3 个答案:

答案 0 :(得分:0)

您的代码很好..只需要在get view方法中稍作修改 添加一个返回语句..如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;

    }

答案 1 :(得分:0)

您确定没有调用Log.d()吗?您可能希望更改日志记录范围,通常默认范围为info,并且它不显示调试消息。您可以尝试使用Log.i()或更改范围。

答案 2 :(得分:0)

尝试将adapter.setNotifyOnChange(true);发布到处理程序。请遵循此link