在新活动中加载listView

时间:2016-04-29 12:27:29

标签: android listview

我在将内容加载到新的listView时遇到了一些麻烦。如果我使用setContentView,它会显示一个空列表,如果我尝试更改我的Intent应用程序崩溃。请问我做错了什么。主要的Activity类是:

ListView lv;
Context context;

private int [] prgmImages={R.drawable.phones_and_tablets,R.drawable.home_and_kitchen_appliances,
        R.drawable.beauty_health_and_personal_care,R.drawable.fashion,R.drawable.subscriptions,
        R.drawable.electronics,R.drawable.windows_logo,R.drawable.music_and_movies,
        R.drawable.baby_kids_and_toys,R.drawable.sports,R.drawable.books_and_stationery,
        R.drawable.fastfood_and_eatery};
private String [] prgmNameList={"Phones & Tablets","Home & Kitchen","Beauty, Health & Personal Care",
        "Fashion","Subscriptions","Electronics","Computer & Accessories","Music & Movies","Baby, Kids & Toys",
        "Sports & Fitness","Books & Stationery","Fast Food, Drinks & Groceries"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this; // Instantiate context after listView's properties are set.
    lv=(ListView) findViewById(R.id.listView);
    lv.setAdapter(new MainCustomAdapter(this, this.prgmNameList, this.prgmImages));
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch( position ){
                case 0:  { setContentView(R.layout.activity_fone_cats); break; }
                case 1:  { setContentView(R.layout.activity_home_kit_cats); break; }
                case 2:  { setContentView(R.layout.activity_beauty_care_cats); break; }
                case 3:  { setContentView(R.layout.activity_fashion_cats); break; }
                case 4:  { setContentView(R.layout.activity_telecoms_cats); break; }
                case 5:  { setContentView(R.layout.activity_electronics_cats); break; }
                case 6:  { setContentView(R.layout.activity_computers_cats); break; }
                case 7:  { setContentView(R.layout.activity_entertainment_cats); break; }
                case 8:  { setContentView(R.layout.activity_toys_cats); break; }
                case 9:  { setContentView(R.layout.activity_sports_cats); break; }
                case 10:  { setContentView(R.layout.activity_stationeries_cats); break; }
                case 11:  { setContentView(R.layout.activity_f_food_drinks_grocery_cats); break; }
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

我的主要xml布局结构如下:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="25dp"
    android:textStyle="bold"
    android:textColor="@color/colorBlack"
    android:text="@string/action_companyName" />

<ImageView
    android:id="@+id/imageView"
    android:layout_gravity="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/textView_1"
    android:layout_gravity="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/action_motto"
    android:textSize="18sp"
    android:textStyle="italic|bold"/>

<TextView
    android:id="@+id/textView_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/action_query"
    android:textColor="#3a94d5" />

<SearchView
    android:id="@+id/searchView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:queryHint="@string/action_query2"
    android:contentDescription="@string/action_query2" />

<ImageView
    android:id="@+id/imageView_1"
    android:layout_width="match_parent"
    android:layout_height="100sp"
    android:scaleType="fitXY"
    android:src="@drawable/ad" />

<TextView
    android:id="@+id/textView_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="."
    android:textColor="#FFFFFF" />

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

我构建了以下自定义适配器:

String [] category; // The category a sales-item belongs to
Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public MainCustomAdapter(MainActivity currentActivity, String[] prgmNameList, int[] prgmImages) {
    context = currentActivity;
    category = prgmNameList;
    imageId = prgmImages;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return category.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder{ // Set an object to hold category items for each position in the list
    TextView txt;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder=new Holder();
    View rowView = inflater.inflate(R.layout.programlist, null);
    holder.txt=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.txt.setText(category[position]); // Hold the name of the selected category
    holder.img.setImageResource(imageId[position]); // Hold the icon of the selected category
    return rowView;
}

程序列表布局构建如下:

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24dp"
    android:text="TextView" />

因此我建立了activity_fone_cats:

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="25dp"
    android:textStyle="bold"
    android:textColor="@color/colorBlack"
    android:text="@string/action_companyName" />

<ImageView
    android:id="@+id/imageView"
    android:layout_gravity="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/textView_1"
    android:layout_gravity="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/action_motto"
    android:textSize="18sp"
    android:textStyle="italic|bold"/>

<TextView
    android:id="@+id/textView_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/action_query"
    android:textColor="#3a94d5" />

<SearchView
    android:id="@+id/searchView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:queryHint="@string/action_query2"
    android:contentDescription="@string/action_query2" />

<ImageView
    android:id="@+id/imageView_1"
    android:layout_width="match_parent"
    android:layout_height="100sp"
    android:scaleType="fitXY"
    android:src="@drawable/ad" />

<TextView
    android:id="@+id/textView_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="."
    android:textColor="#FFFFFF" />

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

请问如何确保在主要活动中单击listItem时显示列表中的项目?如果我想使用Intent更改而不是切换contentViews,如何阻止应用程序崩溃?

2 个答案:

答案 0 :(得分:0)

 lv=(ListView) findViewById(R.id.listView);

        lv.setAdapter(new MainCustomAdapter(this, prgmNameList, prgmImages));

   lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) setContentView(R.layout.activity_fone_cats);
                else if (position == 1) {
                    startActivityForResult(new Intent(view.getContext(),HomeKitchenCatsActivity.class), 0);
                    // setContentView(R.layout.activity_home_kit_cats);
                }
                else if (position == 2) setContentView(R.layout.activity_beauty_care_cats);
                else if (position == 3) setContentView(R.layout.activity_fashion_cats);
                else if (position == 4) setContentView(R.layout.activity_telecoms_cats);
                else if (position == 5) setContentView(R.layout.activity_electronics_cats);
                else if (position == 6) setContentView(R.layout.activity_computers_cats);
                else if (position == 7) setContentView(R.layout.activity_entertainment_cats);
                else if (position == 8) setContentView(R.layout.activity_toys_cats);
                else if (position == 9) setContentView(R.layout.activity_sports_cats);
                else if (position == 10) setContentView(R.layout.activity_stationeries_cats);
                else if (position == 11) setContentView(R.layout.activity_f_food_drinks_grocery_cats);
            }
            });

答案 1 :(得分:0)

@aburimegregory是的,你现在得到了答案。 OnItem单击您想要将新布局与您的活动绑定。

        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new MainCustomAdapter(this, this.prgmNameList, this.prgmImages));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch( position ){
                    case 0:  { setContentView(R.layout.activity_fone_cats); break; }
                    case 1:  { setContentView(R.layout.activity_home_kit_cats); break; }
                    case 2:  { setContentView(R.layout.activity_beauty_care_cats); break; }
                    case 3:  { setContentView(R.layout.activity_fashion_cats); break; }
                    case 4:  { setContentView(R.layout.activity_telecoms_cats); break; }
                    case 5:  { setContentView(R.layout.activity_electronics_cats); break; }
                    case 6:  { setContentView(R.layout.activity_computers_cats); break; }
                    case 7:  { setContentView(R.layout.activity_entertainment_cats); break; }
                    case 8:  { setContentView(R.layout.activity_toys_cats); break; }
                    case 9:  { setContentView(R.layout.activity_sports_cats); break; }
                    case 10:  { setContentView(R.layout.activity_stationeries_cats); break; }
                    case 11:  { setContentView(R.layout.activity_f_food_drinks_grocery_cats); break; }
                }

               lv=(ListView) findViewById(R.id.listView);
               lv.setAdapter(new MainCustomAdapter(this, this.prgmNameList, this.prgmImages));
            }
        });

绑定新布局后,您需要找到listview并设置适配器以加载新ContentView中的项目列表。

你需要为新布局中的所有其他项做同样的事情。(在绑定新布局后查找视图并设置值)

注意:我不确定您的项目需求。您接近列表视图的方式可能不合适。花更多时间学习listview及其功能,然后开始实现。一切顺利。