Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning Android 4 Application Development
|
BOOK: Beginning Android 4 Application Development
This is the forum to discuss the Wrox book Beginning Android 4 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-9954-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 21st, 2013, 03:15 AM
Registered User
 
Join Date: Mar 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help... to create an application using Custom adapter

Hiiii,

I want to create an app which contains EditText & "Add" button...when user type country name in EditText n after clicking add button , country names should be added in ListView.

my problem is whenever i run app. country names are override in ListView, i refer this book but not found any solutions...Please help me.

My Code: Main XML file
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity"
    android:background="#000000" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:text="Country Names"
        android:textColor="#ffffff"
        />
        
    <EditText 
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        
        android:background="#ffffff"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="20dp"
        android:layout_below="@id/tv1"
        android:layout_centerHorizontal="true"
        />
   
    <Button 
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD"
        android:onClick="next"
        android:layout_below="@id/et1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"/>
</RelativeLayout>
another XML file for custom adapter
Code:
<?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" >
    

    <!-- <ListView 
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        /> -->
    <TextView 
        android:id="@+id/tview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>
    
    <Button 
        android:id="@+id/bb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:onClick="back"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
        
</RelativeLayout>
Main Activity java file
Code:
package com.example.intent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	EditText e1;
	Intent i;
	String str;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		e1 = (EditText)findViewById(R.id.et1);
		
		Button bb = (Button)findViewById(R.id.b);
		
	}

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

	public void next(View v)
	{
		str= e1.getText().toString();
		
		i = new Intent(this,Next.class);
		i.putExtra("country",str);
		startActivity(i);
	}
	
}
second activity
Code:
package com.example.intent;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;


import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

import android.widget.TextView;
import android.widget.Toast;

public class Next extends ListActivity{

	Intent i ;
	
	ArrayList<String> aa = null;
	private Custom_Adapter adap = null;
	String country=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.nnext);

		//ListView l = (ListView)findViewById(R.id.lv);
		
		country= getIntent().getStringExtra("country");
		
		aa = new ArrayList<String>();
		aa.add(country);
		adap = new Custom_Adapter(this,R.layout.nnext,aa);
		if(aa!=null && aa.size()>0)
		{
			adap.notifyDataSetChanged();
			//adap.add(aa.get(0));
		}
		adap.notifyDataSetChanged();
		setListAdapter(this.adap);
		
		
		//adap.notifyDataSetChanged();
		//String[] array=aa.toArray(new String [aa.size()]);
		
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
		
		
		//adapter.add(country);
		//adapter.notifyDataSetChanged();
		//l.setAdapter(adap);
		
	}
	
	public void back(View v)
	{
		i =new Intent(getApplicationContext(),MainActivity.class);
		startActivity(i);
	}
	
	
	
	
}
custom adapter file:
Code:
package com.example.intent;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Custom_Adapter extends ArrayAdapter<String> {
	
	private Context appcontext = null;
	private ArrayList<String> items = null;
	TextView view;
	
	public Custom_Adapter(Context context ,int id, ArrayList<String> items) {
		super(context, id,items);
		this.appcontext = context;
		this.items= items;
		
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		if(v==null)
		{
			LayoutInflater li= (LayoutInflater)appcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v =li.inflate(R.layout.nnext,null);
			
			TextView view = (TextView)v.findViewById(R.id.tview);
			view.setText(items.get(position));
		}
		
		return v;
		
		//return super.getView(position, convertView, parent);
	}

}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Custom ScrollBar dartcoach VB How-To 8 November 13th, 2006 09:30 PM
Help with my custom adapter SinStereo Biztalk 1 March 23rd, 2006 06:17 AM
Create an About property for custom control mouratx General .NET 1 March 27th, 2004 11:18 PM
How to create a custom window shape lotfi VS.NET 2002/2003 1 November 17th, 2003 08:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.