Java Listview未显示数据
我正在做一个项目,但它没有工作好,所以为了让我的头脑清楚,我做了另一个项目,只是简单地检查它是否工作,但我的listview没有显示数据,我从这个网站上得到的代码每件事都是完全一样的,但数据没有显示在列表中。拜托,我被困在这里了,我真的需要一些快速的帮助。等待一些积极的回应 //主代码Java Listview未显示数据,java,android,listview,parse-platform,Java,Android,Listview,Parse Platform,我正在做一个项目,但它没有工作好,所以为了让我的头脑清楚,我做了另一个项目,只是简单地检查它是否工作,但我的listview没有显示数据,我从这个网站上得到的代码每件事都是完全一样的,但数据没有显示在列表中。拜托,我被困在这里了,我真的需要一些快速的帮助。等待一些积极的回应 //主代码 public class MainActivity extends Activity { // Declare Variables ListView listview; List<ParseObject&g
public class MainActivity extends Activity {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.main);
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Country");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into an ArrayAdapter
adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.single_item);
// Retrieve object "name" from Parse.com database
for (ParseObject country : ob) {
adapter.add((String) country.get("name"));
}
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(MainActivity.this,
SingleItemView.class);
// Pass data "name" followed by the position
i.putExtra("name", ob.get(position).getString("name")
.toString());
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}
}
//main的xml文件
<?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="fill_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
//用于单个项目显示
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" >
虽然您从其他地方复制了代码,但仍需要提供当前代码和日志。如果您尝试调试代码,请在
适配器附近添加((字符串)country.get(“name”)代码>适配器何时添加数据?刚刚添加了数据,但我不明白,因为我是初学者!我调试它,但它停止!运行程序时,logcat
中是否有任何错误?请张贴它,如果你有任何没有任何错误,它的工作正常,但没有数据显示在listview。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" >