如何实现onclick意图在自定义列表视图中显示值?

时间:2013-12-07 12:03:15

标签: android listview

我从androidhive找到了这个自定义列表视图教程,我试图实现onclick,意图显示自定义列表视图中的值。每当我点击这些项目时,应用程序就崩溃了。请帮忙!

CustomListView.Java

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "http://myurl/Announcement.xml";
    // XML node keys
    static final String KEY_ANNOUNCEMENT = "announcement"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_INFORMATION = "information";
    static final String KEY_DATE = "date";
    static final String KEY_ICON = "icon";

    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        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_ANNOUNCEMENT);
        // 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);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_INFORMATION, parser.getValue(e, KEY_INFORMATION));
            map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
            map.put(KEY_ICON, parser.getValue(e, KEY_ICON));

            // adding HashList to ArrayList
            songsList.add(map);
        }


        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
                String information = ((TextView) view.findViewById(R.id.information)).getText().toString();
                String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
                String tag = view.getTag().toString();

                // Starting new intent
                Intent intent = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                intent.putExtra("title",KEY_TITLE);
                intent.putExtra("information",KEY_INFORMATION);
                intent.putExtra("date",KEY_DATE);
                startActivity(intent);

            }
        });     
    }   

LazyAdapter.Java

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    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); // title
        TextView date = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView icon =(ImageView)vi.findViewById(R.id.list_image); // thumb image


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

        vi.setTag(song.get(CustomizedListView.KEY_ID));

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));
        date.setText(song.get(CustomizedListView.KEY_DATE));
        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_ICON), icon);
        return vi;
    }
}

SingleMenuItemActivity.Java

public class SingleMenuItemActivity  extends Activity {

    // XML node keys
    static final String KEY_TITLE = "title";
    static final String KEY_INFORMATION = "information";
    static final String KEY_DATE = "date";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_menu_item);

        // getting intent data
        Intent in = getIntent();

        // Get XML values from previous intent
        String title = getIntent().getExtras().getString("title");
        String information = getIntent().getExtras().getString("information");
        String date = getIntent().getExtras().getString("date");

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.title);
        TextView lblCost = (TextView) findViewById(R.id.information);
        TextView lblDesc = (TextView) findViewById(R.id.date);

        lblName.setText(title);
        lblCost.setText(information);
        lblDesc.setText(date);
    }
}

logcat的

12-07 20:01:36.575: W/dalvikvm(1167): threadid=1: thread exiting with uncaught exception (group=0x4188f898)
12-07 20:01:36.575: E/AndroidRuntime(1167): FATAL EXCEPTION: main
12-07 20:01:36.575: E/AndroidRuntime(1167): java.lang.NullPointerException
12-07 20:01:36.575: E/AndroidRuntime(1167):     at com.example.androidhive.CustomizedListView$1.onItemClick(CustomizedListView.java:79)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.widget.AdapterView.performItemClick(AdapterView.java:301)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.widget.AbsListView.performItemClick(AbsListView.java:1510)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3333)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.os.Handler.handleCallback(Handler.java:730)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.os.Looper.loop(Looper.java:137)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at android.app.ActivityThread.main(ActivityThread.java:5419)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invoke(Method.java:525)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-07 20:01:36.575: E/AndroidRuntime(1167):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案