我的脚本无效。我的目标是使用一些可点击的项目制作列表视图。但应用程序不断崩溃。如何解决这个问题?我是否正确使用适配器?
我使用标题,副标题和图片作为列表视图中的项目。
package com.example.whs;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MenuAdapter extends BaseAdapter{
// Define variables
ArrayList<HashMap<String, String>> data;
Activity activity;
private static LayoutInflater inflater=null;
public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
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); // title
TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // subtitle
HashMap<String, String> item = new HashMap<String, String>();
item = data.get(position);
// Setting all values in listview
title.setText(item.get(Index.TITLE));
subtitle.setText(item.get(Index.SUBTITLE));
return vi;
}
}
这是我的索引类:
package com.example.whs;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class Index extends Activity {
public static final Object TITLE = "title";
public static final Object SUBTITLE = "subtitle";
public static final Object THUMBNAIL = "thumbnail";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
buildMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.index, menu);
return true;
}
//Builds the menu for listview
public void buildMenu(){
ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
//Arrays for info
String[] menuTitleArray = {"Gallerij"};
String[] menuSubtitleArray = {"Bekijk foto's en geef reacties"};
String[] menuThumbnailArray = {"gallery"};
for(int i=0; i < menuTitleArray.length; i++){
HashMap<String, String> item = new HashMap<String, String>();
item.put((String) TITLE, menuTitleArray[i]);
item.put((String) SUBTITLE, menuSubtitleArray[i]);
item.put((String) THUMBNAIL, menuThumbnailArray[i]);
menu.add(item);
}
MenuAdapter adapter = new MenuAdapter(this, menu);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
答案 0 :(得分:1)
一个问题:
private static LayoutInflater inflater=null,
它永远不会在您的代码中实例化:
在你的构造函数中执行:
private LayoutInflater inflater=null;
public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = LayoutInflater.from (a);
}
如您所见,我还取出了static
修饰符。对于与Contexts相关的任何内容,我建议不要使用static
修饰符,特别是因为它绑定到Activity。
答案 1 :(得分:0)
使用.put
并插入字符串后。要使它们可以点击,请尝试使用onItemClickListener
。它会有所帮助。