Android 如何从listview中提取内容并将其存储为字符串?
我的应用程序读取手机收件箱中的信息,并将其显示在应用程序的列表视图中。我试图提取消息的内容本身,并将其存储为字符串以供将来使用。我该怎么办?当我检查log cat时,它告诉我列表视图中的内容一开始甚至不是字符串,所以我还不能提取它。我需要先将内容转换为字符串吗?如果是,我怎么做 这是我当前的代码,我是如何将收件箱中的消息提取并显示到应用程序的:Android 如何从listview中提取内容并将其存储为字符串?,android,string,listview,sms,Android,String,Listview,Sms,我的应用程序读取手机收件箱中的信息,并将其显示在应用程序的列表视图中。我试图提取消息的内容本身,并将其存储为字符串以供将来使用。我该怎么办?当我检查log cat时,它告诉我列表视图中的内容一开始甚至不是字符串,所以我还不能提取它。我需要先将内容转换为字符串吗?如果是,我怎么做 这是我当前的代码,我是如何将收件箱中的消息提取并显示到应用程序的: public class MainActivity extends AppCompatActivity implements OnClickListen
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>
一旦有了游标对象,就可以从中检索数据。请参见从列表视图中获取所选项目的文本,如下所示
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,);
}
});
}
你试过parent.getItemAtPositionposition.toString吗?是的,这是我代码的一部分