2014-07-05 117 views
-3

这是我的代码:自定义网格视图

public class CustomGridAdapter extends BaseAdapter{ 

Context ctx; 
String [] items ={"72","58","67","77","90"} ; //Can be images or video or any other content 
String [] stuff = {"bus to town ","Bus to north","Bus to west","Bus to east","Bus out"}; 
public CustomGridAdapter(Context c) { 
    ctx = c; 
} 

@Override 
public int getCount() { 
    return items.length; 
} 

@Override 
public Object getItem(int position) { 
    return items[position]; 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    TextView tv = new TextView(ctx); 
    TextView tv2 = new TextView(ctx); 
    tv.setText(String.valueOf(items[position])); 
    tv2.setText(String.valueOf(stuff[position])); 
    return tv; 
} 

}

需要帮助试图找出如何显示在下面的电视TV2。 有什么建议吗?

+0

你为什么不创建一个布局XML和膨胀的方法getView(....)这些XML? – TeRRo

回答

0

你需要创建一个垂直的LinearLayout:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LinearLayout layout = new LinearLayout(ctx); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
      LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    TextView tv = new TextView(ctx); 
    TextView tv2 = new TextView(ctx); 
    tv.setLayoutParams(lparams); 
    tv2.setLayoutParams(lparams); 
    tv.setText(String.valueOf(items[position])); 
    tv2.setText(String.valueOf(stuff[position])); 
    layout.addView(tv); 
    layout.addView(tv2); 
    return layout; 
} 
+0

LinearLayout layout = new LinearLayout(this);这不工作任何建议如何使它工作? – user3691808

+0

构造函数LinearLayout(CustomGridAdapter)未定义thtas它说什么 – user3691808

+0

我修正了错字,将其替换为ctx – Yoavst