Saturday, 16 June 2012

Adapter Example in Android

Adapter Example in Android


DisplayTo.java

package com.success;

public class DisplayTo {
private int id;
private String name;
public DisplayTo() {
super();
}
public DisplayTo(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


OrderAdapter.java


package com.success;

import java.util.List;

import android.R.layout;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter {
private Activity activity;
private List<DisplayTo> items;

public OrderAdapter(Activity activity,
List<DisplayTo> items) {
// TODO Auto-generated constructor stubi
this.activity = activity;
this.items = items;
}

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

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return items.get(arg0);
}

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

@Override
public int getItemViewType(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
ViewHolder holder;
if(convertView == null )
{
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView1);
holder.id = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (position < items.size()) {
final DisplayTo producName = items.get(position);

holder.name.setText(""+producName.getId());
holder.id.setText(producName.getName());
}
return convertView;
}



private static class ViewHolder {
TextView name, id;
}
}

SuccessActivity.java

package com.success;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;

public class SuccessActivity extends Activity {
    /** Called when the activity is first created. */
private TableLayout tab1;
     private LinearLayout lv1;
//private LinearLayout lv1;
private LinearLayout lv2;
private ListView listv;
private static final String TAG = SuccessActivity.class
.getSimpleName();
private static List<DisplayTo> displaylist= new ArrayList<DisplayTo>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         lv1 = (LinearLayout)findViewById(R.id.linear1);
         lv2 = (LinearLayout)findViewById(R.id.linear2);
         tab1 = (TableLayout)findViewById(R.id.table1);
          listv = (ListView)findViewById(R.id.listView1);
       // listv.setAdapter(new OrderAdapter(this ,displaylist));
        
        Button button1 =(Button)findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);
       // ListView list = (ListView) findViewById(R.id.listView1);
        button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tab1.setVisibility(View.VISIBLE);
lv2.setVisibility(View.GONE);
}
});
        button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Log.i(TAG,"button2");
tab1.setVisibility(View.GONE);
lv2.setVisibility(View.VISIBLE);
for(DisplayTo displayTo:displaylist)
{
Log.i(TAG,displayTo.getName());
}
listv.setAdapter(new OrderAdapter(SuccessActivity.this,displaylist));
}
});
        button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et1 = (EditText)findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
int id = Integer.parseInt(et1.getText().toString());
String name = et2.getText().toString();
System.out.println("text in ed1" + id);
                System.out.println("text in ed2" + name);
                displaylist.add(new DisplayTo(id,name));
                
}
});
        
        
        
        
    }
}

Layouts

list_items.xml


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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
</LinearLayout>

Main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <LinearLayout 
        android:id="@+id/linear1"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">

           <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />
      
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />
        
    </LinearLayout>
    <TableLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/table1">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ID" />
            
            
            
            
            <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
        
            
            
            
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            
            <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />
            
            <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
            
                      
        </TableRow>
   
<TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
        
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="submit" />
        
   </TableRow>
    </TableLayout>
    <LinearLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/linear2" >
            <TextView
            android:id="@+id/android:empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hi" />
    
    <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    
     </LinearLayout>
</LinearLayout>

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...