【问题标题】:How to get position and set listener of ImageButton in custom ListView如何在自定义 ListView 中获取 ImageButton 的位置和设置监听器
【发布时间】:2014-02-10 11:13:59
【问题描述】:

我有一个加载了 AsyncTask 的 CustomListView。 我在每一行的末尾都有一个 imageButton。

我的问题:当我点击我的 imageButton 时,没有任何效果。 我尝试了很多这样的解决方案:

Android : how to set listener and get position of imagebutton click in a custom adapter

我的 ListView 类调用 AsyncTask :

public class listview extends Activity {

private ListView maListViewPerso;   
ImageButton imgButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#000000")));

    maListViewPerso = (ListView) findViewById(R.id.listviewperso);

    asyncTask asynchrone_task_tournee = new asyncTask(listview.this, maListViewPerso);
    asynchrone_task_tournee.execute();  

}

这是我的 imageButton,我想在其中获取位置并设置侦听器..

EDIT 1(回答评论)这是我的onPostExecute(),还有我的 SimpleAdapter:

@Override
protected void onPostExecute(JSONArray jArray) {        
    super.onPostExecute(jArray);

    JSONObject json_data=null;     
    HashMap<String, String> map;
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

    this.pDialog.dismiss();     



      Log.i("JSON_TOURNER",jArray.toString());

      for(int i=0;i < jArray.length();i++)
      {             
            try {

                json_data = jArray.getJSONObject(i);

                 map = new HashMap<String, String>();
                 map.put("titre", json_data.getString("time"));
                 map.put("description", json_data.getString("date"));
                 map.put("begin_hour", json_data.getString("hour"));
                 map.put("end_hour", json_data.getString("hour"));


                 listItem.add(map);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }     
      }

      SimpleAdapter mSchedule = new SimpleAdapter (Mycontext, listItem, R.layout.list_type_tournee,
             new String[] {"titre", "description","begin_hour","end_hour"}, new int[] {R.id.miscellaneous, R.id.infos,R.id.begin_hour,R.id.end_hour});

      listview.setAdapter(mSchedule);


}

【问题讨论】:

  • 您的ListAdapter 代码在哪里?你可以在getView() 方法中实现它。

标签: java android android-listview onclicklistener imagebutton


【解决方案1】:

好吧,如果您在ListView 中使用ImageButtonCheckBoxButton 等控件,那么您将面临herehere 讨论的问题。

要解决此问题,您需要在 ImageButton 的 xml 中添加 android:focusable="false"

如果上述方法不起作用,那么您应该将android:descendantFocusability="blocksDescendants" 添加到您的ImageButton 布局中,然后为ImageButton 实现onClickListener()

更多信息请参见this

希望对你有帮助。

【讨论】:

  • 我尝试为 ImageButton 实现 onClickListener(),并将 android:descendantFocusability="blocksDescendants" 设置为父布局。但是当我运行应用程序时,它直接崩溃了
【解决方案2】:

我解决了我的问题。 最后,我删除了 imageButton,并在我的线性布局中设置了一个框架布局,并在我的 listeView 上设置了一个侦听器以打开对话框。

我也喜欢 android:descendantFocusability="blocksDescendants",在我的框架布局的线性父级中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2020-08-17
    • 1970-01-01
    • 2015-04-19
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多