我希望在点击项目后从数据库中获取项目的ID,然后我想将此ID发送到另一个活动。
我收到错误:
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.myPackage.Place
该错误是由行引起的:
Place mPlace = (Place)(lv.getItemAtPosition(position));
当然,如果我在下面一行更改该行,则应用程序正在运行,但我在屏幕上显示单行名称而不是id。
String str = (String)(lv.getItemAtPosition(position));
这是我的完整代码。
public class VisitedPlaces extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
.
.
.
fillTheList();
displayResultList();
}
public void fillTheList(){
DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
List<Place> placesList = new ArrayList<>();
String sql = "SELECT _id, Name, Description, Latidute, Longtidute FROM Places WHERE Visited=1";
placesList = dbHelper.getAllPlaces(sql);
for (Place place : placesList){
results.add(place.getPlaceName());
}
}
public void displayResultList() {
ListView lv = getListView();
lv.addHeaderView(tvNew);
lv.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// String str = (String)(lv.getItemAtPosition(position));
Place mPlace = (Place)(lv.getItemAtPosition(position)); //HERE IS A PROBLEM
int mID = mPlace.getID();
Toast.makeText(getApplicationContext(),"Click ListItem: " + mID + ", " + id + ", " , Toast.LENGTH_LONG).show();
goToPlaceDescriptions(view, id);
}
});
}
public void goToPlaceDescriptions(View v, long id) {
Intent intent = new Intent(this, PlaceDescriptions.class);
intent.putExtra("id", id);
startActivity(intent);
}
}
somebode可以帮助我吗?
答案 0 :(得分:1)
没有施法,请执行此操作:
将placesList
设置为全局变量,然后编写此行以获取正确的Place
:
Place mPlace=placesList.get(position);
答案 1 :(得分:1)
问题是你使用ArrayAdapter。我认为从BaseAdapter扩展的自定义适配器将是更好的解决方案。
请查看以下代码:
自定义适配器类:
public class PlaceListAdapter extends BaseAdapter {
private List<Place> mPlaceList;
private Context mContext;
PlaceListAdapter(List<Place> placeList, Context c){
mPlaceList = new LinkedList<>(placeList);
mContext = c;
}
@Override
public int getCount() {
return mPlaceList.size();
}
@Override
public Place getItem(int i) {
return mPlaceList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//here inflate and return some view it can be text view
}
}
现在您的代码将如下所示
public class VisitedPlaces extends ListActivity {
private List<Place> results = new ArrayList<Place>();
protected void onCreate(Bundle savedInstanceState) {
.
.
.
fillTheList();
displayResultList();
}
public void fillTheList(){
DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
String sql = "SELECT _id, Name, Description, Latidute, Longtidute FROM Places WHERE Visited=1";
List<Place> placesList = dbHelper.getAllPlaces(sql);
for (Place place : placesList){
results.add(place);
}
}
public void displayResultList() {
ListView lv = getListView();
lv.addHeaderView(tvNew);
lv.setTextFilterEnabled(true);
setListAdapter(new PlaceListAdapter(results, this));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Place mPlace = (((PlaceListAdapter)parent.getAdapter()).getItem(position)); //HERE IS A PROBLEM
int mID = mPlace.getID();
Toast.makeText(getApplicationContext(),"Click ListItem: " + mID + ", " + id + ", " , Toast.LENGTH_LONG).show();
goToPlaceDescriptions(view, id);
}
});
}
public void goToPlaceDescriptions(View v, long id) {
Intent intent = new Intent(this, PlaceDescriptions.class);
intent.putExtra("id", id);
startActivity(intent);
}
}
我可以在代码中犯一些错误,但想法是最重要的。 请阅读有关自定义列表适配器的信息。