我有一个从互联网上获得的自定义适配器。 它的食谱中包含图像和每个标题。 我已经在谷歌和这个网站上查找了我的具体案例,但还没找到我需要的确切内容。 我找到的信息让我很难调整我自己的案例问题。
实际上对初学者以上的人来说这应该是一项简单的任务,但不幸的是我是初学者。
当它是一个ArrayAdapter时很简单,因为我在适配器中没有图像变量,但现在找到标题,更改名称和删除项目的所有功能都不同。
public class MainActivity extends Activity {
private ListView listView1;
public String mName;
View btnAdd;
String mTitle;
Cursor mCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = this.findViewById(R.id.btnAdd);
Recipe recipe_data[] = new Recipe[]
{
new Recipe(R.drawable.cherry_pie, "Cherry Pie"),
new Recipe(R.drawable.chocchip_cookie, "Cocolate Chips Cookies"),
new Recipe(R.drawable.quaker_cookie, "Quaker Cookies"),
new Recipe(R.drawable.short_pastry, "Short Pastry"),
new Recipe(R.drawable.flour, "Will be added in the future...")
};
RecipeAdapter adapter = new RecipeAdapter(this,
R.layout.listview_item_row, recipe_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
OnClickListener listen = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
//we get the text into a global string
mName = ((TextView)edit).getText().toString();
//we reverse the edit var back to be empty
edit.setText("");
//this.notifyDataSetChanged();
}
};
btnAdd.setOnClickListener(listen);
registerForContextMenu(listView1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Change Name");
menu.add(0, v.getId(), 0, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle()=="Change Name"){changeN(item.getItemId());}
else if(item.getTitle()=="Delete"){
Toast.makeText(this, "Item " + mTitle + " has been removed..", Toast.LENGTH_SHORT).show();
}
else {return false;}
return true;
}
//Change item name function
public void changeN(int id){
}
}
public class RecipeAdapter extends ArrayAdapter<Recipe>{
Context context;
int layoutResourceId;
Recipe data[] = null;
public RecipeAdapter(Context context, int layoutResourceId, Recipe[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecipeHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecipeHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (RecipeHolder)row.getTag();
}
Recipe recipe = data[position];
holder.txtTitle.setText(recipe.title);
holder.imgIcon.setImageResource(recipe.icon);
return row;
}
static class RecipeHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
public class Recipe {
public int icon;
public String title;
public Recipe(){
super();
}
public Recipe(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
@Override
public String toString() {
return this.title;
}
}
答案 0 :(得分:0)
回答自己.. 需要添加以下行:mTitle = mArrayAdapter.getItem(pos).name; (在TOAST之前)