ListView仅显示文本而非图像

时间:2016-07-08 22:02:04

标签: android listview android-studio android-arrayadapter

我试图创建一个listView,在文本下显示文本和图像。我创建了自己的自定义适配器,因此我可以执行这样的任务。但是,当我创建我的应用程序时,它只显示一个textView并完全跳过imageView。我该怎么做才能显示图像?我假设我的自定义适配器类可能有问题,但我不确定它可能是什么,因为我相信其他一切都应该是正确的。

请注意我使用Picasso加载和显示图片

我的MainActivity AsyncTask类

private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }

    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);



        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.

            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);
            List<Bean> myList = new ArrayList<>();
            list = (ListView)findViewById(R.id.orderListView);

            for(int i = 0; i < descriptionArray.length;i++) {
                Bean bean = new Bean();
                bean.setText(descriptionArray[i]);
                bean.setUrl(photoArray[i]);
                myList.add(bean);
                MyAdapter adapter = new MyAdapter(OrdersActivity.this, myList);
                list.setAdapter(adapter);
            }

        } else {
            findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
        }
    }

}

我的自定义阵列适配器

public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;

public MyAdapter(Context context,List<Bean> list){
    mContext=context;
    mList=list;
}

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

@Override
public Object getItem(int position) {
    return mList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    //use converview recycle
    if(convertView==null){
        holder=new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
        holder.textView= (TextView) convertView.findViewById(R.id.textView2);
        holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    //set text and url
    holder.textView.setText(mList.get(position).getText());
    Picasso.with(mContext).load(mList.get(position).getUrl()).into(holder.imageView);

    return convertView;
}

class ViewHolder{
    TextView textView;
    ImageView imageView;

}
}

我的GetterAndSetterClass(Bean)

public class Bean {
String text;
String url;

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
}

我的内容XML文件由RelativeLayout

包围
<TextView
    android:id="@+id/nullOrders"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/no_orders_message"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:visibility="gone" />


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"/>

包含listView的我的活动XML文件

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/orderListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:dividerHeight="2dp"/>

0 个答案:

没有答案