如何从CustomAdapter调用Activity中的方法

时间:2015-08-07 14:01:11

标签: java android custom-adapter

@Override public boolean onKeyUp(int keyCode, android.view.KeyEvent event) { //InputConnection ic = getCurrentInputConnection(); // Toast.makeText(this, "KEYVALUE"+String.valueOf(KeyEvent.KEYCODE_A), Toast.LENGTH_SHORT).show(); Log.i("key pressed", String.valueOf(event.getKeyCode())); return onKeyDown(keyCode, event); } image feed_item.xml custom adapter relative layout中的activity_main。我能够处理图片的点击事件。

我的player.setVisibility(View.GONE);Main Activity setVisibility(View.VISIBLE)在我的Custom Adapter发布后呈现Main Activity。我想public class FeedListAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private List<FeedItem> feedItems; ImageLoader imageLoader = AppController.getInstance().getImageLoader(); public FeedListAdapter(Activity activity, List<FeedItem> feedItems) { this.activity = activity; this.feedItems = feedItems; } @Override public int getCount() { return feedItems.size(); } @Override public Object getItem(int location) { return feedItems.get(location); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (inflater == null) inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) convertView = inflater.inflate(R.layout.feed_item, null); TextView name = (TextView) convertView.findViewById(R.id.name); FeedItem item = feedItems.get(position); name.setText(item.getName()); String fontPath = "fonts/Lato-Light.ttf"; // Loading Font Face Typeface tf = Typeface.createFromAsset(activity.getAssets(), fontPath); // Applying font name.setTypeface(tf); ImageView play=(ImageView)convertView.findViewById(R.id.play); play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(activity, "Show Layout!", Toast.LENGTH_SHORT).show(); } }); ImageView options=(ImageView)convertView.findViewById(R.id.options_popup); options.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFilterPopup(view); } }); return convertView; } } 这个布局时public class MainActivity extends ActionBarActivity { RelativeLayout player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar); setSupportActionBar(toolbar); TextView mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title); mToolBarTextView.setText("Home"); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); listView = (InnerGridView) findViewById(R.id.list); feedItems = new ArrayList<FeedItem>(); listAdapter = new FeedListAdapter(this, feedItems); listView.setAdapter(listAdapter); player = (RelativeLayout) findViewById(R.id.player); player.setVisibility(View.GONE); } } 单击行中的图像。

我无法将这两个布局与[d for d in request.user.distributors.all() if ''.lower() in d.full_name().lower()] request.user.distributors.all()相关联。任何建议都将受到赞赏!!

FeedListAdapter.java

request.user.distributors.all().filter(name__icontains=name | surname__icontains=surname)

MainActivity.java

replace()

3 个答案:

答案 0 :(得分:2)

您可以使用类似

的方法创建自己的界面
 public interface OnClickInMyAdapterListener {
    public void onItemclicked();
  }

比您的活动应该实现此接口并覆盖其方法

public class MainActivity extends ActionBarActivity implements OnClickInMyAdapterListener  {
   ...
   ...
}


@Override
public void onItemclicked() {
  // do what you want
}

更改适配器以重新接收界面

     private OnClickInMyAdapterListener  myActivityInterface;

     public FeedListAdapter(Activity activity, List<FeedItem> feedItems, OnClickInMyAdapterListener  myActivityInterface ) {
            this.activity = activity;
            this.feedItems = feedItems;
            this.myActivityInterface= myActivityInterface;

        }
活动中的

将接口传递给适配器

listAdapter = new FeedListAdapter(this, feedItems, (OnClickInMyAdapterListener ) this);

并在您的适配器中随时随地使用

this.myActivityInterface.onItemclicked();

...如果您的适配器用于更多不同的活动

,此解决方案也很有用

答案 1 :(得分:1)

您可以在Activity

中编写一个公共方法

将您的活动背景投射到您的活动

((MainActivity)activity).doSomething();

活动是您传递给适配器的上下文

doSomething是活动中的方法

答案 2 :(得分:0)

您应该调用:

,而不是在自定义适配器中的视图上设置OnClickListeners
listView.setOnItemSelectedListener(this);

让您的活动实施AdapterView.OnItemSelectedListener

请参阅http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html