Sms application - List_item error
Hello i have a problem with this aplication.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.开发者_JS百科net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Smsfive extends Activity {
/** Called when the activity is first created.
* @param messages */
public void onCreate(Bundle savedInstanceState, ListView messages) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
List<String> msgList = getSMS();
for(int i = 0; i<msgList.size(); i++) {
System.out.println(msgList.get(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.list, msgList); --> there is error with list_item
//ListView messages;
messages.setAdapter(adapter);
messages.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SmsManager m = SmsManager.getDefault();
TextView view;
String destinationNumber=(String) ((TextView) view).getText();
m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);
}
});
}
public List<String> getSMS() {
List<String> messages = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = null;
try{
c = getApplicationContext().getContentResolver().query (uri, null, null ,null,null);
}catch(Exception e){
e.printStackTrace();
}
try{
for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
final String address = c.getString(c.getColumnIndex("address"));
final String body = c.getString(c.getColumnIndexOrThrow("body"));
messages.add("Number: " + address + ". Message: " + body);
}
}catch(Exception e){
e.printStackTrace();
}
c.close();
return messages;
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
Error:
Description Resource Path Location Type list_item cannot be resolved or is not a field Smsfive.java /Sms5/src/sms/five line 30 Java Problem
What can i do please?
Definitely either you are missing name of the file or even file list_item.xml
. So Insure that this layout file must be exist in your res/layout
directory.
You are not including the correct R file. When you create a resource, like list_item, an identifier for it is added to the generated R file. This R file is located in the package of your application. There is also an Android R file, which is in a different package.
Make sure you have an import statement the imports the correct R file.
What is the name of your xml file? (Hint: it has to be list_item for R.layout.list_item to work)
Simply do this:
setContentView(R.layout.list_item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, msgList);
What you are probably trying to do is have a custom TextView
in your ListView
but it won't work like this.
精彩评论