Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

php - How to send dynamic data from one activity to another in android

I am developing an android application where I am getting data from mysql database and displaying that in Listview. I have data "Categories" like Telecom, Banking, Social Sites and so on. This Category getting from mysql, is displaying in ListView perfectly. Now when I click on any one of them, then this should move to next activity and display data according to that category. Suppose I click on Social Sites, then on next activity, the data should be Google Plus, Instagram, Twitter etc and this list will come from Database and display in listview. I am not getting any idea about this how to do that..!!! Please help me...This is my code what I've done.

Welcome.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Welcome extends Activity 
{
    TextView text;
    //Button b1,b2,b3;
    String CompName,CompID;

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

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

      //  b1=(Button)findViewById(R.id.dashboard_tab1);
      //  b1.setOnClickListener(this);

        connect();
    }

    public void connect() 
    {
        String data;
        List<String> r = new ArrayList<String>();
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
        ListView list=(ListView)findViewById(R.id.list);
        try 
        {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet("http://10.0.2.2/database/Retrive.php");
                HttpResponse response = client.execute(request);
                HttpEntity entity=response.getEntity();
                data=EntityUtils.toString(entity);
                Log.e("STRING", data);

                try 
                {
                   JSONArray json=new JSONArray(data);
                   for(int i=0;i<json.length(); i++)
                   {
                        JSONObject obj=json.getJSONObject(i);

                        CompName=obj.getString("fldCompName");
                        CompID=obj.getString("fldCompID");

                        Log.e("STRING", CompName);
                        Log.e("STRING",CompID);

                        r.add(CompName);
                        //r.add(CompID);
                        list.setAdapter(adapter);

                        list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
                        {
                            @Override
                            public void onItemClick(AdapterView<?> parent,View view, int position, long id) 
                            {
                                Intent i1=new Intent(Welcome.this,Category1.class);
                                i1.putExtra("CompName", CompName);
                                startActivity(i1);
                            }
                        });
                   }
                } 
                catch (JSONException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (ClientProtocolException e) {
                Log.d("HTTPCLIENT", e.getLocalizedMessage());
            } catch (IOException e) {
                Log.d("HTTPCLIENT", e.getLocalizedMessage());
            }
    }

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

This is my layout file...!!!

activity_welcome.xml

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

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg" >
</ListView>

</LinearLayout>

Retrive.php

<?php

    $host='localhost';
    $uname='root';
    $pwd='';
    $db='database';

    $con=mysqli_connect($host,$uname,$pwd) or die("Connection Failed");
        mysqli_select_db($con,$db) or die("database selection failed");

    $sql=mysqli_query($con,"select fldCompName,fldCompID from tblCompanies where fldStatus='1'");
    while($row=mysqli_fetch_assoc($sql))
    $output[]=$row;
    $a=json_encode($output);
    print($a);
    mysqli_close();

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Replace

 i1.putExtra("CompName", CompName);

by

 i1.putExtra("CompName", r[position]);

Then get this data in the next activity and fetch the data from DB depending on the passed value.

You have to pass the value which user has selected. So all your values are in your adapter r and you will get to know which one user has selected by item selected position, which is third parameter of onItemClick. So it should be r[position]. It's better to completely fill the adapter before setting it to ListView. So fetch all the data from JSON, add it to adapter and then set that adapter to ListView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...