1

我的脚本不工作。我的目标是制作一个包含一些可点击项目的列表视图。但应用程序不断崩溃。如何解决这个问题?我是否正确使用了适配器?

我使用标题、副标题和图像作为列表视图中的项目。

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);   
    }
}

日志文件: http: //pastebin.com/kzeVMLuk

4

2 回答 2

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修饰符。对于与上下文相关的任何内容,我建议不要使用static修饰符,特别是因为它绑定到一个活动。

于 2013-02-26T22:34:43.607 回答
0

在您使用.put并插入字符串之后。要使它们可点击,请尝试使用onItemClickListener. 我会帮你的。

于 2013-04-17T10:11:47.383 回答