Call startActivityForResult() on the Activity, then. Depending on how you have set up your code, that might simply be a matter of:
MyActivity.this.startActivityForResult() or possibly you will need to arrange to pass the Activity instance to your OnClickListener. On Wed, Sep 1, 2010 at 8:12 AM, mani <[email protected]> wrote: > > Hi all, > > I am in a situation like, i have custom list view. One button, with > listitems. > > When i click the button, i need to switch the activity. But the > calls, > startActivityForResult(myIntent, 0); --> is a non static call. I > couldnt initiate this function in onClickLIstener() for the button. > > Dont know how to proceed with this situation ? > > Can anyone help me out with this.? > > > Thanks in advance...!! > > > Thanks, > 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.HttpClient; > import org.apache.http.client.methods.HttpGet; > import org.apache.http.impl.client.DefaultHttpClient; > 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.OnClickListener; > import android.view.ViewGroup; > import android.widget.BaseAdapter; > import android.widget.Button; > import android.widget.ImageView; > import android.widget.TextView; > > public class results 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(context.getResources(), > R.drawable.next_arrow); > mIcon2 = > BitmapFactory.decodeResource(context.getResources(), > R.drawable.next_arrow); > } > > public int getCount() { > return DATA.length+1; > } > public Object getItem(int position) { > return position; > } > > public long getItemId(int position) { > return position; > } > > public View getView(int position, View convertView, ViewGroup > parent) { > ViewHolder holder; > if(position == 0) > { > convertView = mInflater.inflate(R.layout.main1, null); > Button button = > (Button)convertView.findViewById(R.id.button); > button.setOnClickListener(mButtonListener); > } > else > { > if (convertView == null) { > convertView = mInflater.inflate(R.layout.main2, null); > holder = new ViewHolder(); > holder.title = (TextView) > convertView.findViewById(R.id.title); > holder.address = (TextView) > convertView.findViewById(R.id.address); > holder.icon = (ImageView) > convertView.findViewById(R.id.icon); > convertView.setTag(holder); > } else { > holder = (ViewHolder) convertView.getTag(); > } > holder.title.setText(DATA[position-1]); > holder.address.setText(ADDR[position-1]); > holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : > mIcon2); > } > return convertView; > } > static class ViewHolder { > TextView title; > TextView address; > ImageView icon; > } > } > > private static OnClickListener mButtonListener = new > OnClickListener() { > public void onClick(View v) { > Intent myIntent = new Intent(v.getContext(), > firstandroid.class); > // How do i switch to other activity from here ???? > // startActivityForResult(myIntent, 0); > // finish(); > sendSearchRequest(8); > > } > }; > public static String sendSearchRequest(int count) > { > HttpClient client = new DefaultHttpClient(); > String query = "http://ajax.googleapis.com/ajax/services/search/local? > hl=en&v=1.0&q=paris hotel&start="; > query+=count; > 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!"); > } > return result; > } > public static String Userrequest(HttpResponse response){ > try{ > InputStream in = response.getEntity().getContent(); > 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(); > > }catch(Exception ex){ > result = "Error"; > } > return result; > } > public static void updateData(String result) > { > try > { > JSONObject json=new JSONObject(result); > JSONArray ja; > json = json.getJSONObject("responseData"); > 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("titleNoFormatting").toString(); > JSONArray addr; > addr = resultObject.getJSONArray("addressLines"); > int count = addr.length(); > ADDR[i]=""; > for(int j=0;j<count;j++) > { > ADDR[i]+=addr.getString(j); > if(j==0) > ADDR[i]+=','; > } > } > } > catch(Exception e) > { > > > } > } > �...@override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > sendSearchRequest(0); > updateData(result); > setListAdapter(new EfficientAdapter(this)); > } > > } > > > main2.xml > ---------------- > > <?xml version="1.0" encoding="utf-8" ?> > > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" android:orientation="horizontal" > android:layout_width="match_parent" > android:layout_height="match_parent"> > > <TextView android:id="@+id/title" > android:layout_gravity="center_vertical" > android:layout_width="0dip" > android:layout_weight="1.0" > android:layout_height="wrap_content" /> > <TextView android:id="@+id/address" > android:layout_gravity="center_vertical" > android:layout_width="0dip" > android:layout_weight="1.0" > android:layout_height="wrap_content" /> > > <ImageView android:id="@+id/icon" > android:layout_gravity="center_vertical" > android:layout_width="30dip" > android:layout_height="30dip" /> > > </LinearLayout> > > main1.xml > --------------------- > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > android:orientation="vertical" > > <Button android:id="@+id/button" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:text="More" /> > </LinearLayout> > > > Thanks, > - mani > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android App Developer Books: http://commonsware.com/books -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

