I am developing an android app which functions like as follows
Admin creates the data and saves it into database then user access the same data through app when it comes down to showing the data to user i fetch the data from the database which admin has already saved i show the data in listview, and listview having buttons in each row when user click on button it dose not goes to next activity.When user will click on button in listview the activity should pass to next activity.How do i do this?
//java activity
public class MainActivity extends ActionBarActivity {
String myJSON;
SimpleAdapter adapter;
Button btn;
private static final String TAG_RESULTS = "result";
private static final String TAG_NAME = "sname";
private static final String TAG_PRICE = "sprice";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
// ArrayList<HashMap<String, String>> personList = new ArrayList<HashMap<String, String>>();
ListView list, listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView_search);
btn = (Button) findViewById(R.id.button3_book);
personList = new ArrayList<HashMap<String, String>>();
getData();
// Listview on item click listener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent zoom=new Intent(parent.getContext(), Details.class);
parent.getContext().startActivity(zoom);
}
});
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String sname = c.getString(TAG_NAME);
String sprice = c.getString(TAG_PRICE);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,sname);
persons.put(TAG_PRICE,sprice);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.search_item,
new String[]{TAG_NAME,TAG_PRICE},
new int[]{ R.id.textView8_sellernm, R.id.textView19_bprice}
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(MainActivity.this,Details.class);
startActivity(i);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://example.com/User/reg/listview.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
Related
iam using menu slidding, this code for call other class
}else if (id == R.id.list) {
//Set the fragment initially
StockProduct fragment = new StockProduct();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
but fragment error i don't know why...
and this is my class code
public class StockProduct extends Fragment implements ListView.OnItemClickListener {
String myJSON;
private static final String TAG_RESULTS = "result";
private static final String TAG_NAME = "name_product";
private static final String TAG_ID = "product_id";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_product, container, false);
list = (ListView) view.findViewById(R.id.allproduct);
personList = new ArrayList<HashMap<String, String>>();
getData();
return view;
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(Config.ALLPRODUCT_URL);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
}
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID, id);
persons.put(TAG_NAME, name);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
StockProduct.this.getActivity(), personList, R.layout.list_product_component,
new String[]{TAG_ID,TAG_NAME},
new int[]{R.id.idproduct, R.id.nameproduct}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this.getActivity(), StockProductView.class);
HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
String product_id = map.get(TAG_ID).toString();
intent.putExtra(Config.TAG_PRODUCT_ID,product_id);
startActivity(intent);
}}
and this is log error
Try replacing this line,
StockProduct fragment = new StockProduct();
with
Fragment fragment = new StockProduct();
Make sure all your import statements import support fragment
I have an update fragment using which i will update data in my database. Once the update is done i move back to 'view the data from database' fragment and i need the list to be updated. I think i have to write code for this inside onResume() but i don't know how to do it.
Code for 'view the data from database' fragment:
public class Testing extends Fragment {
String uname="921312104053";
private static final String TAG_RESULTS="result";
private static final String TAG_Sname = "Subject_name";
private static final String TAG_Scode = "Subject_code";
private static final String TAG_Sgrade ="Grade";
ListView lv;
JSONArray subjects = null;
String myJSON;
ListAdapter adapter;
ArrayList<HashMap<String, String>> subList,subList1;
public Testing() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_testing,
container, false);
disp(view);
return view;
}
public void disp(View view)
{
lv = (ListView)view.findViewById(R.id.list);
subList = new ArrayList<HashMap<String,String>>();
getData(view);
}
public void getData(View view){
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://10.0.2.2/markolepsy/android_connect/testing.php?usrname="+uname);
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
subjects = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<subjects.length();i++){
JSONObject c = subjects.getJSONObject(i);
String sname = c.getString(TAG_Sname);
String scode = c.getString(TAG_Scode);
String sgrade = c.getString(TAG_Sgrade);
HashMap<String,String> records = new HashMap<String,String>();
records.put(TAG_Sname,sname);
records.put(TAG_Scode,scode);
records.put(TAG_Sgrade,sgrade);
subList.add(records);
}
adapter = new SimpleAdapter(
getActivity(), subList, R.layout.list_layout,
new String[]{TAG_Sname,TAG_Scode,TAG_Sgrade},
new int[]{R.id.subject_name, R.id.subject_code, R.id.grade}
);
lv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Using notifyDataSetChanged() may suite your needs.
Refer to this: How to refresh Android listview?
Edit:
adapter = new SimpleAdapter(
getActivity(), subList, R.layout.list_layout,
new String[]{TAG_Sname,TAG_Scode,TAG_Sgrade},
new int[]{R.id.subject_name, R.id.subject_code, R.id.grade}
);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
The above code should help.
I am new in android.I am fetching data from mysql and showing it in listview.things are working fine but now i want to pass the value to intent when user click on listview(Row).I have implemented setOnItemClickListener.but the list view is dynamic so i am not getting how to get values and pass it to the intent.
Thanks in advance
public class AdminNotice extends Activity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private EditText editTextName;
SharedPreferences sp;
private String jsonResult;
private ListView listView;
private Button b;
EditText etname, et;
TextView tv;
String myJSON;
private static final String TAG = "MainActivity.java";
private static final String TAG_NAME = "notice";
private static final String TAG_DATE = "ndate";
ProgressBar progressBar;
Date date;
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
public static final String USER_NAME = "USERNAME";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noticelist);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
// String session_id= myprefs.getString("session_id", null);
//TextView textView = (TextView) findViewById(R.id.fname);
//textView.setText("Welcome "+session_id);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
// load icons from
// strings.xml
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
//send messages stop
//get json data start
protected void showList(){
try {
JSONArray peoples = new JSONArray(myJSON);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String name=null, date=null;
/*if(c==null){
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
}*/
if(c.has("notice"))
if(c.has("ndate"))
progressBar.setVisibility(View.GONE);
name = c.getString("notice");
date = c.getString("ndate");
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,name);
persons.put(TAG_DATE,date);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
AdminNotice.this, personList, R.layout.list_item1,
new String[]{TAG_NAME,TAG_DATE},
new int[]{R.id.name, R.id.date}
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
/*ModelClass obj = getItem(position);
String name = obj.getName();*/
// Simple Toast to show the position Selected
Log.d("SELECT_POSITION", "Position For this List Item = " + position);
}
});
/* list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});*/
} catch (JSONException e) {
Log.i("tagconvertstr", "["+myJSON+"]");
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
String session_id= myprefs.getString("session_id", null);
InputStream inputStream = null;
String result = null;
try {
String postReceiverUrl = "http://notice.php";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", session_id));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
inputStream = resEntity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "["+result+"]");
System.out.println(e);
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
//get json data stop
}
Inside your listview onItemClickListener
Intent intent=new Intent(currentyactivty.this,secondactiviy.class);
intent.putExtra("TAG_NAME", personList.get(position).get(TAG_NAME));
startActivity(intent);
To getdata in second activity at onCreate method
String data;
Intent in=getIntent();
if(in!=null && in.hasExtra("TAG_NAME")){
data=in.getStringArrayExtra("TAG_NAME");
}
you can set text from view
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Intent intent=new Intent(currentyactivty.this,secondactiviy.class);
intent.putExtra("NAME", personList.get(position).get(TAG_NAME));
startActivity(intent);
// Simple Toast to show the position Selected
Log.d("SELECT_POSITION", "Position For this List Item = " + position);
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
/*ModelClass obj = getItem(position);
String name = obj.getName();*/
String personName = personList.get(position).get(TAG_NAME);
Intent i = new Intent(AdminNotice.this, YourNextActivity.class);
i.putExtra("person_name", personName);
startActivity(i);
// Simple Toast to show the position Selected
Log.d("SELECT_POSITION", "Position For this List Item = " + position);
}
});
you can use bundle to pass you're data from intent.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
ModelClass obj = getItem(position);
String name = obj.getName();
String date = obj.getDate();
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("date", date );
Intent in = new Intent(currentActivity.this,destinationActivity.class);
in.putExtras(b);
startActivity(in);
// Simple Toast to show the position Selected
Log.d("SELECT_POSITION", "Position For this List Item = " + position);
}
});
and you can get it in your other activity by doing.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent in = getIntent();
Bundle b = in.getExtras();
String name = b.getString("name");
String date = b.getString("date);
}
with this approach you don't have to hit the database again to retrieve data and you're one query is saved.
similarly you can send just you're 'id' also if you want.
but if you just want to pass 'id' then #Nas method would be more sorted and easy.
Im creating an android app where in im going to fetch datas such as image and text from mysql database. I have a problem with it. When i run the program, nothing was display in my listview.
Here's my City.java
public class City extends ActionBarActivity {
String myJSON;
ImageView image;
TextView name,id,description;
private static final String TAG_RESULTS="result";
public static final String TAG_ID = "place_id";
public static final String TAG_NAME = "place_name";
public static final String TAG_ADD ="description";
//public static final String TAG_PIC ="place_icture";
JSONArray cities = null;
ArrayList<HashMap<String, String>> CitiesList;
UserLocalStore userLocalStore;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.citylayout);
userLocalStore = new UserLocalStore(this);
name=(TextView)findViewById(R.id.name);
id=(TextView)findViewById(R.id.id);
description=(TextView)findViewById(R.id.description);
list = (ListView) findViewById(R.id.listView1);
CitiesList = new ArrayList<HashMap<String, String>>();
image = (ImageView)findViewById(R.id.image);
getData();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(City.this, "Opening", Toast.LENGTH_LONG).show();
Map<String, Object> map = (Map<String, Object>)parent.getItemAtPosition(position);
String info1 = (String) map.get("place_id");
String info2 = (String) map.get("place_name");
String info3 = (String) map.get("description");
//String info4 = (String) map.get("place_picture");
Intent myIntent = new Intent(view.getContext(), Login.class);
myIntent.putExtra("info1", info1);
myIntent.putExtra("info2", info2);
myIntent.putExtra("info3", info3);
//myIntent.putExtra("info4", info4);
startActivity(myIntent);
}
});
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
cities = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<cities.length();i++){
JSONObject c = cities.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);
//String picture = c.getString(TAG_PIC);
HashMap<String,String> persons = new HashMap<String, String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
//persons.put(TAG_PIC,picture);
CitiesList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
City.this, CitiesList, R.layout.city,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id,R.id.name, R.id.description}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://iguideph-001-site1.btempurl.com/city_info.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Please change:
public static final String TAG_PIC ="place_icture";
to
public static final String TAG_PIC ="place_picture";
in the declaration.
and
persons.put(TAG_PIC,picture);
to
persons.put(TAG_PIC,String.valueOf(d));
in showList()
Hope this helps!
I am new in android,I have created a simple listview which is fetching data from mysql and every thing is working fine but now i want to pass the values of listview(Row) to another activity and when i am clicking on particular row i am getting null value.
public class AdminNotice extends Activity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private EditText editTextName;
SharedPreferences sp;
private String jsonResult;
private ListView listView;
private Button b;
EditText etname, et;
TextView tv;
String myJSON;
private static final String TAG = "MainActivity.java";
private static final String TAG_NAME = "notice";
private static final String TAG_DATE = "ndate";
ProgressBar progressBar;
Date date;
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
public static final String USER_NAME = "USERNAME";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noticelist);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
// String session_id= myprefs.getString("session_id", null);
//TextView textView = (TextView) findViewById(R.id.fname);
//textView.setText("Welcome "+session_id);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
// load icons from
// strings.xml
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
//send messages stop
//get json data start
protected void showList(){
try {
JSONArray peoples = new JSONArray(myJSON);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String name=null, date=null;
/*if(c==null){
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
}*/
if(c.has("notice"))
if(c.has("ndate"))
progressBar.setVisibility(View.GONE);
name = c.getString("notice");
date = c.getString("ndate");
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap<String, Object> data = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
Intent intent = new Intent(getApplicationContext(), SingleMessage.class);
intent.putExtra("KEY", data);
startActivity(intent);
}
});
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,name);
persons.put(TAG_DATE,date);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
AdminNotice.this, personList, R.layout.list_item1,
new String[]{TAG_NAME,TAG_DATE},
new int[]{R.id.name, R.id.date}
);
list.setAdapter(adapter);
/* list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});*/
} catch (JSONException e) {
Log.i("tagconvertstr", "["+myJSON+"]");
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
String session_id= myprefs.getString("session_id", null);
InputStream inputStream = null;
String result = null;
try {
String postReceiverUrl = "http://.php";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", session_id));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
inputStream = resEntity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "["+result+"]");
System.out.println(e);
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
//get json data stop
}
set the OnItemClickListener to the list view
public boolean onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
/ /write your code here to send data like
ModelClass obj = getItem(position);
String name = obj.getName();
Intent intent = new Intent(Activity.this, Activity2.class);
intent.putExtras("Name", name);
startActivity(intent);
}
or you can do like this way also
interfaceObj.sendData(obj);