0

主要的.xml

<include layout="@layout/header" />

<ListView
    android:id="@+id/alist"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:dividerHeight="0dp"
    android:listSelector="@drawable/list_selector" />

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_reminders"
    android:visibility="gone" />
<TextView
android:id="@+id/TextView02" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>

ReminderListActivity.java

package com.dummies.android.taskreminder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.dummies.android.taskreminder.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ReminderListActivity extends Activity implements OnClickListener {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    private RemindersDbAdapter mDbHelper;
    ListView lvRemind;
    ListView llv;
    ImageButton btnAdd, btnBack, btnSettings;
    TextView text;
    LinearLayout layout;
    ArrayAdapterExample aAdapter;
    String remainTime;
    int finalVal;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // setContentView(R.layout.reminder_list);
        LayoutInflater inflate = LayoutInflater.from(this);
        layout = (LinearLayout) inflate.inflate(R.layout.main, null);
        this.setContentView(layout);
        btnAdd = (ImageButton) findViewById(R.id.btnAdd);
        btnBack = (ImageButton) findViewById(R.id.btnBackMain);
        btnSettings = (ImageButton) findViewById(R.id.btnSettings);

        btnAdd.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        btnSettings.setOnClickListener(this);
        mDbHelper = new RemindersDbAdapter(this);
        mDbHelper.open();

        llv = (ListView) findViewById(R.id.alist);
        fillData();

        llv.setAdapter(aAdapter);
        llv.setClickable(true);
        llv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(ReminderListActivity.this, "hello", Toast.LENGTH_SHORT).show();

            }
        });
        registerForContextMenu(llv);

    }

    private void fillData() {
        Cursor remindersCursor = mDbHelper.fetchAllReminders();
        startManagingCursor(remindersCursor);

        // Create an array to specify the fields we want to display in the list
        // (only TITLE)
        String[] from = new String[] { RemindersDbAdapter.KEY_TITLE };

        Log.d("Length: ", "" + from.length);
        // and an array of the fields we want to bind those fields to (in this
        // case just text1)
        int[] to = new int[] { R.id.text1 };

        // Now create a simple cursor adapter and set it to display
        // SimpleCursorAdapter reminders =
        // new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor,
        // from, to);
        // setListAdapter(reminders);

        text = (TextView) findViewById(R.id.TextView02);



        // listview.setLayoutParams(new LinearLayout.LayoutParams(
        // LinearLayout.LayoutParams.FILL_PARENT,
        // LinearLayout.LayoutParams.FILL_PARENT));
        // layout.addView(listview);

        aAdapter = new ArrayAdapterExample(ReminderListActivity.this,
                mDbHelper.fetchAllReminders());




    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.list_menu_item_longpress, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_delete:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                    .getMenuInfo();
            mDbHelper.deleteReminder(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void createReminder() {
        Intent i = new Intent(this, ReminderEditActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    // @Override
    // protected void onListItemClick(ListView l, View v, int position, long id)
    // {
    // super.onListItemClick(l, v, position, id);
    // Intent i = new Intent(this, ReminderEditActivity.class);
    // i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
    // startActivityForResult(i, ACTIVITY_EDIT);
    // }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnAdd:
            createReminder();
            break;
        case R.id.btnSettings:
            Intent i = new Intent(this, TaskPreferences.class);
            startActivity(i);
            break;
        case R.id.btnBackMain:
            onBackPressed();
            break;
        default:
            break;
        }

    }

    @SuppressLint("NewApi")
    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

    class ArrayAdapterExample extends CursorAdapter {

        public ArrayAdapterExample(Context context, Cursor c) {

            super(context, c);

        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // TODO Auto-generated method stub
            SeekBar sBar = (SeekBar) view.findViewById(R.id.seekBarTime);

            String curDte = cursor.getString(cursor
                    .getColumnIndex(cursor.getColumnName(3)));



            TextView txtNote = (TextView) view.findViewById(R.id.label);
            TextView tvDateValue = (TextView) view.findViewById(R.id.tvValueDate);
            txtNote.setText(cursor.getString(cursor.getColumnIndex(cursor
                    .getColumnName(1))));
            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
            Date date = new Date();

            try {
                String format = "dd-MM-yyyy hh:mm a";

                DateFormat sdf = new SimpleDateFormat(
                        "EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

                String v_date_str = cursor.getString(cursor
                        .getColumnIndex(cursor.getColumnName(4)));

                String curDate = cursor.getString(cursor
                        .getColumnIndex(cursor.getColumnName(3)));
                Log.d("CUrr SET DAT: ", curDate);

                // String setDate =sdf.format(dt + " " + tm);
                Date v_date = new SimpleDateFormat("yyyy-MM-dd HH:mm",
                        Locale.ENGLISH).parse(v_date_str);
                DateFormat formatter = null;

                formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm a");

                Log.d("sset: ", "" + formatter.format(v_date));
                String currDate = dateFormat.format(date);

                String setDate = formatter.format(v_date);
                tvDateValue.setText(setDate);
                Date cDate = new SimpleDateFormat("dd-MM-yyyy hh:mm a")
                        .parse(currDate);
                Log.d("Current: ", "" + cDate);

                Date sDate = new SimpleDateFormat("dd-MM-yyyy hh:mm a")
                        .parse(setDate);

                Date startDate = new SimpleDateFormat("dd-MM-yyyy hh:mm a").parse(curDate);
                Log.d("Set: ", "" + sDate);

                long diff = sDate.getTime() - cDate.getTime();
                if(diff>0){
                    long dif = sDate.getTime() - startDate.getTime();
                    Log.d("Starting: ", ""+dif);
                    Log.d("Ending: ", ""+diff);

                    int dif1 = (int)dif;
                    int dif2 = (int)diff;

                    Log.d("Starting1: ", ""+dif1);
                    Log.d("Ending2: ", ""+dif2);

                    sBar.setMax(100);


                    finalVal = 100*dif2/dif1;

                    Log.d("Result: ", ""+finalVal*100);
                    TextView txtPer = (TextView)view.findViewById(R.id.tvPercent);
                    txtPer.setText(""+finalVal+"% left");
                    sBar.setProgress(finalVal);

                    double diffInHours = diff / ((double) 1000 * 60 * 60);
                    double minLeft = (diffInHours - (int) diffInHours) * 60;
                    long mLeft = Math.round(minLeft);

                    remainTime = "("+(int) diffInHours + "H " + mLeft
                            + "M)";
                    TextView txtTime = (TextView) view
                            .findViewById(R.id.tvBelowSeekBar);

                    txtTime.setText(remainTime);
                    txtTime.setTextColor(Color.MAGENTA);

                }
                else{
                    long diffRev = cDate.getTime() - sDate.getTime();
                    double diffInHours = diff / ((double) 1000 * 60 * 60);
                    double minLeft = (diffInHours - (int) diffInHours) * 60;
                    long mLeft = Math.round(minLeft);

                    remainTime = "(Overdue!)";

                    TextView txtTime = (TextView) view
                            .findViewById(R.id.tvBelowSeekBar);

                    txtTime.setText(remainTime);
                    txtTime.setTextColor(Color.RED);
                }


            } catch (ParseException e) {
                e.printStackTrace();
            }





        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());

            View retView = inflater
                    .inflate(R.layout.list_remind, parent, false);

            return retView;
        }

    }

}

注意:我onitemclicklistener在 custom 上实现了listview。但它没有用。

4

1 回答 1

1

按照以下方式创建您的ListView控件,

llv = (ListView) layout.findViewById(R.id.alist); 

这样做的原因是您的 Listview 在您的正常布局中不可用,因为您正在代码中创建动态布局。

于 2013-03-08T06:14:36.630 回答