Android导入正确的Item&类ItemView控件

时间:2014-09-16 06:53:47

标签: java android listview android-arrayadapter

我想在我的应用程序中使用Customizing Android ListView Rows by Subclassing但是在这种方式进行修改后,我导致错误导入Item&的正确类ItemView我的应用程序支持旧版本的android,我正在使用android.support任何类。

通知导入Itementer image description here

通知导入ItemViewenter image description here

为inport引入的类不正确,我收到错误,例如setItem

如何解决此问题并可以导入正确的类?

ItemAdapter类:

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import ir.tsms.wsdl.ReceiveFields;

public class ItemAdapter extends ArrayAdapter<ReceiveFields> {

    public ItemAdapter(Context c, List<ReceiveFields> items) {
        super(c, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemView itemView = (ItemView)convertView;
        if (null == itemView)
            itemView = ItemView.inflate(parent);
        itemView.setItem(getItem(position));
        return itemView;
    }

}

片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ArrayList<ReceiveFields> items = new ArrayList<ReceiveFields>();
    for (int i = 0; i < 100; i++) {
        String url = String.format("http://www.google.com/image/%d.png", i);
        String title = String.format("Item %d", i);
        String description = String.format("Description of Item %d", i);
        Item item = new Item(url, title, description);
        items.add(item);
    }

    setListAdapter(new ItemAdapter(getActivity(), items));

    return super.onCreateView(inflater, container, savedInstanceState);
}

2 个答案:

答案 0 :(得分:0)

正如Mike M所说,您应该定义Item和ItemView类,而不是尝试导入它们。因此,这是来自您的链接,您应该有两个类。 ItemView看起来像:

public class ItemView extends RelativeLayout {
    private TextView mTitleTextView;
    private TextView mDescriptionTextView;
    private ImageView mImageView;

    public ItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true);
        setupChildren();
    }

    public static ItemView inflate(ViewGroup parent) {
         ItemView itemView = (ItemView)LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_view, parent, false);
         return itemView
    }

    private void setupChildren() {
         mTitleTextView = (TextView) findViewById(R.id.item_titleTextView);
         mDescriptionTextView = (TextView) findViewById(R.id.item_descriptionTextView);
         mImageView = (ImageView) findViewById(R.id.item_imageView);
    }

    public void setItem(Item item) {
         mTitleTextView.setText(item.getTitle());
         mDescriptionTextView.setText(item.getDescription());
         // TODO: set up image URL
    }
}

然后你还需要一个Item类并设置你的布局文件。我希望这有帮助。

答案 1 :(得分:0)

本教程的示例代码在github上。以下是我使用的关键类:

https://github.com/bignerdranch/android-listview-custom-view/tree/master/ListItemViewDemo/src/com/bignerdranch/android/listitemviewdemo

“Item”和“ItemView”只是我选择的名字;您可能希望使用更具体的应用程序域名。

很高兴你喜欢这种模式!