数据在数据库中的活动中不可见

时间:2014-03-19 09:09:33

标签: android sqlite listview

我创建了一个应用程序,当我点击listview的项目时,它从数据库中检索数据并在另一个活动中显示..,当我试图这样做时,它显示的是coloumns名称,而不是下一个活动中的数据..

campaign_details

package com.example.smscampaign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;




import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Campaign_Details extends Activity {
    Cursor c;
    private int ACTIVITY_EDIT=1;
     SQLiteDatabase db;
    private ListView lvMessage;
    private Demo adapter;
    public String ADD_TO_NAME = "toname";
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_demostration);
        TextView txt1 = (TextView) findViewById(R.id.data);
        listview = (ListView) findViewById(R.id.listview);
        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        txt1.setText(pInfo.versionName);

        TextView txt = (TextView) findViewById(R.id.textnum1);

        // String [] values1= data.split("\n");
        // int t = values1.length;
        // txt.setText(Integer.toString(t));

        final DatabaseHelp info = new DatabaseHelp(this);
        info.open();

        ArrayList<String> arr = info.getData();
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < arr.size(); i++) {
            list.add(arr.get(i));
        }
        int t = arr.size();
        txt.setText(Integer.toString(t));
        adapter = new Demo(this, list);
        adapter.setNotifyOnChange(true);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {

                TextView tv = (TextView) view.findViewById(R.id.text1);
                String value = tv.getText().toString();
                c=info.fetchChildren(value);

            /*  Intent intent = new Intent(Campaign_Details.this, SmsSend.class);
                 intent.putExtra("key",value);
                startActivity(intent);*/

                Intent i = new Intent(Campaign_Details.this, SmsSend.class);
            //  i.putExtra(DatabaseHelp.KEY_ROWID, id);
                i.putExtra("key" ,info.KEY_NAME);
                i.putExtra("key1" ,info.KEY_SCALE);
                i.putExtra("key2", info.KEY_CONTACTS);
                startActivityForResult(i, ACTIVITY_EDIT);

            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.nextPage:
            Intent i = new Intent(Campaign_Details.this, SmsSend.class);
            startActivity(i);
            break;

        }
        return true;
    }

}

Databasehelp.java

package com.example.smscampaign;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Comment;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;



public class DatabaseHelp{

    public static final String KEY_ROWID="_id";
    public static final String KEY_NAME="person_name";
    public static final String KEY_SCALE="scale_person";
    public static final String KEY_CONTACTS="Contacts_person";

    private static final String DATABASE_NAME="Himani";
     static final String DATABASE_TABLE="peopleTable";
    private static final int DATABASE_VERSION=1;

    private DbHelper ourHepler;
    private final Context ourContext;
     SQLiteDatabase ourDatabase;
    public class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
                    KEY_NAME + " TEXT NOT NULL, " +
                    KEY_SCALE + " TEXT NOT NULL ,"  + KEY_CONTACTS + ")"
                    );
            }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
            onCreate(db);
        }

    }

    public DatabaseHelp(Context c){
        ourContext=c;
    }


    public DatabaseHelp open() throws SQLException{
        ourHepler = new DbHelper(ourContext);
        ourDatabase=  ourHepler.getWritableDatabase();
        return this;
    }
    public void close()
    {
        ourHepler.close();
    }


    public long entryCreate(String name, String scale , String contacts) {
        // TODO Auto-generated method stub

        ContentValues cv=new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_SCALE, scale);
        cv.put(KEY_CONTACTS, contacts);
        return ourDatabase.insert(DATABASE_TABLE, null, cv); 

    }


    public ArrayList<String> getData() {

        String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE, KEY_CONTACTS};
        Cursor c=  ourDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
        String run="";
        int iRow=c.getColumnIndex(KEY_ROWID);
        int iName=c.getColumnIndex(KEY_NAME);
        int iScale=c.getColumnIndex(KEY_SCALE);
        int iMessage=c.getColumnIndex(KEY_CONTACTS);
        ArrayList<String> newList= new ArrayList<String>();
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
         newList.add( c.getString(iName));
        }

        return newList;
    }

    public Cursor fetchChildren(String KEY_){
        Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
                               + " WHERE person_name = ?", new String[] {KEY_});
        return c;
    }



    public String getScale(long l) {
        // TODO Auto-generated method stub
        String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE , KEY_CONTACTS};
        Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);

        if(c != null){
            c.moveToFirst();
            String scale=c.getString(2);
            return scale;
        }
        return null;
    }


    public String getName(long l)  {
        // TODO Auto-generated method stub
        String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE , KEY_CONTACTS};
        Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);

        if(c != null){
            c.moveToFirst();
            String name=c.getString(1);
            return name;

        }
        return null;
    }
    public String getContacts(long l)  {
        // TODO Auto-generated method stub
        String[] col= new String[]{KEY_ROWID,KEY_NAME,KEY_SCALE ,KEY_CONTACTS};
        Cursor c= ourDatabase.query(DATABASE_TABLE, col,KEY_ROWID + "-" + l, null, null, null, null);

        if(c != null){
            c.moveToFirst();
            String contacts=c.getString(3);
            return contacts;

        }
        return null;
    }

    public  void updateEntry(long lt, String mName, String mScale ,String mContatcs) {
        // TODO Auto-generated method stub
          ContentValues cvUpdate=new ContentValues();   
          cvUpdate.put(KEY_NAME,mName);
          cvUpdate.put(KEY_SCALE,mScale);
          cvUpdate.put(KEY_CONTACTS,mContatcs );
          ourDatabase.update(DATABASE_TABLE,  cvUpdate, KEY_ROWID + "-" + lt, null);
    }


    public void deleteEntry(long ltt) throws SQLException{
        // TODO Auto-generated method stub
        ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + ltt,null);

    }
}

smsSend.java

package com.example.smscampaign;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.example.smscampaign.MainActivity.MyAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
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.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class SmsSend extends Activity implements OnClickListener {
    BroadcastReceiver smsSentReciver, smsSentDelivery;
    static EditText ed1, ed2;
    static int ResultCode = 12;
    static ArrayList<String> sendlist = new ArrayList<String>();
    Button b1, b2, b3, b4;
static  TextView txt;
    static StringBuilder conct = new StringBuilder();
    String contacts = "";
    String delim = ";";
    public static String Name;
    TextView ed;
    int i = 0;
    String[] cellArray;

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

        ed1 = (EditText) findViewById(R.id.editText1);
        ed2 = (EditText) findViewById(R.id.editText2);
        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(this);
        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(this);
        b3 = (Button) findViewById(R.id.button3);
        b3.setOnClickListener(this);
        b4 = (Button) findViewById(R.id.button4);
        b4.setOnClickListener(this);
        txt = (TextView) findViewById(R.id.textnum2);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        String value = getIntent().getStringExtra("key");
        String value1 = getIntent().getStringExtra("key1");
        String value2 = getIntent().getStringExtra("key2");
         ed1.setText(value);
         ed2.setText(value1);
         conct.append(value2);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(smsSentReciver);
        unregisterReceiver(smsSentDelivery);
    }

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

        smsSentReciver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "sms has been sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic Fail",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No Service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio Off",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;

                }
            }

        };
        smsSentDelivery = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "Sms Delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "Sms not Delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }

        };
        registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
        registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));

    }

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

        case R.id.button3:

            Intent a = new Intent(SmsSend.this, MainActivity.class);

            startActivityForResult(a, ResultCode);
            break;
        case R.id.button4:
            Intent file = new Intent(SmsSend.this, File_Selecter.class);
            startActivity(file);
            break;

        case R.id.button1:
            if (ed1.getText().toString().length() == 0) {
                ed1.setError("First name is required!");
            } else {
                boolean diditwork1 = true;

                try {
                    String Name = ed1.getText().toString();

                    SmsManager smsManager = SmsManager.getDefault();

                    String msg = ed2.getText().toString();

                    PendingIntent piSend = PendingIntent.getBroadcast(this, 0,
                            new Intent("SMS_SENT"), 0);
                    PendingIntent piDelivered = PendingIntent.getBroadcast(
                            this, 0, new Intent("SMS_DELIVERED"), 0);

                    Log.i("SMS", "contacts: " + contacts);

                    String[] cellArray;

                    contacts = conct.toString();

                    cellArray = contacts.split(";");

                    for (int a1 = 0; a1 < cellArray.length; a1++) {

                        // smsManager.sendTextMessage(cellArray[a1].toString(),
                        // null,
                        // msg, piSend, piDelivered);
                    }
                    DataBaseHandler entry = new DataBaseHandler(SmsSend.this);
                    entry.open();

                    entry.entryCreate(Name, msg , contacts);

                    entry.close();
                } catch (Exception e) {
                    diditwork1 = false;
                    String erroe = e.toString();

                    Dialog d = new Dialog(this);
                    d.setTitle("Dang it!");
                    TextView tv = new TextView(this);
                    tv.setText(erroe);
                    d.setContentView(tv);
                    d.show();

                } finally {
                    if (diditwork1) {
                        Dialog d = new Dialog(this);
                        d.setTitle("Heck Yeah!");
                        TextView tv = new TextView(this);
                        tv.setText("Success");
                        d.setContentView(tv);
                        d.show();
                    }
                }
                ed1.setText("");
                ed2.setText("");
                txt.setText("0");
                conct.delete(0, conct.length());
                break;

            }

        case R.id.button2:

            Log.i("SMS", "Sendlist Size: " + sendlist.size());
              String inputLine = "";
            if (ed1.getText().toString().length() == 0
                    || ed1.getText().toString().length() == 0
        || txt.getText().equals("0")) {
                ed1.setError("First name is required!");
                ed2.setError("Message and contacts are required!");
                txt.setError("Contacts required!");
            } else {
                boolean diditwork1 = true;

                try {
                    String Name = ed1.getText().toString();

                    SmsManager smsManager = SmsManager.getDefault();

                    String msg = ed2.getText().toString();

                    PendingIntent piSend = PendingIntent.getBroadcast(this, 0,
                            new Intent("SMS_SENT"), 0);
                    PendingIntent piDelivered = PendingIntent.getBroadcast(
                            this, 0, new Intent("SMS_DELIVERED"), 0);

                    Log.i("SMS", "contacts: " + contacts);



                    contacts = conct.toString();

                    cellArray = contacts.split(";");

                    for (int a1 = 0; a1 < cellArray.length; a1++) {

                        // smsManager.sendTextMessage(cellArray[a1].toString(),
                        // null,
                        // msg, piSend, piDelivered);

                    }
                    DatabaseHelp entry = new DatabaseHelp(SmsSend.this);
                    entry.open();

                    entry.entryCreate(Name, msg , contacts);

                    entry.close();

                } catch (Exception e) {
                    diditwork1 = false;
                    String erroe = e.toString();

                    Dialog d = new Dialog(this);
                    d.setTitle("Dang it!");
                    TextView tv = new TextView(this);
                    tv.setText(erroe);
                    d.setContentView(tv);
                    d.show();

                } finally {
                    if (diditwork1) {
                        Dialog d = new Dialog(this);
                        d.setTitle("Heck Yeah!");
                        TextView tv = new TextView(this);
                        tv.setText("Success");
                        d.setContentView(tv);
                        d.show();
                    }
                }
                ed1.setText("");
                ed2.setText("");
                txt.setText("0");
                conct.delete(0, conct.length());






                break;
            }
        }
    }


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

        if (requestCode == ResultCode) {

            if (resultCode == RESULT_OK) {
                // Intent t = getIntent();
                sendlist = data.getStringArrayListExtra("name");
                if (sendlist != null) {

                    for (int i = 0; i < sendlist.size(); i++) {
                        conct.append(sendlist.get(i).toString());
                        conct.append(delim);
                    }

                }

            }

            i = sendlist.size();
            txt.setText(Integer.toString(i));
            if (resultCode == RESULT_CANCELED) {

            }

        }
    }

}

1 个答案:

答案 0 :(得分:0)

i.putExtra("key" ,info.KEY_NAME);
i.putExtra("key1" ,info.KEY_SCALE);
i.putExtra("key2", info.KEY_CONTACTS);

我认为这是因为你传递的是静态变量而且它们永远不会改变。