Sqlite数据库将值传递给多个Android表?

时间:2013-09-10 12:06:03

标签: android sqlite simplecursoradapter

我根据需要构建了一个三个sqlite表。

1.登陆

enter image description here

2.list

enter image description here

3.memo

enter image description here

在我的表中,我将登录ID(id)值从第1个表传递给显示id2的第二个表。

现在我想将id值从表2传递到id2下的表。

我试过通过游标使用intent传递值,但我没有得到任何值。它只显示零。

任何人都可以从我的代码中找出错误。

我的代码

1.class显示第三个表的详细信息

public class addmemo extends Activity{
EditText memtitle, content;
Button add, cancel;
int id1;
String title;
int id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addmemo);
    setUpViews();
    //title = (EditText) findViewById(R.id.admemotit);
    //content = (EditText) findViewById(R.id.addmem);
    add = (Button) findViewById(R.id.addmembutton);
    cancel = (Button) findViewById(R.id.canmembuttn);
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            addmemo.this.finish();
        }
    });
}
private void setUpViews() {
    // TODO Auto-generated method stub
    memtitle = (EditText) findViewById(R.id.admemotit);
    title = memtitle.getText().toString();
    content = (EditText) findViewById(R.id.addmem);
    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        id=extras.getInt("id");
        Log.d("idd", String.valueOf(id));
        id1=extras.getInt("id1");
        Log.d("idd1",String.valueOf(id1));
        memtitle.setText(extras.getString("title"));
        content.setText(extras.getString("content"));
        Log.d("title", memtitle.getText().toString());
    }
    add = (Button) findViewById(R.id.addmembutton);
    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (memtitle.getText().length() != 0) {
                AsyncTask<Object, Object, Object> saveContactTask = new AsyncTask<Object, Object, Object>() {
                    @Override
                    protected Object doInBackground(Object... params) {
                        saveContact();
                        Log.d("asd", "asd");
                        return null;
                    }
                    @Override
                    protected void onPostExecute(Object result) {
                        finish();
                    }
                };

                saveContactTask.execute((Object[]) null);
            } else {
                AlertDialog.Builder alert = new AlertDialog.Builder(
                        addmemo.this);
                alert.setTitle("Error In Save Contact");
                alert.setMessage("You need to Enter Name of the Contact");
                alert.setPositiveButton("OK", null);
                alert.show();
            }
        }
    });
}
private void saveContact() {


    logindatabaseadapter sqlCon = new logindatabaseadapter(this);
    Log.d("asd","asd1");
    if (getIntent().getExtras() == null) {
        sqlCon.insertmemoentry(memtitle.getText().toString(), content.getText().toString(),
                GetSet.getUserId(), GetSet.getListId());
        Log.d("ti", memtitle.toString());
        Log.d("co", content.toString());
        Log.d("i", String.valueOf(id));
    }

    else {
        boolean modified=true;
        sqlCon.updateContact(title.getText().toString(), category.getText().toString(),
                modified);
    }
}}

2.adapter class

@Override
public void bindView(View v, final Context context, Cursor c) {
    final int id = c.getInt(c.getColumnIndex(contacts.loginid1));
    Log.d("id", Integer.toString(id));
    final int id1 = c.getInt(c.getColumnIndex(contacts.loginid));
    Log.d("id1", Integer.toString(id1));
    final String title = c.getString(c.getColumnIndex(contacts.title));
    final String category = c.getString(c.getColumnIndex(contacts.category));
    c.moveToFirst();{
    ArrayList<String> newarray = new ArrayList<String>();
    newarray.add(title);
    }
    //curval = (TextView) v.findViewById(R.id.cursorvalue);
    Cursor cur = logindatabaseadapter.remindervalues(id,id1);
    if(cur!=null){
        cur.moveToFirst();
        str = cur.getCount();
        Log.d("vaal", String.valueOf(str));
    }
    curval.setText(xurva + "/" + "0");
    cur.close();

    Cursor cur1 = logindatabaseadapter.getcursorvalues(id,id1,values);
    if(cur!=null){
        cur.moveToFirst();
        str = cur.getCount();
        Log.d("vaal", String.valueOf(str));
    }
    String xurva1 = String.valueOf(str);
    curval = (TextView) v.findViewById(R.id.cursorvalue);
    curval.setText("  "+ xurva + "/" + xurva1);
    cur1.close();

    ImageView iv = (ImageView) v.findViewById(R.id.photo123);
    iv.setSelected(false);

        // TODO Auto-generated method stub
    if (category.equalsIgnoreCase("personal")) {

        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Entertainment")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Official")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Health")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Friends")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Travel")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Sports")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Education")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Birthday")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Miscellaneous")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else if (category.equalsIgnoreCase("Meeting")) {
        iv.setImageResource(R.drawable.ic_launcher);
    } else {
        iv.setImageResource(R.drawable.ic_launcher);
    }
    TextView ttitle = (TextView) v.findViewById(R.id.title);
    ttitle.setSelected(false);
    RelativeLayout layout = (RelativeLayout) v
            .findViewById(R.id.relativeLayout1);
    if (col % 2 != 1) {
        layout.setBackgroundColor(0xFFC1E7FF);
    } else {
        layout.setBackgroundColor(0xFF98D4FA);
    }       col++;      ttitle.setText(title);
    ImageView timage = (ImageView) v.findViewById(R.id.photo123);
    LinearLayout layou1 = (LinearLayout) v.findViewById(R.id.linearali1);
    if (col1 % 2 != 1) {
        layou1.setBackgroundColor(0xFF87CEFA);
    } else {
        layou1.setBackgroundColor(0xFF6495ED);
    }
    col1++;

    final logindatabaseadapter sqlCon = new logindatabaseadapter(context);

    v.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, memo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("id",id );
            Log.d("ci", String.valueOf(id));
            intent.putExtra("id1", id1);
            Log.d("ci1", String.valueOf(id1));
            context.startActivity(intent);
        }
    });
    v.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            // Toast.makeText(context, "U Long Clicked..", 5000).show();
                //return true;
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

                // set title
                alertDialogBuilder.setTitle("Delete");

                // set dialog message
                alertDialogBuilder
                    .setMessage("Are you sure you want to delete")
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id1) {
                            // if this button is clicked, close
                            // current activity
                            //sqlCon.deleteContact(id);
                            //Intent intent = new Intent(context, MyContactsActivitynew.class)
                                //  .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            //context.startActivity(intent);
                            dialog.dismiss();
                        }
                      })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();
                    return true;
                }
    });

}

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

在适配器类中,您将传递给类名memo.class

 v.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, memo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("id",id );
        Log.d("ci", String.valueOf(id));
        intent.putExtra("id1", id1);
        Log.d("ci1", String.valueOf(id1));
        context.startActivity(intent);
    }
});

并且您将在其他类

中获取这些包值
public class addmemo extends Activity

您可以将这些包值传递给memo.class并使用共享首选项,您可以将值传递给addmemo类。

相关问题