打破了应用程序或删除了一些东

时间:2015-06-21 12:25:57

标签: android

我的按钮,在我的应用程序上名为imgStar。我希望当我点击此按钮在ListView中显示所有标记为" top"。此功能在前一天工作,但今天我删除了该应用程序我的手机,重装并且无法正常工作(
可以删除导致此问题吗? MyActivity:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnadd;
    LinearLayout llMain;
    Time today = new Time(Time.getCurrentTimezone());
    DBAdapter myDb;
    ImageButton imgStar;
    String changedname;
    public long idchange;
    private ListView myList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        btnadd = (Button) findViewById(R.id.button);
        //llMain = (LinearLayout) findViewById(R.id.llMain);
        btnadd.setOnClickListener(this);
        openDB();
        populateListView();
        imgStar = (ImageButton)findViewById(R.id.imageButton2);
        imgStar.setOnClickListener(this);
        myList = (ListView) findViewById(R.id.listView);

        listViewItemClick();
        listViewItemLongClick();
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this, result.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.imageButton2:
                ifTop();

                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode==1) {
            String name = data.getStringExtra("task");
            String grup = data.getStringExtra("grup");
            today.setToNow();
            String timestamp = today.format("%Y-%m-%d");
            myDb.insertRow(name, timestamp, grup);
            populateListView();
        }
        else if(requestCode==2){
            changedname = data.getStringExtra("changedname");
            updateTask(idchange,changedname);
            populateListView();
        }
    }

    private void openDB(){
        myDb = new DBAdapter(this);
        myDb.open();
    }
    private void populateListView()    {
        Cursor cursor = myDb.getAllRows();
        String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DATE};
        int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6,R.id.textView7};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
        myList = (ListView)findViewById(R.id.listView);
        myList.setAdapter(myCursorAdapter);
    }

    private void ifTop(){
        Cursor c = myDb.ifTopViews();
        String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DATE};
        int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6,R.id.textView7};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,c,fromFieldNames,toViewIDs,0);
        myList.setAdapter(myCursorAdapter);
    }
    private void listViewItemClick(){
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent1 = new Intent(MainActivity.this,edit.class);
                startActivityForResult(intent1, 2);
                idchange = id;
            }
        });
    }
    private void updateTask(long id,String changedname){
        Cursor cursor = myDb.getRow(id);
        if(cursor.moveToFirst()){
            today.setToNow();
            String date = today.format("%Y-%m-%d %H:%M:%S");
            this.changedname = changedname;
            myDb.updateRow(id,changedname,date,null);
        }
        cursor.close();

    }
    private void listViewItemLongClick(){
        myList = (ListView)findViewById(R.id.listView);
        myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                myDb.deleteRow(id);
                populateListView();
                return false;
            }
        });


    }
}

resultActivity:

public class result extends ActionBarActivity implements View.OnClickListener {
    EditText etName;
    Button btnOk;
    EditText etData;
    boolean bIcon = true;
ImageButton star;
    String grup;
    Intent intent = new Intent();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        etName = (EditText) findViewById(R.id.editText);
        btnOk = (Button) findViewById(R.id.button2);
star = (ImageButton) findViewById(R.id.imageButton);
        btnOk.setOnClickListener(this);
        star.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_result, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:

            intent.putExtra("task", etName.getText().toString());

            setResult(RESULT_OK, intent);

            finish();
                Toast.makeText(this,"entry is added",Toast.LENGTH_LONG).show();
                break;
            case R.id.imageButton:
                if (bIcon) {
                    star.setImageResource(R.drawable.star1);
                    grup = "'top'";


                }
                else

                {
                    star.setImageResource(R.drawable.star);
                    grup = null;
                }
                intent.putExtra("grup",grup);
                bIcon = !bIcon;
                break;
        }
    }
}

0 个答案:

没有答案