我研究了这个,但没有什么能与我想要的相提并论。我有一个类型的食谱列表,如果我点击其中一个类别(开胃菜),它会将我带入另一个列表:列表视图,其中包含一个开胃菜列表。如果我点击列表中的其中一个项目,它应该带我到一个活动,其中包含图像,标题,标题下方的烹饪时间以及图像和文本视图下方的食谱本身。
编辑以显示查询解析的代码:
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Appetizers");
query.addAscendingOrder("appetizer");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> recipes, ParseException e) {
if (e == null) {
// success
} else {
// error
}
我该怎么做?
开胃菜清单:
的活动:
编辑:
AppetizerAdapter:
public class AppetizerAdapter extends ArrayAdapter {
protected Context myContext;
protected List myAppetizer;
public AppetizerAdapter(Context context, List myappetizer) {
super(context, R.layout.custom_appetizer, myappetizer);
myContext = context;
myAppetizer = myappetizer;
}
// inflates each row of the app
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// initialize viewholder
ViewHolder holder;
if (convertView == null) { // If no items in the view to be displayed
convertView = LayoutInflater.from(myContext).inflate(
R.layout.custom_appetizer, null);
// initialize the views
holder = new ViewHolder(); // creates a new view
holder.appetizerTextView = (TextView) convertView // calls the view
.findViewById(R.id.appetizer);
holder.appetizerImageView = (ImageView) convertView.findViewById(R.id.appetizerImage);
holder.cookTimeTextView = (TextView) convertView.findViewById(R.id.cookTime);
// call the view and setTag to the parameter (holder)
convertView.setTag(holder);
} else { // if view is previously displayed
// initialize holder
holder = (ViewHolder) convertView.getTag();
}
// get the position of the row
ParseObject appObject = (ParseObject) myAppetizer.get(position);
// Title and Cook Time
String app = appObject.getString("appetizer"); // column passing
holder.appetizerTextView.setText(app);
String cook = appObject.getString("cookTime");
holder.cookTimeTextView.setText(cook);
// image
Picasso.with(getContext()).load(appObject.getParseFile("imageFiles").getUrl())
.into(holder.appetizerImageView);
return convertView; // return the view
}
public static class ViewHolder {
// declaration of variables
TextView appetizerTextView;
TextView cookTimeTextView;
ImageView appetizerImageView;
}
}
Appetizer.java
public class Appetizer extends ListActivity {
protected List<ParseObject> mAppetizers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appetizer);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Appetizers");
query.addAscendingOrder("appetizer");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> appetizer, ParseException e) {
if (e == null) {
// success
mAppetizers = appetizer;
AppetizerAdapter adapter = new AppetizerAdapter(getListView().getContext(),
mAppetizers);
setListAdapter(adapter);
} else {
// there is a problem
AlertDialog.Builder builder = new AlertDialog.Builder(Appetizer.this);
builder.setMessage(e.getMessage());
builder.setTitle("Sorry");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the dialog
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_directory, 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);
}
// click on a row item
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(Appetizer.this, AppetizerRecipe.class);
startActivity(intent);
}
}
我有一个名为content_appetizer_recipe的布局,其中包含带有图像的pic中的布局,以及3个textviews(不是列表)。 AppetizerRecipe是一个AppCompatActivity。
希望这有助于解决我的问题。
答案 0 :(得分:3)
假设您有一个Recipe
类和一些ArrayAdapter<Recipe>
实现,您应该能够遍历Parse返回给您的值列表并使用适当的方法从{ {1}}。
ArrayAdapter负责在TextViews和ImageViews中显示正确的数据。
ParseObject
如果你没有这个ListView lv = (ListView) findViewById(R.id.listView); // TODO
List<Recipe> recipeList = new ArrayList<Recipe>();
final RecipeAdapter adapter = new RecipeAdapter(RecipeActivity.this, recipeList);
lv.setAdapter(adapter);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Appetizers");
query.addAscendingOrder("appetizer");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> recipes, ParseException e) {
if (e == null) {
for (ParseObject po : recipes) {
Recipe r = new Recipe();
// TODO: Retrieve fields from the ParseObject
r.setName(po.get...);
r.setTime(po.get...);
r.setImageUrl(po.get...);
adapter.add(r);
}
} else {
// error
}
});
类,那么只需像你一样将Recipe
加载到适配器中,然后在List<ParseObject>
方法中,你可以从中获取一个特定的对象。该列表使用onListItemClick
参数。
在这里,您还需要position
中public static final String ARG_NAME = "name";
的字段来保持您的参数一致。有关详细信息,请参阅Passing data between Activities。您还需要从AppetizerRecipe.java
内的Intent获取数据。
AppetizerRecipe.java