如何为自定义ListView项设置SMS Pending Intent

时间:2015-06-27 04:04:21

标签: android xml listview android-listview android-pendingintent

我有一个自定义ListView,其中的哪些项目是CardView。我已经用SQLite数据库填充了它们。现在我已向每个人发送SMS。我做到了没有任何问题。现在我必须为每个人设置PendingIntent。我不知道如何实现这一目标。我在这里提供了我的代码。有人请帮忙。

  

我的活动是

public class TableBikeActivity extends ActionBarActivity {

private DisplayAdapter adapter;
private SearchDisplayAdapter adapter1;
ListView bikeList;
DatabaseHelper databaseHelper;
EditText myFilter;
ImageButton search,imgSendStatus;
RadioGroup radioFilter;
SmsManager smsManagerBike;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_table_bike);

    databaseHelper=new DatabaseHelper(this);
    bikeList=(ListView)findViewById(R.id.listView2);

    bikeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView textSrNo = (TextView) view.findViewById(R.id.textView29);
            String srNo = textSrNo.getText().toString();

            String selectQuery = "select * from bikeTable where uSerialNo='" + srNo + "'";
            SQLiteDatabase db = databaseHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            cursor.moveToFirst();
            String strUser = cursor.getString(1);
            String strMobile = cursor.getString(2);
            String strAddress = cursor.getString(3);
            String strSerialNo = cursor.getString(4);
            String strImei = cursor.getString(5);
            String strCimi = cursor.getString(6);
            String strDeviceNo = cursor.getString(7);
            String strDealerCode = cursor.getString(8);
            String strDealerContact = cursor.getString(9);

            cursor.close();

            String strTest = strUser + "#" + strMobile + "#" + strAddress + "#" + strSerialNo + "#" + strImei + "#" + strCimi + "#" + strDeviceNo + "#" + strDealerCode + "#" + strDealerContact + "#";

            Bundle bundle = new Bundle();
            bundle.putString("details", strTest);
            Intent bikeIntent = new Intent(TableBikeActivity.this, BikeReport.class);
            bikeIntent.putExtras(bundle);
            startActivity(bikeIntent);
        }
    });

    int[] colors = { Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3") };
    bikeList.setDivider(new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, colors));

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getAllDataBike());
            bikeList.setAdapter(adapter);
        }
    });

    search=(ImageButton)findViewById(R.id.imageView13);

    myFilter = (EditText) findViewById(R.id.editText2);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String strFilter = myFilter.getText().toString();

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    if (strFilter.equals("")) {
                        Toast.makeText(TableBikeActivity.this,"Please Enter Valid Serial No.",Toast.LENGTH_SHORT).show();
                        adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getAllDataBike());
                        bikeList.setAdapter(adapter);
                    } else {
                        adapter1 = new SearchDisplayAdapter(TableBikeActivity.this, databaseHelper.searchCustomerBike(strFilter));
                        bikeList.setAdapter(adapter1);
                    }
                }
            });
        }
    });

    myFilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            final String strFilter = myFilter.getText().toString();

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    if (strFilter.equals("")) {
                        adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getAllDataBike());
                        bikeList.setAdapter(adapter);
                    } else {
                        adapter1 = new SearchDisplayAdapter(TableBikeActivity.this, databaseHelper.searchCustomerBike(strFilter));
                        bikeList.setAdapter(adapter1);
                    }
                }
            });
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    radioFilter=(RadioGroup)findViewById(R.id.radioFilter);

    radioFilter.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId==R.id.radioAll){

                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getAllDataBike());
                        bikeList.setAdapter(adapter);
                    }
                });

            }else if (checkedId==R.id.radioActivated){

                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getActivated());
                        bikeList.setAdapter(adapter);
                    }
                });

            }else if (checkedId==R.id.radioNotActivated){

                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new DisplayAdapter(TableBikeActivity.this, databaseHelper.getNotActivated());
                        bikeList.setAdapter(adapter);
                    }
                });
            }
        }
    });

    smsManagerBike=SmsManager.getDefault();
    imgSendStatus=(ImageButton)findViewById(R.id.imageButton3);

    imgSendStatus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SQLiteDatabase db=databaseHelper.getReadableDatabase();
            String query="select uContactNo from bikeTable WHERE mobileNo IS NOT NULL AND vehicleType='Two Wheeler'";
            Cursor statusBikeCursor=db.rawQuery(query,null);

            if (statusBikeCursor.getCount()>0) {

                while (statusBikeCursor.moveToNext()) {
                    String deviceNo = statusBikeCursor.getString(statusBikeCursor.getColumnIndex("uContactNo"));

                    smsManagerBike.sendTextMessage(deviceNo, null, "?", null, null);

                }
            }else {
                Toast.makeText(TableBikeActivity.this,"No Device is Activated",Toast.LENGTH_SHORT).show();
            }
            statusBikeCursor.close();
        }
    });

}
  

自定义ListView的DisplayAdapter是

public class DisplayAdapter extends CursorAdapter{

@SuppressWarnings("deprecation")
public DisplayAdapter(Context context,Cursor c){
    super(context,c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.card_items, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvUserName=(TextView)view.findViewById(R.id.textView27);
    tvUserName.setText(cursor.getString(2));

    TextView tvMobile=(TextView)view.findViewById(R.id.textView28);
    tvMobile.setText(cursor.getString(3));

    TextView tvSerail=(TextView)view.findViewById(R.id.textView29);
    tvSerail.setText(cursor.getString(5));
}
  

我的DatabaseHelper是

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String db_name="unitdb";
public static final int version=1;
Context context;

public DatabaseHelper(Context context){
    super(context, db_name, null, version);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db){

    db.execSQL("CREATE TABLE bikeTable( _id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, mobileNo INTEGER, address TEXT, uSerialNo INTEGER UNIQUE, uImeiNo integer, uCimiNo INTEGER, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER, vehicleType TEXT)");
    Toast.makeText(context,"Database Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate", "Database Created");
    Log.i("Table Created", "Bike Tracker Table Created");

    db.execSQL("CREATE TABLE dealerTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, dealerName TEXT, mobileNo INTEGER, dealerCode INTEGER UNIQUE, address TEXT)");
    Log.i("Table Created", "Dealer Details Table Created");

    db.execSQL("CREATE TABLE statusTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, mobileNo INTEGER, serialNo INTEGER, deviceContact INTEGER, sentTime TEXT )");
    Log.i("Table Status Created","Status Table Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion>=newVersion)

        return;

    if (oldVersion==1){

        Log.d("New Version","Data's can be Upgraded");

    }

    Log.d("Sample Data", "onUpgrade:" + newVersion);

}

public Cursor getAllDataBike(){

        String query= "select rowid _id,* from bikeTable where vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getAllDataCar(){

    String query= "select rowid _id,* from bikeTable where vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getAllDataCv(){

    String query= "select rowid _id,* from bikeTable where vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getDealers(){

    String query="select rowid _id,* from dealerTable";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerBike(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerCar(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerCv(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivated(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivated(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivatedCar(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivatedCar(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivatedCv(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivatedCv(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

}

  

列表视图项的XML是

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_margin="1dp"
    card_view:cardCornerRadius="0dp"
    card_view:contentPadding="2dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/card_style">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Username"
            android:id="@+id/textView24"
            android:textColor="#FFFFFF"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_marginTop="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mobile No."
            android:id="@+id/textView25"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_alignRight="@+id/textView24"
            android:layout_alignEnd="@+id/textView24" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Serial No."
            android:id="@+id/textView26"
            android:textColor="#FFFFFF"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView27"
            android:textColor="#FFFFFF"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView28"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/textView27"
            android:layout_alignStart="@+id/textView27" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView29"
            android:layout_alignTop="@+id/textView26"
            android:layout_alignLeft="@+id/textView28"
            android:layout_alignStart="@+id/textView28"
            android:layout_marginBottom="8dp"
            android:textColor="#FFFFFF"/>

        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="ON"
            android:textColor="#ffffff"
            android:id="@+id/btnStatusON"
            android:focusable="false"
            android:background="@drawable/btn_report"
            android:layout_marginEnd="50dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="30dp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

0 个答案:

没有答案