-1

My Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mediator_id= getIntent().getStringExtra("mediator_id");
    mediator_name= getIntent().getStringExtra("mediator_name");
    new Thread(transaction).start();
    setContentView(R.layout.transaction_log);

    ImageView back=(ImageView) findViewById(R.id.imageView1);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(Log.this,Mediator.class));

        }
    });
}

public Runnable transaction = new Runnable() {
    public void run() {

        try {
            if (mediator_id.equals("") || mediator_name.equals("")) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("1", "Improper");
                        // TODO Auto-generated method stub
                    }
                });
            } else {
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();
                HttpPost post = new HttpPost(Configure.APP_BASE_URI_Mediator_TRANSACTIONS);
                post.addHeader("Content-Type", "application/json");
                json.put("no", mediator_id);

                post.setEntity(new StringEntity(json.toString()));

                response = client.execute(post);

                final int status = response.getStatusLine().getStatusCode();
                if (status != HttpStatus.SC_OK) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            Toast.makeText(Log.this,
                                    "Unable to connectggggggggg. Please try again.",
                                    Toast.LENGTH_LONG).show();

                        }
                    });

                } else {
                    InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    String a = convertStreamToString(in);
                    JSONObject jObject = new JSONObject(a);
                    int result = jObject.getInt("status");
                    System.out.print("result+++++++++" + result);
                    Log.d("Result", jObject.getString("message"));
                    final String transObj = jObject.getString("message");

                    if (result == 1) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Intent intent = new Intent(Log.this, Mediator.class);
                                intent.putExtra("mediator_id", mediator_id);
                                intent.putExtra("mediator_name", mediator_name);
                                startActivity(intent);
                                Toast.makeText(Log.this, " Login Successful" + transObj, Toast.LENGTH_LONG).show();
                            }
                        });

                        Intent intent = new Intent(TransactionLog.this, TransactionLog.class);
                        startActivity(intent);
                    } else if (result == 0) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Intent intent = new Intent(TransactionLog.this, Operator_Transactions.class);
                                intent.putExtra("mediator_id", mediator_id);
                                intent.putExtra("mediator_name", mediator_name);
                                startActivity(intent);
                                // TODO Auto-generated method stub
                                Toast.makeText(Log.this, "login details are wrong", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }
            }
        } catch (final Exception e) {
            //                Toast.makeText( Log.this, "login details are wrong"+e.toString(),Toast.LENGTH_LONG).show();
            Log.d("error", "+++++++" + e.toString());
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Toast.makeText(Log.this,
                            "Unable to connect. Please try again.************** " + e.toString(),
                            Toast.LENGTH_LONG).show();

                }
            });
        }
    }
};

I've got a JSON response like this:

D/Result(3928): {"19":{"sender_id":"1210","m":"200","mediator":"121030910807","reciever":"508505","id":"21","sender_no":"508505","remarks":"NULL","sender_no":"12","type":"books","date_time":"2014-08-22 19:45:06"},"22":{"sender_id":"121030910805","m":"50.0","mediator":"121030910807","reciever":"508505","id":"24","sender_no":"508505","remarks":"NULL","sender_no":"121030910805","type":"books","date_time":"2014-08-23 11:22:17"},"17":

How can I send this data to ListView?

My custom ListView:

<?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"
    android:orientation="vertical" 
    android:id="@+id/relMain"
    android:background="@drawable/roundedcorner1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="5dp"
        android:text="DateTime                        :" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Type: " />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text=" Number            : " />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Amount:" />

    <TextView
        android:id="@+id/transaction_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_marginLeft="39dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="15-08-2014/12:30:00" />

    <TextView
        android:id="@+id/transaction_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/transaction_date"
        android:layout_below="@+id/transaction_date"
        android:text="Withdrawal" />

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/transaction_type"
        android:text="234561843901" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/number"
        android:text="500.00" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="450dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="14dp"
        android:src="@drawable/line1" />

</RelativeLayout>

Code for transaction Adapter:

public class TransactionLog_Adapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;

    ArrayList<Item> data = new ArrayList<Item>();

    public TransactionLog_Adapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        Item myImage = data.get(position);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(parent.getLayoutParams());

        for (int j = 0; j < myImage.getName().length; j++) {

            row = inflater.inflate(layoutResourceId, parent, false);

            RelativeLayout linearMain = (RelativeLayout) row
                    .findViewById(R.id.relMain);
            TextView transaction_date = (TextView) row.findViewById(R.id.transaction_date);
            //transaction_date.setText(myImage.name[j]);

            TextView transaction_type = (TextView) row.findViewById(R.id.transaction_type);
            //transaction_type.setText(myImage.name[j]);

            TextView aadhar_number = (TextView) row.findViewById(R.id.number);
            //aadhar_number.setText(myImage.name[j]);

            TextView amount = (TextView) row.findViewById(R.id.amount);
            //amount.setText(myImage.name[j]);
        }
        return row;
    }

Can you please tell me how can I send the JSON response to the ListView

gnuanu
  • 2,252
  • 3
  • 29
  • 42
anonymous
  • 321
  • 4
  • 16
  • Google [JSON Parsing in Android](https://www.google.co.in/?gfe_rd=cr&ei=osD6U4GZNJSAoAOklIKQBg&gws_rd=ssl#q=json%20parsing%20in%20android%20tutorial). Basically you need to create a DAO class. In your case you are passing arraylist of Item, so create a class called Item and put getters and setters for the details in your JSON response. Parse the response, store that data in object of Item class, and add those objects to your arraylist. Finally pass that arraylist to adapter of your listview and override `getView` – Nitish Aug 25 '14 at 04:56

1 Answers1

0

Try http://jsongen.byingtondesign.com/ link. It will parse JSON to create client side source files to model the JSON data structure. Then use Gson to parse the response thats simpler

Gson integration tutorial http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

Deniz
  • 12,332
  • 10
  • 44
  • 62