Ich hab ein Problem mit meinem ListView. Ich brauche zwei Listen zwischen denen man beliebig Einträge Hin und Her schieben kann. Problem ist:
Ich würde gerne ein bestimmtes Layout verwenden und kann somit nicht den standart Array Adapter verwenden.
Nun brauch ich ja aber den remove bzw den add befehl und wollte somit fragen wie ich den in den Custom Adapter integriere.
Hier mein Custom ListView Code:
MainActivity.java:
Code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.recipe_list_view);
final ArrayList<Recipe> recipeList = Recipe.getRecipesFromFile("recipes.json", this);
String[] listItems = new String[recipeList.size()];
for(int i = 0; i < recipeList.size(); i++){
Recipe recipe = recipeList.get(i);
listItems[i] = recipe.title;
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems);
mListView.setAdapter(adapter);
final Context context = this;
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 1
Recipe selectedRecipe = recipeList.get(position);
// 2
Intent detailIntent = new Intent(context, RecipeDetailActivity.class);
// 3
detailIntent.putExtra("title", selectedRecipe.title);
detailIntent.putExtra("url", selectedRecipe.instructionUrl);
// 4
startActivity(detailIntent);
}
});
};
private ListView mListView;
}
Alles anzeigen
RecipeAdapter.java:
Code
public class RecipeAdapter extends BaseAdapter {
private static final HashMap<String, Integer> LABEL_COLORS = new HashMap<String, Integer>() {{
put("Low-Carb", R.color.colorLowCarb);
put("Low-Fat", R.color.colorLowFat);
put("Low-Sodium", R.color.colorLowSodium);
put("Medium-Carb", R.color.colorMediumCarb);
put("Vegetarian", R.color.colorVegetarian);
put("Balanced", R.color.colorBalanced);
}};
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<Recipe> mDataSource;
public RecipeAdapter(Context context, ArrayList<Recipe> items) {
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//1
@Override
public int getCount() {
return mDataSource.size();
}
//2
@Override
public Object getItem(int position) {
return mDataSource.get(position);
}
//3
@Override
public long getItemId(int position) {
return position;
}
//4
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// 1
if(convertView == null) {
// 2
convertView = mInflater.inflate(R.layout.list_item_recipe, parent, false);
// 3
holder = new ViewHolder();
holder.thumbnailImageView = (ImageView) convertView.findViewById(R.id.recipe_list_thumbnail);
holder.titleTextView = (TextView) convertView.findViewById(R.id.recipe_list_title);
holder.subtitleTextView = (TextView) convertView.findViewById(R.id.recipe_list_subtitle);
holder.detailTextView = (TextView) convertView.findViewById(R.id.recipe_list_detail);
// 4
convertView.setTag(holder);
}
else{
// 5
holder = (ViewHolder) convertView.getTag();
}
// 6
TextView titleTextView = holder.titleTextView;
TextView subtitleTextView = holder.subtitleTextView;
TextView detailTextView = holder.detailTextView;
ImageView thumbnailImageView = holder.thumbnailImageView;
Recipe recipe = (Recipe) getItem(position);
// 2
titleTextView.setText(recipe.title);
subtitleTextView.setText(recipe.description);
detailTextView.setText(recipe.label);
// 3
Picasso.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
Typeface titleTypeFace = Typeface.createFromAsset(mContext.getAssets(), "fonts/JosefinSans-Bold.ttf");
titleTextView.setTypeface(titleTypeFace);
Typeface subtitleTypeFace =
Typeface.createFromAsset(mContext.getAssets(), "fonts/JosefinSans-SemiBoldItalic.ttf");
subtitleTextView.setTypeface(subtitleTypeFace);
Typeface detailTypeFace = Typeface.createFromAsset(mContext.getAssets(), "fonts/Quicksand-Bold.otf");
detailTextView.setTypeface(detailTypeFace);
detailTextView.setTextColor(ContextCompat.getColor(mContext, LABEL_COLORS.get(recipe.label)));
return convertView;
}
private static class ViewHolder {
public TextView titleTextView;
public TextView subtitleTextView;
public TextView detailTextView;
public ImageView thumbnailImageView;
}
}
Alles anzeigen