我的应用会从手机的收件箱中读取消息,并将其显示在应用中的列表视图中。我正在尝试提取消息的内容本身并将其存储为字符串以供将来使用。我该怎么做?当我检查log cat时,它告诉我列表视图中的内容首先不是字符串,所以我还不能提取它。我需要先将内容转换为字符串吗?如果是的话,我该怎么做?
这是我当前的代码,我如何从收件箱中提取并显示消息到应用程序:
public class MainActivity extends AppCompatActivity implements OnClickListener {
// GUI Widget
Button getSmsButton, viewGoogleMapButton;
TextView lblMsg, lblNumber;
ListView messageListView;
Context ctx = this;
String msg;
// Cursor Adapter
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init GUI Widget
getSmsButton = (Button) findViewById(R.id.getSmsButton);
getSmsButton.setOnClickListener(this);
messageListView = (ListView) findViewById(R.id.messageListView);
viewGoogleMapButton = (Button) findViewById(R.id.viewGoogleMapButton);
viewGoogleMapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent startGoogleMap = new Intent(ctx, MapsActivity.class);
startActivity(startGoogleMap);
}
});
}
@Override
public void onClick(View v) {
if (v != getSmsButton)
return;
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
messageListView.setAdapter(adapter);
}
}
我尝试将这些代码用于提取内容并以字符串形式存储但不起作用
messageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data=(String)parent.getItemAtPosition(position);
Log.i("msg", data);
}});
}
这是我的row.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblMsg"></TextView>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f"
android:id="@+id/lblNumber"></TextView>
答案 0 :(得分:0)
从ListView获取所选项目的文本,如下所示
messageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//for getting the position
String data=messageListView.getItemAtPosition(position).toString();
//for getting the text from selected item
TextView textView = (TextView) view.findViewById(R.id.lblMsg); //textView in your listView
String text = textView.getText().toString();
Log.i("msg", "Selected item: " + text + " - " + position,);
}
});
}
答案 1 :(得分:-1)
一旦有了Cursor对象,就可以从中检索数据。请参阅this