Search code examples
androidlistviewsticky

sticky row in listview in activity


Just one row in my listview should be sticky .Not sections or section with alphabets in stickyheaders.I really appreciate any help w.r.t listview sticky one row within activity and not fragment.How do I do that ? I really appreciate any help.Thanks in Advance. Using code like :

class MyAsyncTask extends
        AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> UploadsList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();        }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... params) {

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Uploads
            uploads = json.getJSONArray(TAG_UPLOADS);

            // looping through All Uploads
            for (int i = 0; i < uploads.length(); i++) {
                JSONObject c = uploads.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String date = c.getString(TAG_DATE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_DATE, date);

                // adding HashList to ArrayList
                UploadsList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return UploadsList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

        pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(this, result,
                R.layout.list_item,
                new String[] { TAG_NAME, TAG_ID, TAG_DATE }, new int[] {
                R.id.name, R.id.id, R.id.date });

        setListAdapter(adapter);
    }
}

Solution

  • I wrote this code for Json... Hope this will solve your puzzle.. And you like this...

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity" >
    
            <TextView
                android:id="@+id/headerRow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:text="@string/hello_world" />
    
            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/headerRow" >
    
            </ListView>
    
        </RelativeLayout>
    
        public class MainActivity extends Activity {
    
            private ArrayList<String> data;
            private TextView stickRow;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                stickRow = (TextView) findViewById(R.id.headerRow);
                data = new ArrayList<String>();
                for (int ak = 1; ak < 20; ak++) {
                    data.add("Row " + ak);
                }
    
                ListView lv = (ListView) findViewById(R.id.listView1);
                lv.setAdapter(new CustomAdapter(getApplicationContext(), R.layout.activity_main));
    
            }
    
            class CustomAdapter extends ArrayAdapter<String> {
    
                public CustomAdapter(Context context, int resource) {
                    super(context, resource);
                }
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    // TODO Auto-generated method stub
                    if (position == 0) {
                        stickRow.setText(getItem(position));
                    }
                    TextView tv = new TextView(getApplicationContext());
                    tv.setTextSize(20);
                    tv.setText(getItem(position + 1));
                    return tv;
                }
    
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return data.size() - 1;
                }
    
                @Override
                public String getItem(int position) {
                    // TODO Auto-generated method stub
                    return data.get(position);
                }
    
            }
        }