我正在尝试使用Parse.com获取餐馆名称并将其放入listView的代码。 当您单击其中一个名称时,它会将您带到详细信息页面,您可以在其中获取有关餐厅的详细信息。 问题是,当我点击ListView上的名称时,它会转到详细信息页面但不显示任何内容。 我是android和解析的新手,并不完全理解它应该如何工作。
这是我的mainActivity中的代码
public class MainActivity extends Activity {
private CustomAdapter urgentTodosAdapter;
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView listView;
List<ParseObject> Name =new ArrayList<ParseObject>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
mainAdapter = new ParseQueryAdapter<ParseObject>(this,"RestaurantDetail");
urgentTodosAdapter = new CustomAdapter(this);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
// Binds the Adapter to the ListView
listView.setAdapter(urgentTodosAdapter);
// Capture button clicks on ListView items
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Send single item click data to SingleItemView Class
Intent random1 = new Intent(MainActivity.this, SingleItemView.class);
if (Name.size() > i)
// Pass data "name" followed by the position
random1.putExtra("Name", Name.get(i).getString("Name"));
// Open SingleItemView.java Activity
startActivity(random1);
}
});
}
}
和我的customAdapter类
public class CustomAdapter extends ParseQueryAdapter<ParseObject> {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("RestaurantDetail");
/* query.whereEqualTo("Name", true);*/
return query;
}
});
}
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.activity_main, null);
}
super.getItemView(object, v, parent);
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("Logo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView3);
titleTextView.setText(object.getString("Name"));
return v;
}
}
以及想要显示餐馆名称的类等。
@ParseClassName("RestaurantDetail")
public class SingleItemView extends Activity {
// Declare Variables
protected TextView txtname;
String Name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from SingleItemView.xml
setContentView(R.layout.single_item_view);
txtname = (TextView) findViewById(R.id.name);
// Retrieve data from MainActivity on item click event
Intent random1 = getIntent();
// Get the name
Name = random1.getStringExtra("Name");
// Locate the TextView in singleitemview.xml
// Load the text into the TextView
txtname.setText(Name);
}
}
答案 0 :(得分:1)
请在OP的评论部分查看我的评论以获取一些背景信息:
答案:
在您的MainActivity中,您没有创建要添加到适配器的查询。看看下面,根据您的需要调整ParseQuery:
MainActivity Class
public class MainActivity extends Activity {
private CustomAdapter urgentTodosAdapter;
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView listView;
List<ParseObject> Name =new ArrayList<ParseObject>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.listView1);
//I'm not sure how you initialized your custom adapter - i'll show you how i did it, but yours might work too
urgentTodosAdapter = new urgentTodosAdapter(MainActivity.this, new ArrayList<RestaurantDetail>());
listView.setAdapter(urgentTodosAdapter);
//Here is where you forgot to add your ParseQuery for 'Restaurants'
ParseQuery<RestaurantDetail> query = ParseQuery.getQuery(RestaurantDetail.class);
query.findInBackground(new FindCallback<RestaurantDetail>() {
public void done(List<RestaurantDetail> resutarantList, ParseException e) {
if (e == null) {
if (resutarantList.size() > 0) {
for (int i = 0; i < resutarantList.size(); i++) {
//you forgot to do this - didn't add anything for the adapter to show.
urgentTodosAdapter.add(resutarantList.get(i));
}
} else {
// do something if empty
}
} else {
// do something if null
}
}
});
//This is fine here, but you can also do the same in your adapter class; I'll keep this here and show you where to put it
//in the adapter as well.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Send single item click data to SingleItemView Class
Intent random1 = new Intent(MainActivity.this, SingleItemView.class);
if (Name.size() > i)
// Pass data "name" followed by the position
random1.putExtra("Name", Name.get(i).getString("Name"));
// Open SingleItemView.java Activity
startActivity(random1);
}
});
}
}
CustomAdapter类:
public class CustomAdapter extends ArrayAdapter<RestaurantDetail> {
private final Context mContext;
private List<RestaurantDetail> restrauants;
public CustomAdapter(Context context, List<RestaurantDetail> objects) {
super(context, R.layout.include_restaurants, objects);
this.mContext = context;
this.restrauants = objects;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
v = mLayoutInflater.inflate(R.layout.include_restaurants, null);
}
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("Logo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView3);
titleTextView.setText(object.getString("Name"));
//this is also where you can handle to onClick stuff
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return v;
}
}
初始化你的解析类:这也是你可以放置你的getter / setter的地方。
@ParseClassName("YourClassNameInYourDBHere")
public class ResuaurantDetail(your class here) extends ParseObject
{
}