0

Im trying to use intent to get information from the textview in my listview and display it on another activity. I have tried searching for answers but i cant seem to find the problem .

Menuactivity :

public class MenuActivity extends Activity {


private String server = "http://172.16.156.56";
private String sql_table = "orders";
private ListView list;
private TextView txtOrder, txtMember, txtPrice;
private Button btnAdd;
ArrayList<String> rows;

ListView listView1;
Button btnSubmit;
ArrayList<CustomItem> itemList, selectedList;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    txtOrder = (TextView) findViewById(R.id.txt1);
    txtMember = (TextView) findViewById(R.id.txt2);
    txtPrice = (TextView) findViewById(R.id.txt3);



    list=(ListView) findViewById(R.id.listView);
    btnAdd = (Button)findViewById(R.id.button);
    rows = new ArrayList<String>();



    itemList=new ArrayList<CustomItem>();
    itemList.add(new CustomItem("Fried Rice","description","1","Quantity"));
    itemList.add(new CustomItem("Fried Noodle","description","2","Quantity"));
    itemList.add(new CustomItem("Prawn noodle","description","3","Quantity"));
    itemList.add(new CustomItem("Chicken Rice","description","4","Quantity"));

    int[] prgmImages={R.drawable.friedrice,R.drawable.friednoodle,R.drawable.pnoodle,R.drawable.chickenrice};

    listView1 = (ListView) findViewById(R.id.listView);
    final CustomLVAdapter customLVAdapter = new CustomLVAdapter(this, itemList,prgmImages);
    listView1.setAdapter(customLVAdapter);
    btnSubmit = (Button) findViewById(R.id.button);


    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String strOrder = txtOrder.getText().toString();
            String strMember = txtMember.getText().toString();
            String strPrice = txtPrice.getText().toString();


            StringBuffer responseText = new StringBuffer();
            selectedList = new ArrayList<CustomItem>();
            int total = 0;
            for (int i = 0; i < itemList.size(); i++) {
                CustomItem object = itemList.get(i);
                if (object.isSelected()) {
                    responseText.append(object.getItem() + "," + object.getQty() + ",");//item
                    selectedList.add(object);
                    //calculate price
                    total = total + Integer.parseInt(object.getQty()) * Integer.parseInt(object.getPrice());

                }


            }
            Add(responseText.toString(), "5565", String.valueOf(total));


            Intent i = new Intent(getApplicationContext(), ReceiptActivity.class);

            i.putExtra("Order",strOrder);
            i.putExtra("Member",strMember);
            i.putExtra("Price",strPrice);

            startActivity(i);

            Toast.makeText(getApplicationContext(), responseText + " $" + total,
                    Toast.LENGTH_SHORT).show();


            SelectAll();
            //store in database

            //go to ReceiptActivity - membership, item, totalprice

        }
    });


}





public void Add(final String item, final String membership, final String price)
{
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    String url = server + "/insertorder.php";
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("item",item );
            MyData.put("membership",membership );
            MyData.put("price",price );

            return MyData;
        }
    };
    MyRequestQueue.add(MyStringRequest);
    SelectAll();
}
public void SelectAll()
{
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    String url = server + "/fetchorder.php";
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray jsonMainNode = jsonResponse.optJSONArray(sql_table);
                rows.clear();
                for(int i=0; i < jsonMainNode.length(); i++)
                {
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                    String order       = jsonChildNode.optString("item").toString();
                    String membership     = jsonChildNode.optString("membership").toString();
                    String price = jsonChildNode.optString("price").toString();

                    rows.add(order + ", " + membership + ", " + price );
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MenuActivity.this,
                        android.R.layout.simple_list_item_1, rows);
                list.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {
            //This code is executed if there is an error.
        }
    });
    MyRequestQueue.add(MyStringRequest);
}

}

recieptactivity (the results should be displayed here)

public class ReceiptActivity extends Activity {
static public String txtOrder = "";
TextView foodorder;
ArrayList<CustomItem> itemList, selectedList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receipt);

    Bundle extras = getIntent().getExtras();
    String strOrder = extras.getString("Order");
    String strMember = extras.getString("Member");
    String strPrice = extras.getString("Price");

    TextView foodorder = (TextView)findViewById(R.id.foodorder);
    foodorder.setText("hey"+strOrder);

menu.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MenuActivity">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit Order"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

listview.xml :

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


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1"
    android:id="@+id/txt1"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    style="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView2"
    android:id="@+id/txt2"
    android:textStyle="bold"
    android:layout_below="@+id/txt1"
    android:layout_alignStart="@+id/txt1" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView3"
    android:id="@+id/txt3"
    android:textStyle="bold"
    android:layout_below="@+id/txt2"
    android:layout_alignStart="@+id/txt2" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ckBox"
    android:checked="false"
    android:layout_below="@+id/txt3"
    android:layout_alignStart="@+id/txt3"
    />

<ImageView

    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"

    android:scaleType="fitXY"
    android:gravity="left"
    android:layout_alignBottom="@+id/ckBox"
    android:layout_toStartOf="@+id/txt1"
    android:layout_alignParentEnd="false"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/numberPicker"
    android:layout_toEndOf="@+id/txt2"
    android:layout_marginStart="23dp"
    android:layout_alignParentTop="true"
    android:layout_alignBottom="@+id/ckBox" />

customadapter :

public class CustomLVAdapter extends BaseAdapter {

private Context context;
LayoutInflater inflater;
private ArrayList<CustomItem> objectList;
private int[] imageId;
public static ArrayList<CustomItem> arl_food=new ArrayList<>();

private class ViewHolder {
    TextView txt1,txt2,txt3;
    CheckBox ckBox;
    ImageView image;

    NumberPicker np;
}




public CustomLVAdapter(Context context, ArrayList<CustomItem> objectList,int[]prgmImages){
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    this.objectList = objectList;
    this.imageId = prgmImages;

    this.objectList=objectList;
    arl_food.clear();
    for(int i=0;i<this.objectList.size();i++)
    {
        arl_food.add(this.objectList.get(i));
    }
}

public int getCount(){
    return objectList.size();
}

public CustomItem getItem (int position) {
    return objectList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;


    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row_checkbox_textview, null);
        holder.txt1 = (TextView) convertView.findViewById(R.id.txt1);
        holder.txt2 = (TextView) convertView.findViewById(R.id.txt2);
        holder.txt3 = (TextView) convertView.findViewById(R.id.txt3);
        holder.image=(ImageView) convertView.findViewById(R.id.image);
        holder.ckBox = (CheckBox) convertView.findViewById(R.id.ckBox);
        holder.np  = (NumberPicker) convertView.findViewById(R.id.numberPicker);
        holder.np.setMinValue(0);
        holder.np.setMaxValue(10);






        convertView.setTag(holder);

        holder.ckBox.setOnClickListener(new View.OnClickListener() {
                                            public void onClick(View v) {
                                                CheckBox cb = (CheckBox) v;
                                                CustomItem object = (CustomItem) cb.getTag();
                                                Toast.makeText(context,
                                                        "You have selected:  " + object.getItem() +
                                                                "Price: " + object.getPrice() +
                                                                "Qty: " + object.getQty(),
                                                        Toast.LENGTH_LONG).show();
                                                object.setSelected(cb.isChecked());
                                                arl_food.get(position).setSelected(cb.isChecked());
                                            }
                                        }
        );

        holder.np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPicker p = picker;
                CustomItem object = (CustomItem) p.getTag();
                object.setQty(String.valueOf(newVal));
                arl_food.get(position).setQty(String.valueOf(newVal));
            }
        });
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    CustomItem object = objectList.get(position);
    holder.txt1.setText(object.getItem());
    holder.txt2.setText(object.getDesc());
    holder.txt3.setText(object.getPrice());

    holder.np.setTag(object);
    holder.image.setImageResource(imageId[position]);
    holder.ckBox.setChecked(object.isSelected());
    holder.ckBox.setTag(object);

    return convertView;
}

}

customitem :

public class CustomItem {
private  int id;
private  String item;
private  String price;
private  String desc;
private String qty;
private  Boolean selected = false;

public CustomItem(){

}


public CustomItem(String item,String desc, String price,String qty){
    this.item=item;
    this.desc=desc ;
    this.price=price;
    this.qty=qty;
}

public CustomItem(int id, String item,String desc, String price,String qty){
    this.id=id;
    this.item=item;
    this.desc=desc;
    this.price=price;
    this.qty=qty;
}

public int getId(){
    return id;
}

public void setId (int id) {
    this.id=id;
}

public String getItem(){
    return item;
}

public void setItem(String item){
    this.item=item;
}


public String getDesc()
{
    return desc;
}


public void setDesc(String desc) {
    this.desc=desc;
}



public String getPrice()
{
    return price;
}


public void setPrice(String price) {
    this.price=price;
}


public String getQty()
{
    return qty;
}


public void setQty(String qty) {
    this.qty=qty;
}





public boolean isSelected(){
    return selected;
}

public void setSelected(boolean selected){
    this.selected=selected;
}

logcat :

06-30 15:25:59.969 7196-7196/com.example.android.cardemulation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.android.cardemulation, PID: 7196
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
                                                                                 at com.example.android.cardemulation.MenuActivity$1.onClick(MenuActivity.java:79)
                                                                                 at android.view.View.performClick(View.java:5226)
                                                                                 at android.view.View$PerformClick.run(View.java:21266)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:168)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
himanshu1496
  • 1,881
  • 17
  • 33
goh
  • 5
  • 8

1 Answers1

-1

You can not use this in to your Activity.

txtOrder = (TextView) findViewById(R.id.txt1);
txtMember = (TextView) findViewById(R.id.txt2);
txtPrice = (TextView) findViewById(R.id.txt3);

You should use another approach to get selected item data. You can get from itemList or selectedList and can process further.

Ashish Vora
  • 571
  • 1
  • 8
  • 26