活动列表视图中的粘性行

时间:2014-01-04 20:47:30

标签: android listview sticky

我的listview中只有一行应该是粘性的。在stickyheaders中我没有使用字母表的部分或部分。我非常感谢任何帮助w.r.t listview在活动中粘贴一行而不是片段。我该怎么办?我非常感谢任何帮助。谢谢你。 使用如下代码:

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);
    }
}

2 个答案:

答案 0 :(得分:1)

我为Json写了这段代码......希望这能解决你的难题......你喜欢这个......

<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);
            }

        }
    }

答案 1 :(得分:0)

仅使用ListView无法实现。

如果我在你的位置,我会使用相同的单项布局(将在ListView的单个项目中使用)作为固定布局,下面我将有我的ListView。

<Layout1>
    <Layout2>
        <ItemView1>
        </ItemView1>
    </Layout2>
    <ListView>
    </ListView> 
</Layout1>

Layout1和Layout2可以是相对或线性布局

ItemView1与将在ListView项目布局中使用的布局相同。

由于我的第一行是常量,我将在调用视图后设置值,其余的将照常工作。