`我是android开发的新手。我正在使用消息应用程序...我已在“列表视图但相同的 ID/地址消息”中检索到所有消息,但我想将它们存储在列表中我应该怎么做..这是我的结果
package com.example.messaging;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class Adapter extends BaseAdapter {
long timeinms;
private ArrayList<SMS> _data;
Context _c;
Adapter (ArrayList<SMS> data, Context c){
this._data = data;
this._c = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
TextView fromView = (TextView)v.findViewById(R.id.address);
TextView subView = (TextView)v.findViewById(R.id.body);
// TextView descView = (TextView)v.findViewById(R.id.person);
// TextView timeView = (TextView)v.findViewById(R.id.date);
SMS msg = _data.get(position);
fromView.setText(msg.address);
subView.setText("Msg:"+(msg.body));
//timeView.setText(msg.date);
//descView.setText("From:"+msg.person);
return v;
}
}
package com.example.messaging;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Messaging extends Activity {
ListView msgList;
ArrayList<SMS> listAdapter;
String key_body;
String key_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
msgList = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayList<SMS>();
getSMS();
}
private List<String> getSMS() {
// TODO Auto-generated method stub
List<String> sms2 = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
SMS sms = new SMS();
try{
sms.setbody(c.getString(c.getColumnIndexOrThrow("body")).toString());
}
catch(Exception e){
Log.i("", "body"+e);
}
try{
sms.setaddress(c.getString(c.getColumnIndexOrThrow("address")).toString());
}
catch(Exception e){
Log.i("", "address"+e);
}
listAdapter.add(sms);
c.moveToNext();
}
}
@Override
public void onItemClick(AdapterView<?> view, View arg1, int position,
long arg3) {
Intent intent=new Intent(getApplicationContext(),Chat.class);
String address_key= "msg";
TextView addr=(TextView)findViewById(R.id.address);
String address=addr.getText().toString();
TextView bod=(TextView)findViewById(R.id.body);
String body=bod.getText().toString();
intent.putExtra(address_key, address);
String body_key="msgbody";
intent.putExtra(body_key, body);
startActivity(intent);
}
return sms2;
//
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.messaging, menu);
return true;
}}