我必须在 android 应用程序的文本和列表视图中创建自定义网格视图。
在这里,我必须在 gridview 上显示类别名称,如果类别名称匹配,这意味着必须显示列表显示在相应类别名称下方的列表视图上。这里我参考了以下教程:我的参考教程
如果类别名称是 notchbolly,我已在 gridview.here 上显示类别名称,这意味着 gridview 列表项显示在 notchbolly 类别名称上方的水平列表视图上。
但是在这里我必须运行该应用程序意味着 else 部分仅在所有类别名称上执行。我的代码中有什么错误。请给我这些解决方案...
我使用了以下代码:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
ListAdapter adapter;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return 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.list_row, null);
TextView title = (TextView) vi.findViewById(R.id.title);
ListView imageView = (ListView) vi
.findViewById(R.id.grid_item_image);
HashMap<String, String> item = new HashMap<String, String>();
item = data.get(position);
// Setting all values in listview
title.setText(item.get(MainActivity.KEY_TITLE));
if (item.equals("notchbolly")) {
listview.setAdapter(this);
} else {
listview.setBackgroundResource(R.drawable.rihanna);
}
return vi;
}
MainActivity.java:
public class MainActivity extends Activity {
static final String URL = "http://webservices/new_feed_articls.xml";
static String KEY_CATEGORY = "Categories";
static final String KEY_TITLE = "Category";
static final String KEY_TITLENAME = "name";
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TITLE);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String> ();
Element e = (Element) nl.item(i);
map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute(KEY_TITLENAME));
// map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
songsList.add(map);
}
GridView list = (GridView) findViewById(R.id.listView1);
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
请给我这些的解决方案。我的代码有什么问题????
编辑:
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.notch.HorizontalListView
android:id="@+id/grid_item_image"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TextView>
</LinearLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<GridView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
/>
如果类别名称是 notchbolly,我已在 gridview.here 上显示类别名称,这意味着 gridview 列表项显示在 notchbolly 类别名称上方的水平列表视图上。
但是在这里我必须运行该应用程序意味着 else 部分仅在所有类别名称上执行。我的代码中有什么错误。请给我这些解决方案...
这里 else 部分只执行了......