设置listview只有一行差异

时间:2012-11-26 02:07:44

标签: android android-layout android-listview baseadapter

我试图仅使用imageview使我的listview只有一行。这是我的适配器:

public class ListAdapter extends BaseAdapter {

private Context activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;

public ListAdapter(Context a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return data.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listview, null);
    TextView subject = (TextView)vi.findViewById(R.id.subject);
    TextView added = (TextView)vi.findViewById(R.id.added); 
    TextView permalink = (TextView)vi.findViewById(R.id.permalink);

    HashMap<String, String> map = new HashMap<String, String>();
    map = data.get(position);

     //Setting all values in listview
    subject.setText(map.get(ViewPagerAdapter.KEY_NEWS_SUBJECT));
    added.setText(map.get(ViewPagerAdapter.KEY_NEWS_LAPSE));
    permalink.setText(map.get(ViewPagerAdapter.KEY_NEWS_PERMA));
    return vi;
}

}

当前版面的文本视图很少,但唯一的一行可以说我在4日修复它以仅显示imageview。我该怎么办?

1 个答案:

答案 0 :(得分:0)

首先在自定义布局中添加ImageView  在public View getView(int position, View convertView, ViewGroup parent)方法内添加以下内容:

   ImageView image = (ImageView)vi.findViewById(R.id.imageview);
    if( position == 0){ // here I suppose you want it display on the first row
         image.setVisibility(View.Visible);
    }
    else{
        image.setVisibility(View.GONE);
    }

附注:我建议您在使用getView();

时使用ViewHolder