如何从列表视图中提取内容并将其存储为字符串?
How do i extract content from a listview and store it as a string?
我的应用程序从 phone 的收件箱中读取消息并将其显示在应用程序的列表视图中。我正在尝试提取消息的内容本身并将其存储为字符串以供将来使用。我该怎么做?当我检查 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>
有了 Cursor 对象后,您就可以从中检索数据。请参阅 this
从您的 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,);
}
});
}
我的应用程序从 phone 的收件箱中读取消息并将其显示在应用程序的列表视图中。我正在尝试提取消息的内容本身并将其存储为字符串以供将来使用。我该怎么做?当我检查 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>
有了 Cursor 对象后,您就可以从中检索数据。请参阅 this
从您的 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,);
}
});
}