我创建了一个应用程序,我从sqllite数据库中检索数据,但它只显示数据库中的第一条记录,而不显示其余的数据。我只想用rowid从数据库中检索名称,但它也向我显示了消息。 Campaign_Details.java
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;
import com.example.second.Scale;
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 implements OnClickListener {
Cursor c;
private DataBaseHelp mdb;
private ListView lvMessage;
private ArrayList<Bean> alSentMessage;
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);
/* mdb=new DataBaseHelp(this);
alSentMessage=new ArrayList<Bean>();
lvMessage=(ListView)findViewById(R.id.listview);
c=mdb.fetchAllContacts();
c.moveToFirst();
Log.d("cursor",""+c.getCount());
while(c.moveToNext()){
Bean mb=new Bean();
Log.e("ColumnIndex",""+c.getColumnIndex(DataBaseHelp.KEY_NAME));
mb.setToName((c.getString(c.getColumnIndex(DataBaseHelp.KEY_NAME))));
alSentMessage.add(mb);
}
adapter=new Demo(Campaign_Details.this,alSentMessage);
lvMessage.setAdapter(adapter);*/
// String [] values1= data.split("\n");
// int t = values1.length;
// txt.setText(Integer.toString(t));
// String[] values = new String[] { data };
DataBaseHelp info= new DataBaseHelp(this);
info.open();
//ArrayList<String> list1 = new ArrayList<String>();
//list1= info.getData();
//String[] values= list1.;
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));
}
adapter = new Demo(this, list);
// Demo.setNotifyOnChange(true);
listview.setAdapter(adapter);
listview.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent("com.example.second.SMSSEND");
String mName= SmsSend.ed1.getText().toString();
String mScale=SmsSend.ed2.getText().toString();
String mContatcs= SmsSend.conct.toString() ;
String st= information.getText().toString();
long lt= Long.parseLong(st);
DataBaseHelp e= new DataBaseHelp(this);
e.open();
e.updateEntry(lt,mName,mScale,mContatcs);
e.close();
startActivity(i);
}
@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;
}
}
activity_list_demostration.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="3" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textcolour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/green_circle" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:text="Active Campaign"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4AE56B" />
<TextView
android:id="@+id/textnum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="130dp"
android:background="@drawable/green_badge"
android:gravity="center"
android:text=" 0 "
android:textColor="@color/white" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="240dp"
android:orientation="vertical" >
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
>
</TextView>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black" />
</ScrollView>
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="3" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textcolour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/grey_circle" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Closed Campaign"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textnum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="130dp"
android:background="@drawable/grey_badge"
android:gravity="center"
android:text=" 0 "
android:textColor="@color/white" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="140dp"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black" />
</ScrollView>
</LinearLayout>
</LinearLayout>
Demo.java
package com.example.smscampaign;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Demo extends ArrayAdapter<String>{
private ArrayList<String> values=null;
private final Context context;
public Demo(Context context, ArrayList<String> values) {
super(context,R.layout.list, values);
this.context=context;
this.values= values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowview= inflater.inflate(R.layout.list, parent,false);
TextView txt=(TextView) rowview.findViewById (R.id.text1);
//ImageView img=(ImageView) rowview.findViewById (R.id.icon);
txt.setText(values.get(position));
//changing icon
return rowview;
}
}
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
DataBaseHandler.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(iRow)+ " " + c.getString(iName) + " " + c.getString(iScale) + " " + c.getString(iName));
}
return newList;
}
public Cursor fetchAllContacts()
{
ourDatabase=ourHepler.getReadableDatabase();
String selectQuery = "SELECT * FROM " + DATABASE_TABLE;
return ourDatabase.rawQuery(selectQuery, null);
}
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;
TextView txt;
static StringBuilder conct = new StringBuilder();
String contacts = "";
String delim = ";";
public static String Name;
TextView ed;
int i = 0;
String[] cellArray;
DataBaseHelp notasdb = new DataBaseHelp(this);
@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);
}
@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) {
}
}
}
}
答案 0 :(得分:3)
我看错了一些错误
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black" />
</ScrollView>
ScrollView
ListView
引用文档
你永远不应该使用带有ListView的ScrollView,因为ListView 负责自己的垂直滚动。最重要的是,这样做 击败ListView中的所有重要优化以进行处理 使用大型列表,因为它有效地强制ListView显示 它的整个项目列表,用于填充由提供的无限容器 滚动型。
填充listview的基础数据有一条记录,我猜你会看到1条记录。
编辑:
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();
Intent intent = new Intent(MainActivity.thism,SecondActivity.class);
intent.putExtra("key",value);
startActivity(intent);
}
});
进入SecondActivity
String value = getInent().getStringExtra("key");