How to update items in custom listview content ?
Hi all,
I have a custom listview created. Now i want to update a contents of the listview based on a button Click. I got the contents to be update, but dont know how to proceed.
Can someone pls help me out here.?
-mani
Java file:
----------
package com.example.firstandroid;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpCli
import org.apache.http.client.methods
import org.apache.http.impl.client.De
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListe
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Screen2 extends ListActivity {
static String result="";
private static String DATA[];
private static String ADDR[];
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(c
mIcon2 = BitmapFactory.decodeResource(c
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#get
*/
public int getCount() {
return DATA.length+1;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#get
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#get
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#get
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if(position == 0)
{
convertView = mInflater.inflate(R.layout.mai
Button button = (Button)convertView.findViewBy
button.setOnClickListener(mBut
}
else
{
if (convertView == null) {
convertView = mInflater.inflate(R.layout.mai
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.
holder.address = (TextView) convertView.findViewById(R.id.
holder.icon = (ImageView) convertView.findViewById(R.id.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.title.setText(DATA[posi
holder.address.setText(ADDR[po
holder.icon.setImageBitmap((po
}
return convertView;
}
static class ViewHolder {
TextView title;
TextView address;
ImageView icon;
}
}
private static OnClickListener mButtonListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
HttpClient client = new DefaultHttpClient();
String query = "http://ajax.googleapis.com/aj
try {
URL url = new URL(query);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
result+=Userrequest(response);
}catch (URISyntaxException e){
}
catch(Exception ex){
//txtResult.setText("Failed!")
}
}
};
public static String Userrequest(HttpResponse response){
String result = "";
try{
InputStream in = response.getEntity().getConten
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
str.append(line + "\n");
}
in.close();
result = str.toString();
// I have the data here, need to update the list. How to call Oncreate() function ?????
}catch(Exception ex){
result = "Error";
}
return result;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceSt
Intent myIntent = new Intent(this , firstandroid.class);
//startActivityForResult(myInt
Bundle bundle = getIntent().getExtras();
result = bundle.getString("mani");
try
{
JSONObject json=new JSONObject(result);
JSONArray ja;
json = json.getJSONObject("responseDa
ja = json.getJSONArray("results");
int resultCount = ja.length();
DATA = new String[resultCount];
ADDR = new String[resultCount];
for (int i = 0; i < resultCount; i++)
{
JSONObject resultObject = ja.getJSONObject(i);
DATA[i]=resultObject.get("titl
// DATA[i]+="\n";
JSONArray addr;
addr = resultObject.getJSONArray("add
int count = addr.length();
ADDR[i]="";
for(int j=0;j
main2.xml
-------------
<?xml version="1.0" encoding="utf-8" ?>
can someone help me here?
- mani
it is sometimes world of hard to Pandora in a world of adults Pandora Bracelets
I thought I would find this much earlier considering how good the information is.tiffany uk store,
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@fraufletscher (Jessica Griebsch)I've just received an achievement: Affable http://t.co/ZobME8Cv #Android #Androidgames
8 min 55 sec ago -
@mark_pawlowski (mark pawlowski)#IPhone apps crash more than #Android apps. Who would have known?
http://t.co/8ln0iopQ9 min 2 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title--Voltage- http://t.co/zKs2TktH #music #musik #iPhone #Android 20-42-14
9 min 3 sec ago -
@180rad (paul radcliffe)I've just received an achievement: Lover of space https://t.co/f4Mcp9dW #Android #Androidgames
9 min 3 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title-FREEDOM- http://t.co/hbx947eL http://t.co/Nz84sxao #music #musik #iPhone #Android 20-41-54
9 min 22 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix



as if she can't forgive sense of dismay. mui mui handbags Pocket the phone ring, hasn't attract her attention.