简体   繁体   中英

How to add item to list view

Hi I'm new to Android i want to create application similar to app given in this link http://www.androiddom.com/2011/02/android-shopping-cart-tutorial.html?m=1 Only difference is that instead of list view in catalog activity I have image buttons.so wen I click any image button it goes to product details activity(same as example in link).Other activities are same as example in link.

Only thing I want is how to add item to list view by using button add to cart if there are image buttons in catalog activity.

Please do provide me help how to go about.

Main Layout File

<?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>

Activity File

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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