繁体   English   中英

如何将项目添加到列表视图

[英]How to add item to list view

嗨,我是 Android 新手,我想创建类似于此链接中给出的应用程序http://www.androiddom.com/2011/02/android-shopping-cart-tutorial.html?m=1唯一的区别是而不是目录活动中的列表视图,我有图像按钮。所以我点击任何图像按钮,它会转到产品详细信息活动(与链接中的示例相同)。其他活动与链接中的示例相同。

如果目录活动中有图像按钮,我唯一想要的是如何通过使用按钮添加到购物车将项目添加到列表视图。

请为我提供帮助。

主布局文件

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

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

活动档案

package com.example.android;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleListViewActivity extends Activity {

private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Find the ListView resource. 
mainListView = (ListView) findViewById( R.id.mainListView );

// Create and populate a List of context names.
String[] context = new String[] { "context1", "context2", "context3", "context4","context5"};  
ArrayList<String> context = new ArrayList<String>();
contextList.addAll( Arrays.asList(context) );

// Create ArrayAdapter using the context list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, contextList);

// Add more contexts. If you passed a String[] instead of a List<String> 
// into the ArrayAdapter constructor, you must not add more items. 
// Otherwise an exception will occur.
listAdapter.add( "context8" );
listAdapter.add( "context9" );
listAdapter.add( "context10" );

// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );      
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM