简体   繁体   English

如何使用SimpleAdapter为ListView制作分隔符

[英]How do I make separators for a ListView with a SimpleAdapter

I'm new to the java programming language and I'm trying to build an app. 我是Java编程语言的新手,正在尝试构建一个应用程序。 The app has to make a list of worked hours that have been saved in an MySQL database. 该应用程序必须列出已保存在MySQL数据库中的工作时间列表。 I found an example app, that helped me retrieving the data from the database and putting it in a ListView. 我找到了一个示例应用程序,该应用程序帮助我从数据库中检索了数据并将其放入ListView中。 But now we get to my problem. 但是现在我们解决了我的问题。 I want to put separators in the listview. 我想在列表视图中放置分隔符。

Now, the date of the worked hours is in every item of the ListView. 现在,工作时间的日期在ListView的每个项目中。 I want the date only above the first item. 我只希望日期在第一项上方。

I've searched the internet for a way to do this, but it didn't help me. 我已经在互联网上搜索了实现此目的的方法,但这并没有帮助我。

This it the code that gets the data and puts it in a ListView: 这是获取数据并将其放入ListView的代码:

    public class AllUrenActivity extends ListActivity {
    String url_all_uren;
    String ip;
    String proid;
    String uid = MainScreenActivity.uid;
    String datum;
    String datum1;
    ImageView btntoevoegen;
// Progress Dialog
private ProgressDialog pDialog;


    TextView tvDatum;

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

ArrayList<HashMap<String, String>>urenList;

    // JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_UREN = "uren";
private static final String TAG_TRID = "trid";
private static final String TAG_PROID = "proid";
private static final String TAG_WERKZAAMHEID = "werkzaamheid";
private static final String TAG_TIJD = "tijd";
private static final String TAG_DATUM = "datum";

// products JSONArray
JSONArray uren = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_uren);

    SharedPreferences settings = getSharedPreferences("databaseIP", 0);
    ip = settings.getString("ip", "").toString();
    url_all_uren = ("http://"+ip+"/android_connect/get_all_uren.php");


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

    // Loading products in Background Thread
    new LoadAllUren().execute();

    // Get listview
    ListView lv = getListView();


    // on selecting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String proid = ((TextView) view.findViewById(R.id.tvProid)).getText()
                    .toString();
            String werkzaamheid = ((TextView) view.findViewById(R.id.tvWerkzaamheid)).getText()
                    .toString();
            String trid = ((TextView) view.findViewById(R.id.tvTrid)).getText()
                    .toString();
            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    AllProjectsActivity.class);
            // sending pid to next activity
            in.putExtra(TAG_PROID, proid);
            in.putExtra(TAG_TRID, trid);
            in.putExtra(TAG_WERKZAAMHEID, werkzaamheid);
            // starting new activity and expecting some response back
            startActivityForResult(in, 100);

        }
    });

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received 
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllUren extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllUrenActivity.this);
        pDialog.setMessage("Uren laden...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }


    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {




        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("uid", uid));

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_uren, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("Uren: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                uren = json.getJSONArray(TAG_UREN);

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

                    // Storing each json item in variable
                    String trid = c.getString(TAG_TRID);
                    String proid = c.getString(TAG_PROID);
                    String werkzaamheid = c.getString(TAG_WERKZAAMHEID);
                    String datum = c.getString(TAG_DATUM);
                    String tijd = c.getString(TAG_TIJD);


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

                    // adding each child node to HashMap key => value
                    map.put(TAG_TRID, trid);
                    map.put(TAG_WERKZAAMHEID, werkzaamheid);
                    map.put(TAG_PROID, proid);
                    map.put(TAG_TIJD, tijd);
                    map.put(TAG_DATUM, datum);


                    // adding HashList to ArrayList
                    urenList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread

        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                    ListAdapter adapter = new SimpleAdapter(
                            AllUrenActivity.this, urenList,
                                    R.layout.list_uren, new String[] { TAG_TRID, TAG_PROID, TAG_WERKZAAMHEID, TAG_TIJD, TAG_DATUM},
                            new int[] { R.id.tvTrid, R.id.tvProid, R.id.tvWerkzaamheid, R.id.tvTijd, R.id.tvDatum });

                    // updating listview
                    setListAdapter(adapter);
                }   


        });

    }

}
     }

list_uren.xml list_uren.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="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tvTrid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- Name Label -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvDatum"
            android:layout_width="269dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="6dip"

            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="17sp"
            android:visibility="visible" />

        <ImageView
            android:id="@+id/toevoegen"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@android:drawable/ic_menu_add" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvTijd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="6dip"

            android:text="8:00-12:00"
            android:textSize="17sp"
            android:textStyle="bold"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tvProid"
            android:layout_width="138dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:paddingLeft="6dip"

            android:text="Project"
            android:textSize="17sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvWerkzaamheid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:text="Werkzaamheid"
        android:textSize="17sp" />

        </LinearLayout>

all_uren.xml all_uren.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">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

What is the best way to get the date separators? 获取日期分隔符的最佳方法是什么? Thank you. 谢谢。

Add this at the end of list_uren.xml before closing the LinearLayout 在关闭LinearLayout之前,将其添加到list_uren.xml的末尾

    <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#666666" />

You can change the background to whatever you want. 您可以将背景更改为任何您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM