UserIndex:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users_index);
listView=(ListView)findViewById(R.id.listview);
getdata();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView uname=(TextView)view.findViewById(R.id.sendername);
username = uname.getText().toString();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("username", username);
startActivity(i);
}
});
}
private void getdata() {
try {
File fXmlFile = new File("/data/data/net.multiplesystem.nosms/Messeges/UserList.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("details");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String a=eElement.getElementsByTagName("name").item(0).getTextContent().toString();
userIndex.add(a);
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setListAdapter();
}
private void setListAdapter(){
adapter = new ArrayAdapter<String>(this,R.layout.user_index_layout,R.id.sendername,userIndex);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listview) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
String userdetail=((TextView) info.targetView).getText().toString();
try {
File fXmlFile = new File("/data/data/net.multiplesystem.nosms/Messeges/"+userdetail+".xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fXmlFile);
if(fXmlFile.exists()){
fXmlFile.delete();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
adapter.remove(adapter.getItem(info.position));
return super.onContextItemSelected(item);
}
}
user_index_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/sendername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#343434"
android:textSize="10dip" />
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/sendername"
android:layout_marginRight="5dip"
android:gravity="right"
android:textColor="#10bcc9"
android:textSize="10dip"
android:textStyle="bold" />
</RelativeLayout>
logcat的:
02-20 03:17:59.623: E/AndroidRuntime(3561): FATAL EXCEPTION: main
02-20 03:17:59.623: E/AndroidRuntime(3561): Process: net.multiplesystem.nosms, PID: 3561
02-20 03:17:59.623: E/AndroidRuntime(3561): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
02-20 03:17:59.623: E/AndroidRuntime(3561): at net.multiplesystem.nosms.messegebox.UserIndex.onContextItemSelected(UserIndex.java:153)
02-20 03:17:59.623: E/AndroidRuntime(3561): at android.app.Activity.onMenuItemSelected(Activity.java:2619)
02-20 03:17:59.623: E/AndroidRuntime(3561): at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3808)
02-20 03:17:59.623: E/AndroidRuntime(3561): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-20 03:17:59.623: E/AndroidRuntime(3561): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
基本上在加载时我正在读取XML文件中包含用户列表的数据并将其显示到listview中。当我点击列表项时,它会打开用户详细信息.Listview也有contextMenu(即删除选项),但是当我选择上下文菜单项时,它会抛出类强制转换异常。我对此并不太了解。请指导我。