我的活动有一个列表视图,其中填充了我的parse.com数据库中的数据,列表确实填充没有问题。它显示名称列表。我想将列表中所选项目的名称存储在变量中并传递给另一个活动。列表会被填充,但选择错误会导致应用程序崩溃。继承人我的活动`
import java.util.List;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseQueryAdapter.OnQueryLoadListener;
public class ResterauntList extends ActionBarActivity {
String mValue;
ArrayAdapter<String> adapter;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resteraunt_list);
Bundle bdl = getIntent().getExtras();
mValue = bdl.getString("Value");
setContentView(R.layout.activity_resteraunt_list);
populateList(mValue);
}
private void populateList(final String Value) {
ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ParseQuery create() {
ParseQuery query = new ParseQuery("Restraunt");
query.whereEqualTo("Location", Value);
return query;
}
};
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
this, factory);
adapter.setTextKey("Name");
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
public void onLoading() {
mProgressDialog = new ProgressDialog(ResterauntList.this);
mProgressDialog.setTitle(Value + " Restraunts Search");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
public void onLoaded(List<ParseObject> objects, Exception e) {
mProgressDialog.dismiss();
}
});
final ListView listView = (ListView) findViewById(R.id.restListView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = (String) listView.getItemAtPosition(position);
Intent i = new Intent(getApplicationContext(),
SingleRestraunt.class);
i.putExtra("restName", name);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.resteraunt_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}`
此活动的我的xml如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gastronomaapp.ResterauntList" >
<ListView
android:id="@+id/restListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" >
</ListView>
</RelativeLayout>
我似乎不断收到此错误
08-31 03:00:42.490: E/AndroidRuntime(2003): Process: com.example.gastronomaapp, PID: 2003
08-31 03:00:42.490: E/AndroidRuntime(2003): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
检查了许多解决方案并尝试了几乎我能在网上找到的所有内容。绝对没有任何线索错误。非常感谢任何帮助 编辑 - 堆栈跟踪,告诉我错误是在这一行。有点有直觉
String name = (String) listView.getItemAtPosition(position);
Intent i = new Intent(getApplicationContext(),
SingleRestraunt.class);
答案 0 :(得分:1)
错误发生在以下行
String name = (String) listView.getItemAtPosition(position);
您正在使用
ParseQueryAdapter<ParseObject> adapter
的内容
ArrayAdapter<String> adapter
因此您无法将其强制转换为字符串。尝试使用以下代码
ParseObject object= (ParseObject) listView.getItemAtPosition(position);
然后检查 ParseObject 的属性在putExtra中需要什么