带有ArrayList的Android Custom ListView如何打开第二个活动

时间:2014-11-01 00:09:27

标签: java android listview android-intent arraylist

我有这个自定义ListView与ArrayList,我试图打开第二个活动使用 使用的图标位于文件夹drawable

setOnItemClickListener(new OnItemClickListener() {}

但是当我点击ListItem时它强制关闭应用程序 CbMain.java

package com.frostistudios.circuitbasicspro;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CbMain extends Activity {

String[] listItems = {"Menu One","Menu Two","Menu Three"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cb_main);



    ListView lv = (ListView) findViewById(R.id.listView); //listview in xml file
    List<ListViewItem> items = new ArrayList<CbMain.ListViewItem>();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            if(position == 0){
                //call to custom method for opening activity
                openItemOneActivity();
            }
            else if(position == 1){
                openItemTwoActivity();
            }
            else if(position == 2){
                openItemThreeActivity();
            }

        }
    });


    items.add(new ListViewItem()
    {{
        ThumbnailResource = R.drawable.ic_action_call;
        Title = listItems[0];
        SubTitle = "";


    }});
        items.add(new ListViewItem()
    {{
        ThumbnailResource = R.drawable.ic_action_call;
        Title = listItems[1];
        SubTitle = "";


    }});
        items.add(new ListViewItem()
    {{
        ThumbnailResource = R.drawable.ic_action_call;
        Title = listItems[2];
        SubTitle = "";


     }});

        CustomListViewAdapter adapter = new CustomListViewAdapter(this, items);
        lv.setAdapter(adapter);
}

  //custom methods to open another activity
public void openItemOneActivity(){
    Intent intent = new Intent(this,ItemOneActivity.class);
    startActivity(intent);
} 
public void openItemTwoActivity(){
    Intent intent = new Intent(this,ItemTwoActivity.class);
    startActivity(intent);
} 
public void openItemThreeActivity(){
    Intent intent = new Intent(this,ItemThreeActivity.class);
    startActivity(intent);
} 

class ListViewItem {
    public int ThumbnailResource;
    public String Title;
    public String SubTitle;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    //getMenuInflater().inflate(R.menu.actionbar, menu);
    //return super.onCreateOptionsMenu(menu);
    return true;


}


}

CustomListViewAdapter.java 这是自定义列表适配器文件

package com.frostistudios.circuitbasicspro;

import java.util.List;
import com.frostistudios.circuitbasicspro.CbMain.ListViewItem;
import android.app.Activity;  
import android.content.Context;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;
import android.widget.TextView;  

public class CustomListViewAdapter extends BaseAdapter
{  

LayoutInflater inflater;
List<ListViewItem> items;

public CustomListViewAdapter(Activity context, List<ListViewItem> items) {  
    super();

    this.items = items;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return items.size();  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return null;  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  

    ListViewItem item = items.get(position);

    View vi=convertView;

    if(convertView==null)
        vi = inflater.inflate(R.layout.item_row, null);

        ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
        TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
        TextView txtSubtitle = (TextView) vi.findViewById(R.id.txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubtitle.setText(item.SubTitle);


    return vi;  
}
}

这是包含ListView设计的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgThumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_action_call" 
    android:layout_marginLeft="10dp"
    android:layout_marginTop="2dp"/>

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Large Text"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="6dp"/>

<TextView
    android:id="@+id/txtSubTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:src="@drawable/arrow" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

如果您发布了错误输出,那将会有很大帮助。您的清单中是否已声明其他活动?您已在适配器中有上下文,因此您可以在那里设置并尝试。我也认为View vi是必要的,你可以使用convertView。

答案 1 :(得分:0)

从您的日志cat中查看,错误似乎是因为您使用的theme不是来自compat。检查您的manifest,并确保使用compat主题。

示例(适用于您应用中的所有活动):

<application
        //other code
        android:theme="@style/Theme.AppCompat.Light" >

或者,如果您仅对特定活动使用操作栏:

<Activity
        //other code
        android:theme="@style/Theme.AppCompat.Light" >
提示:

始终关注log cat,尤其是caused by。我从那里知道问题。