0

I want to implement this code by adding button click that send row data to another activity. Here needed implementation in Spinner and CheckBox, so I pass that value only who is checked and I want to pass quantity also.

Here is my list.java file that have a data that come from web-services.

package com.example.hello;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class List extends ActionBarActivity implements OnClickListener  {
    ListView listview;
    Model[] modelItems;
    private SQLiteDatabase db;
    public void onCreate(Bundle cv) {
        super.onCreate(cv);
        setContentView(R.layout.listv);
        listview = (ListView) findViewById(R.id.listView1);


        /*Button b22=(Button)findViewById(R.id.b52);
        Button b11=(Button)findViewById(R.id.b51);
        b11.setOnClickListener(this);
        b22.setOnClickListener(this);*/
        String response = null;
        try {
                response = CustomHttpClient.executeHttpGet("http://legacysofttech.com/menulist.php/");

                JSONArray jArray = new JSONArray(response);
                modelItems = new Model[jArray.length()];
                for (int i = 0; i < jArray.length(); i++)
             {
                     JSONObject json_data = jArray.getJSONObject(i);
                     modelItems[i] = new Model(json_data.getString("Item"),json_data.getString("price"), 0,1);
                    // item_list.add(json_data.getString("Item"));
                     //price_list.add(json_data.getString("price")); 
             }

                CustomAdapter adapter = new CustomAdapter(this, modelItems);    
                listview.setAdapter(adapter);                   


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "your internet working to slow", Toast.LENGTH_SHORT).show();
                }
    }

    public void onClick(View v)
    {
        switch(v.getId())
        {

        case R.id.b52:
        {

        }
        case R.id.b51:
        {

        }
        }
        }   

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

here it is my customadapter.java file

package com.example.hello;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;


public class CustomAdapter extends ArrayAdapter<Model>

{
             Model[] modelItems = null;
             Context context;
             public CustomAdapter(Context context, Model[] resource) {
             super(context,R.layout.listv,resource);
             // TODO Auto-generated constructor stub
             this.context = context;
             this.modelItems = resource;

 }

             @Override
 public View getView(int position, View convertView, ViewGroup parent) {
                 // TODO Auto-generated method stub
                 LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                 convertView = inflater.inflate(R.layout.listm, parent, false); 
                 TextView name = (TextView) convertView.findViewById(R.id.textView1);
                 TextView price = (TextView) convertView.findViewById(R.id.textView2);
                 CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
                 Spinner spinner=(Spinner) convertView.findViewById(R.id.spinner1);

                 name.setText(modelItems[position].getName());
                 price.setText(modelItems[position].getPrice());
                 spinner.setId(modelItems[position].getQuantity());


                 if(modelItems[position].getValue() == 1)
                 cb.setChecked(true);
                 else
                 cb.setChecked(false);
                 return convertView;
                 }           
}

Here is my Model.java file

package com.example.hello;
public class Model{
 String name;
 String price;
 int Quantity;
 int value;  /* 0 -&gt; checkbox disable, 1 -&gt; checkbox enable */

 Model(String name, String price,int value,int Quantity){
 this.name = name;
 this.value = value;
 this.price = price;
 this.Quantity = Quantity;

 }
 public String getName(){
 return this.name;
 }
 public int getValue(){
 return this.value;
 }
 public int getQuantity(){
     return this.Quantity;
 }
 public String getPrice(){
 return this.price;
 }
}

listv.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginBottom="50dp">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
         >
    </ListView>

</RelativeLayout>

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">


<Button
                android:id="@+id/b51"
                android:layout_gravity="bottom"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_marginRight="3dp"
                android:layout_marginTop="1dp"
                android:text="@string/CONFIRM"
                android:layout_weight="1"
                android:background="@drawable/button_default_bg"
                 />             

<Button
                android:id="@+id/b52"
                android:layout_gravity="bottom"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_marginRight="3dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="@drawable/button_default_bg"
                android:text="@string/cancel" />                

</LinearLayout>
</RelativeLayout>

listm.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

        <CheckBox
                 android:id="@+id/checkBox1"
                 android:layout_width="50dp"
                 android:layout_height="wrap_content"
                 android:text="" />

        <TextView
                 android:id="@+id/textView1"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="TextView" />
        <TextView
                 android:id="@+id/textView2"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="TextView"
                 android:layout_weight="1"
                 />
        <Spinner
            android:id="@+id/spinner1"
            style="@style/AppBaseTheme"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:entries="@array/Quantity"
             />
</LinearLayout>

I want to send checked item with quantity,price and item-name to another activity by using SQLite database.

can you please help me to develop onClick class and another activity file.

Thank you.

2 Answers 2

3

I have change your source code

Model.java

    package com.example.hello;
    public class Model{
     String name;
     String price;
     int Quantity;
     int value;  /* 0 -&gt; checkbox disable, 1 -&gt; checkbox enable */

     Model(String name, String price,int value,int Quantity){
     this.name = name;
     this.value = value;
     this.price = price;
     this.Quantity = Quantity;

     }
     public String getName(){
     return this.name;
     }
     public int getValue(){
     return this.value;
     }
     public int setValue(int value){
     return this.value=value;
     }
     public int getQuantity(){
         return this.Quantity;
     }
     public String getPrice(){
     return this.price;
     }
    }

Code for list.java

package com.example.hello;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class List extends ActionBarActivity implements OnClickListener  {
    ListView listview;
    Model[] modelItems;
    private SQLiteDatabase db;
    public void onCreate(Bundle cv) {
        super.onCreate(cv);
        setContentView(R.layout.listv);
        listview = (ListView) findViewById(R.id.listView1);


        /*Button b22=(Button)findViewById(R.id.b52);
        Button b11=(Button)findViewById(R.id.b51);
        b11.setOnClickListener(this);
        b22.setOnClickListener(this);*/
        String response = null;
        try {
                response = CustomHttpClient.executeHttpGet("http://legacysofttech.com/menulist.php/");

                JSONArray jArray = new JSONArray(response);
                modelItems = new Model[jArray.length()];
                for (int i = 0; i < jArray.length(); i++)
             {
                     JSONObject json_data = jArray.getJSONObject(i);
                     modelItems[i] = new Model(json_data.getString("Item"),json_data.getString("price"), 0,1);
                    // item_list.add(json_data.getString("Item"));
                     //price_list.add(json_data.getString("price")); 
             }

                CustomAdapter adapter = new CustomAdapter(this);    
                listview.setAdapter(adapter);                   


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "your internet working to slow", Toast.LENGTH_SHORT).show();
                }
    }

    public void onClick(View v)
    {
        switch(v.getId())
        {

        case R.id.b52:
        {
  for (int i = 0; i < modelItems.length(); i++)
             {
    if(modelItems[i].getValue()==1)
    {
                  Toast t = Toast.makeText(this,
                                modelItems[i].getName(), Toast.LENGTH_LONG);
   t.setGravity(Gravity.CENTER, 0, 0);
                        t.show();
    }
        }
        }
        case R.id.b51:
        {

        }
        }
        }   

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


public class CustomAdapter extends ArrayAdapter<Model>

{

             Context context;
             public CustomAdapter(Context context) {
             super(context,R.layout.listv,resource);
             // TODO Auto-generated constructor stub
             this.context = context;

 }

             @Override
 public View getView(int position, View convertView, ViewGroup parent) {
                 // TODO Auto-generated method stub
                 LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                 convertView = inflater.inflate(R.layout.listm, parent, false); 
                 TextView name = (TextView) convertView.findViewById(R.id.textView1);
                 TextView price = (TextView) convertView.findViewById(R.id.textView2);
                 CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
                 Spinner spinner=(Spinner) convertView.findViewById(R.id.spinner1);

                 name.setText(modelItems[position].getName());
                 price.setText(modelItems[position].getPrice());
                 spinner.setId(modelItems[position].getQuantity());


                 if(modelItems[position].getValue() == 1)
                 cb.setChecked(true);
                 else
                 cb.setChecked(false);
                 return convertView;



      cb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CheckBox cb1 = (CheckBox) v;

                if (cb1.isChecked()) {

                  modelItems[position].setValue(1)
                } else {

                    modelItems[position].setValue(0)
                }
                notifyDataSetChanged();

            }
        });
                 }           
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are couple of ways you can achieve this, based on your programming style you can choose!

1-

A very robust way is to use content providers and try to pass URIs between different intents by using Intent.putData(URI) and retrieve it by Intent.getData(). with the URI use an implemented content provider to load the data on the other activity. This method needs some re-coding but is more reliable

2-

Let your Model class implement Parcelable Interface and then pass data with Intent.putExtra("tag", Parcelable) and retrieve on the other intent with Intent.getExtra("tag") .

3-

Just simply pass the integer or string data with Intent.putExtra("quantity", quantity), Intent.putExtra("price", price) and retrieve it in the other application with Intent.getExtra("price") and Intent.getExtra("quantity"). This is simpler but also less flexible. I implement this method for better understanding for you:

//I suppose a variable called "index" is used to hold the index of data you want to send from your Model[] array

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("quantity", modelItems[index].getPrice());
intent.putExtra("price", modelItems[index].getQuantity());
startActivity(intent);

6 Comments

i want checked item store to database so can you please give me code that handle the onclick event for button and how to inherit the position from oncreate class to onclick class
you should use content providers and redesign your code
i don't really know about it because i am learning this by my-own, can you help me for that . it is my project work for final year bachelor engineering
can you help me for handle the onclick event for button and how to inherit the position from oncreate class to onclick class
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.