Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select a particular item from the listview in android?
    primarykey
    data
    text
    <p>I have one list-view.It contains some data from the web-service by consuming it.</p> <p>Here my need is, if i click any item from that list-view, it wants to show data from the web service. have i to write any loop code for this?if so how?examples please. How to do this?</p> <p><strong>Logcat</strong></p> <pre><code>03-20 13:09:07.187: W/KeyCharacterMap(275): No keyboard for id 0 03-20 13:09:07.187: W/KeyCharacterMap(275): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 03-20 13:09:13.116: I/System.out(275): Transparent 17170445 03-20 13:09:13.136: W/System.err(275): org.json.JSONException: Value Login of type java.lang.String cannot be converted to JSONArray 03-20 13:09:13.136: W/System.err(275): at org.json.JSON.typeMismatch(JSON.java:107) 03-20 13:09:13.136: W/System.err(275): at org.json.JSONArray.&lt;init&gt;(JSONArray.java:91) 03-20 13:09:13.136: W/System.err(275): at org.json.JSONArray.&lt;init&gt;(JSONArray.java:103) 03-20 13:09:13.136: W/System.err(275): at com.example.png.StationsListActivity.callService(StationsListActivity.java:115) 03-20 13:09:13.136: W/System.err(275): at com.example.png.StationsListActivity$4.run(StationsListActivity.java:103) 03-20 13:09:13.196: D/AndroidRuntime(275): Shutting down VM 03-20 13:09:13.196: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 03-20 13:09:13.206: E/AndroidRuntime(275): FATAL EXCEPTION: main 03-2 0 13:09:13.206: E/AndroidRuntime(275): java.lang.NullPointerException 03-20 13:09:13.206: E/AndroidRuntime(275): at com.example.png.StationsListActivity$1.handleMessage(StationsListActivity.java:43) 03-20 13:09:13.206: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99) 03-20 13:09:13.206: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123) 03-20 13:09:13.206: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627) 03-20 13:09:13.206: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method) 03-20 13:09:13.206: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521) 03-20 13:09:13.206: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 03-20 13:09:13.206: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-20 13:09:13.206: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p><strong>StationsListActivity.java</strong></p> <pre><code>public class StationsListActivity extends Activity { TextView txt; String res_data; Button btn_info; ListView listView; ProgressDialog progressDialog; List&lt;Stations&gt; list; Handler handler =new Handler() { public void handleMessage(android.os.Message msg) { if(progressDialog.isShowing()) { progressDialog.cancel(); if(list.size() &gt; 0) { listView.setAdapter(new StationAdapter(StationsListActivity.this, android.R.id.text1,list)); } else { Toast.makeText(StationsListActivity.this, "List is Empty",Toast.LENGTH_LONG).show(); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stations_list_layout); listView = (ListView)findViewById(R.id.listView_stations); Bundle extras=getIntent().getExtras(); if(extras!=null) { res_data =extras.getString("transfered_data"); } System.out.println("Transparent "+android.R.color.transparent); listView = (ListView) findViewById(R.id.listView_stations); btn_info = (Button)findViewById(R.id.btn_info); progressDialog = new ProgressDialog(StationsListActivity.this); progressDialog.setTitle("Loading"); progressDialog.setMessage("Please Wait"); btn_info.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intnt = new Intent(StationsListActivity.this , InformationsActivity.class); startActivity(intnt); } }); Thread thread =new Thread() { @Override public void run() { // TODO Auto-generated method stub callService(); handler.sendEmptyMessage(0); } }; thread.start(); progressDialog.show(); } public void callService() { // TODO Auto-generated method stub try { JSONArray array = new JSONArray(String.valueOf(res_data).replace("verifyUserResponse{return=", "").replace("; }", "")); list = new ArrayList&lt;Stations&gt;(); for (int i = 0; i &lt; array.length(); i++) { System.out.println(array.getJSONObject(i).get("1")+" == "+array.getJSONObject(i).get("0"));; Stations stations =new Stations(); stations.station_code = (String) array.getJSONObject(i).get("0"); stations.station_name = (String) array.getJSONObject(i).get("1"); list.add(stations); stations = null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p><strong>StationAdaper.java</strong></p> <pre><code>public class StationAdapter extends ArrayAdapter&lt;Stations&gt; { Context context; List&lt;Stations&gt; list; public StationAdapter(Context context, int textViewResourceId, List&lt;Stations&gt; objects) { super(context, textViewResourceId, objects); this.context = context ; this.list = objects ; // TODO Auto-generated constructor stub } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.station_child, null); TextView name = (TextView) view.findViewById(R.id.textView_single_item); ImageView img = (ImageView)view.findViewById(R.id.imageView1); Stations stations =list.get(position); name.setText(stations.station_name); return view; }} </code></pre> <p><strong>Stations.java</strong> public class Stations { </p> <pre><code>public String station_name =""; public String station_code =""; } </code></pre> <p><strong>station_child.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"&gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="55dp" &gt; &lt;TextView android:id="@+id/textView_single_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:inputType="text" android:textColor="#ffffff" android:textStyle="normal" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="@drawable/arrow"/&gt; &lt;/RelativeLayout&gt;&lt;/LinearLayout&gt; </code></pre> <p>suggestions please Thanks for your precious time!..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload