ListView元素选择

时间:2014-08-20 15:07:47

标签: android android-listview

按下listView项目时不工作。无法弄清楚出了什么问题。给我一个错误:

java.lang.NumberFormatException:无效的int:“null” 在这个Add_Update_User,java行         USER_ID = Integer.parseInt(getIntent()。getStringExtra(“USER_ID”));

Add_Update_User.java

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Add_Update_User extends Activity {

    EditText add_name, add_note;
    Button add_save_btn, update_btn, dlt_btn; 

    RelativeLayout add_view, update_view;

    String valid_note = null, valid_name = null, Toast_msg = null, valid_user_id = "", add_date; //valid_email = null

    int USER_ID;

    DatabaseHandler dbHandler = new DatabaseHandler(this);

       String formattedDate;
       SimpleDateFormat date;
       Calendar c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_update_screen);

     c = Calendar.getInstance();
     date = new SimpleDateFormat("yyyy-MM-dd");
     formattedDate = date.format(c.getTime());

    // set screen
    Set_Add_Update_Screen();

    // set visibility of view as per calling activity
    String called_from = getIntent().getStringExtra("called");

    if (called_from.equalsIgnoreCase("add")) {
        add_view.setVisibility(View.VISIBLE);
        update_view.setVisibility(View.GONE);
    } else {

        update_view.setVisibility(View.VISIBLE);
        add_view.setVisibility(View.GONE);
     Bundle extras = getIntent().getExtras();    
    if(extras !=null){

    USER_ID = extras.getInt("USER_ID");

    Contact c = dbHandler.Get_Contact(USER_ID);

    add_name.setText(c.getName());
    add_note.setText(c.getNote());
    }

    }
    add_note.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        // min lenth 10 and max lenth 12 (2 extra for - as per phone
        // matcher format)
        Is_Valid_Sign_Number_Validation(12, 12, add_note);
        }
    });
    add_note.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

    add_name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        Is_Valid_Person_Name(add_name);
        }
    });

    add_save_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dbHandler.Add_Contact(new Contact(valid_name, valid_note, formattedDate));
            Toast_msg = "Data inserted successfully";
            Show_Toast(Toast_msg);
            Reset_Text();    
            Intent intent = new Intent(Add_Update_User.this, Main_Screen.class);
            startActivity(intent);
        }
    });

    update_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

        valid_name = add_name.getText().toString();
        valid_note = add_note.getText().toString();

            dbHandler.Update_Contact(new Contact(USER_ID, valid_name,valid_note, formattedDate));
            dbHandler.close();
            Toast_msg = "Data Update successfully";
            Show_Toast(Toast_msg);
            Reset_Text();

            Intent intent = new Intent(Add_Update_User.this, Main_Screen.class);
            startActivity(intent);
        }
    });
    dlt_btn.setOnClickListener(new OnClickListener() {
        @Override
           public void onClick(View v) {

            DatabaseHandler dBHandler = new DatabaseHandler(getApplicationContext());
            dBHandler.Delete_Contact(USER_ID);
            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();  
            Intent intent = new Intent(getApplicationContext(), chintan.khetiya.sqlite.cursor.Main_Screen.class);
            startActivity(intent);
            finish();
           }
       });
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.mainmenu, menu);
       return true;
       }
    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 

       super.onOptionsItemSelected(item); 
       switch(item.getItemId()) 
       { 
          case R.id.item1: 
            dbHandler.Add_Contact(new Contact(valid_name, valid_note, formattedDate));
            Toast_msg = "Data inserted successfully";
            Show_Toast(Toast_msg);
            Reset_Text();
            Intent add_user = new Intent(getApplicationContext(),chintan.khetiya.sqlite.cursor.Add_Update_User.class);
            add_user.putExtra("called", "add");
            add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(add_user);
            finish();
        }
    return false; 

    } 

    public void Set_Add_Update_Screen() {

    add_name = (EditText) findViewById(R.id.editTextName);
    add_note = (EditText) findViewById(R.id.editTextNote);


    add_save_btn = (Button) findViewById(R.id.buttonSave);
    update_btn = (Button) findViewById(R.id.buttonUpdate);
    dlt_btn = (Button) findViewById(R.id.buttonDelete);

    add_view = (RelativeLayout) findViewById(R.id.add_view);
    update_view = (RelativeLayout) findViewById(R.id.update_view);

    add_view.setVisibility(View.GONE);
    update_view.setVisibility(View.GONE);

    }

    public void Is_Valid_Sign_Number_Validation(int MinLen, int MaxLen,
        EditText edt) throws NumberFormatException {

        valid_note = edt.getText().toString();
    }

    public void Is_Valid_Person_Name(EditText edt) throws NumberFormatException {
        valid_name = edt.getText().toString();
    }

    public void Show_Toast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    public void Reset_Text() {
    add_name.getText().clear();
    add_note.getText().clear();
    }

   public boolean onKeyDown(int keycode, KeyEvent event) {
          if (keycode == KeyEvent.KEYCODE_BACK) {
             Intent a = new Intent(this,Main_Screen.class);
             a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(a);
             return true;
          }
          return super.onKeyDown(keycode, event);
   }
}

Main_Screen.java

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main_Screen extends Activity {
    Button add_btn;
    ListView Contact_listview;
    ArrayList<Contact> contact_data = new ArrayList<Contact>();
    Contact_Adapter cAdapter;
    DatabaseHandler db;
    String Toast_msg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        Contact_listview = (ListView) findViewById(R.id.listView1);
        Set_Referash_Data();

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("some error", "" + e);
    }

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

      super.onOptionsItemSelected(item); 
      switch(item.getItemId()) 
      { 
         case R.id.item1: 
            // TODO Auto-generated method stub
            Intent add_user = new Intent(Main_Screen.this, Add_Update_User.class);
            add_user.putExtra("called", "add");
            add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(add_user);
            finish();

       }
    return false; 

   } 

    public void Set_Referash_Data() {
    contact_data.clear();
    db = new DatabaseHandler(this);
    ArrayList<Contact> contact_array_from_db = db.Get_Contacts();

    for (int i = 0; i < contact_array_from_db.size(); i++) {

        int tidno = contact_array_from_db.get(i).getID();
        String name = contact_array_from_db.get(i).getName();
        String note = contact_array_from_db.get(i).getNote();
        String date = contact_array_from_db.get(i).getDate();
        Contact cnt = new Contact();
        cnt.setID(tidno);
        cnt.setName(name);
        cnt.setNote(note);
        cnt.setDate(date);

        contact_data.add(cnt);
    }
    db.close();
    cAdapter = new Contact_Adapter(Main_Screen.this, R.layout.listview_row, contact_data);
    Contact_listview.setAdapter(cAdapter);
    cAdapter.notifyDataSetChanged();
    }

    public void Show_Toast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Set_Referash_Data();

    }

    public class Contact_Adapter extends ArrayAdapter<Contact> {
    Activity activity;
    int layoutResourceId;
    Contact user;
    ArrayList<Contact> data = new ArrayList<Contact>();

    public Contact_Adapter(Activity act, int layoutResourceId,
        ArrayList<Contact> data) {
        super(act, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.activity = act;
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserHolder holder = null;

        if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);

        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.name = (TextView) row.findViewById(R.id.user_name_txt);
        holder.number = (TextView) row.findViewById(R.id.user_mob_txt);
        row.setTag(holder);
        } else {
        holder = (UserHolder) row.getTag();
        }
        user = data.get(position);
        Contact_listview.setTag(user.getID());
        holder.name.setText(user.getName());
        holder.number.setText(user.getDate());

        Contact_listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

        Contact user=(Contact)cAdapter.getItem(position);

        Intent update_user = new Intent(activity, Add_Update_User.class);

        Bundle mBundle = new Bundle();
        mBundle.putString("called", "update");
        mBundle.putInt("USER_ID", user.getID());
        update_user.putExtras(mBundle);            
        activity.startActivity(update_user);

        }
        });
        return row;
    }

        class UserHolder {
            TextView name;
            TextView number;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我需要在Main_Screen.java中创建新的Bundle 并从Add_Update_User.java访问它 我编辑了我的代码