How to use adapter class inside button onClick listener?
I am new to android development.
I have problem with Popup Window.
Now i am required popup menu when i click the grideview Button than open
the popup to show the data from the arrayList store.
Thank you in advance
package com.example.skpl;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class DashBoardActivity extends Activity {
JSONObject json;
SoapPrimitive responce = null;
private static final String NAMESPACE="SKPLWebService";
private static final String METHOD_NAME = "GetDeshBoard";
private static final String SOAP_ACTION = "SKPLWebService/GetDeshBoard";
private static final String URL =
"http://192.168.1.13/SKPLWS/Service1.asmx";
private String TAG = "PGGURU";
public static ArrayList<String> table = new ArrayList<String>();
TextView tv ;
GridView gd;
Button b;
LinearLayout mainlayout;
LinearLayout layout;
LayoutParams params;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
tv = (TextView) findViewById(R.id.text1);
gd = (GridView) findViewById(R.id.gridView1);
mainlayout = new LinearLayout(this);
layout = new LinearLayout(this);
adapter = new MyAdapter(this,
android.R.layout.simple_list_item_1, table);
Call tast = new Call();
tast.execute();
}
private class Call extends AsyncTask<String, Void, Void>
{
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
deshboard();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Log.i(TAG, "onPostExecute");
gd.setAdapter(adapter);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Log.i(TAG, "onPreExecute");
tv.setText("Loading Data...");
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
Log.i(TAG, "onProgressUpdate");
}
}
private class MyAdapter extends ArrayAdapter<String>
{
private Context context;
private int layoutResourceId;
private ArrayList<String> data = new ArrayList<String>();
public MyAdapter(Context context, int layoutResourceId,
ArrayList<String> data) {
super(context, layoutResourceId, data);
this.context = context;
this.data = data;
this.layoutResourceId = layoutResourceId;
}
@Override
public View getView(final int position, View row, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row1 = inflater.inflate(R.layout.item_list,
parent,false);
b = (Button)row1.findViewById(R.id.Button);
b.setText(data.get(position));
b.setGravity(100);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<String> tabledata = new ArrayList<String>();
try {
String tablename = "Table"+position;
if(tablename.equals("Table0"))
{
tablename = "Table";
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("DisplayVoucherNumber");
String
str_data1=jsonnew.getString("ActualDate");
String str_data2=jsonnew.getString("Pcs");
String str_data3=jsonnew.getString("Weight");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
tabledata.add(str_data3);
}
Log.d("Table", tabledata.toString());
}
else if(tablename.equals("Table1"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("DisplayVoucherNumber");
String
str_data1=jsonnew.getString("ActualDate");
String
str_data2=jsonnew.getString("Difference");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
}
Log.d("Table1", tabledata.toString());
}
else if(tablename.equals("Table2"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String str_data=jsonnew.getString("Weight");
String
str_data1=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
}
Log.d("Table2", tabledata.toString());
}
else if(tablename.equals("Table3"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("DisplayVoucherNumber");
String
str_data1=jsonnew.getString("ActualDate");
String
str_data2=jsonnew.getString("NetWeight");
String
str_data3=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
tabledata.add(str_data3);
}
Log.d("Table3", tabledata.toString());
}
else if(tablename.equals("Table4"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("DisplayVoucherNumber");
String
str_data1=jsonnew.getString("ActualDate");
String
str_data2=jsonnew.getString("TagStatus");
String
str_data3=jsonnew.getString("NetWeight");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
tabledata.add(str_data3);
}
Log.d("Table4", tabledata.toString());
}
else if(tablename.equals("Table5"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("VoucherNo");
String
str_data1=jsonnew.getString("PacketNo");
String
str_data2=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
}
Log.d("Table5", tabledata.toString());
}
else if(tablename.equals("Table6"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("VoucherNo");
String
str_data1=jsonnew.getString("PacketNo");
String
str_data2=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
}
Log.d("Table6", tabledata.toString());
}
else if(tablename.equals("Table7"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("OrderVNO");
String
str_data1=jsonnew.getString("DifferenceWgt");
String
str_data2=jsonnew.getString("Justification");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
}
Log.d("Table7", tabledata.toString());
}
else if(tablename.equals("Table8"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String str_data=jsonnew.getString("Weight");
String
str_data1=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
}
Log.d("Table8", tabledata.toString());
}
else if(tablename.equals("Table9"))
{
json=new JSONObject(responce.toString());
JSONArray jsonarray =
json.getJSONArray(tablename);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject
jsonnew=jsonarray.getJSONObject(i);
String
str_data=jsonnew.getString("DisplayVoucherNo");
String
str_data1=jsonnew.getString("ActualDate");
String
str_data2=jsonnew.getString("FromDeptName");
String
str_data3=jsonnew.getString("ToDeptName");
String
str_data4=jsonnew.getString("CatName");
tabledata.add(str_data);
tabledata.add(str_data1);
tabledata.add(str_data2);
tabledata.add(str_data3);
tabledata.add(str_data4);
}
Log.d("Table9", tabledata.toString());
}
else
{
Log.e("Error", "No Data Found");
}
if(tabledata.size() !=0)
{
//Here require to popup window with tabledata
}
else
{
Toast.makeText(getApplicationContext(), "Sorry
No data Available !!!",
Toast.LENGTH_LONG).show();
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return row1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
}
private class MyAdapter1 extends ArrayAdapter<String>
{
private Context context;
private int layoutResourceId;
private ArrayList<String> data = new ArrayList<String>();
TextView tv;
public MyAdapter1(Context context, int layoutResourceId,
ArrayList<String> data) {
super(context, layoutResourceId, data);
this.context = context;
this.data = data;
this.layoutResourceId = layoutResourceId;
}
@Override
public View getView(final int position, View row, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row1 = inflater.inflate(R.layout.popup_item, parent,false);
tv = (TextView) row1.findViewById(R.id.gd1);
tv.setText(data.get(position));
return row1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
}
public void deshboard()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
aht.debug = true;
try
{
aht.call(SOAP_ACTION, envelope);
responce = (SoapPrimitive) envelope.getResponse();
Log.e("Responce", responce.toString());
}
catch(Exception ex)
{
String err = (ex.getMessage()==null)?"ExceptionError Means No
Value Return":ex.getMessage();
Log.e("ExceptionError:",err);
}
try{
json=new JSONObject(responce.toString());
JSONArray jsonarray = json.getJSONArray("Table10");
for(int i=0;i<jsonarray.length();i++)
{
JSONObject jsonnew=jsonarray.getJSONObject(i);
String str_data=jsonnew.getString("TableName");
String str_data1=jsonnew.getString("Notification");
table.add(str_data+"\\n["+str_data1+"]");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
No comments:
Post a Comment