【问题标题】:Onclick is not working for a button in Listview to fetch TextView in androidOnclick 不适用于 Listview 中的按钮以在 android 中获取 TextView
【发布时间】:2014-12-19 12:27:01
【问题描述】:

我有两个Buttons 以及ListView 中的文本。当我点击按钮时,Onclick 监听器不工作。

以下是我的代码:

list_details.xml

<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp" />

list_item_details.xml

<TextView
android:id="@+id/try_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" 
android:text="tile try" />

<Button
    android:id="@+id/download"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    />

ListDetailsActivity.java

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.list_details);

productsList = new ArrayList<HashMap<String, String>>();

new LoadAllProducts().execute();

ListView lv = getListView();

Button download = (Button) findViewById(R.id.download);

download.setOnClickListener(new View.OnClickListener() {

     public void onClick(View view) {

         TextView try_txt = (TextView) findViewById(R.id.try_it);

         String try_text_val = try_txt.getText().toString();

        Toast.makeText(getApplicationContext(), try_text_val, Toast.LENGTH_LONG).show();
     }
  });
 }


protected void onPostExecute(String file_url) {

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {

                public void run() {

                 ListAdapter adapter = new SimpleAdapter(

                         ListDetailsActivity.this, productsList,
                    R.layout.list_item_details, new String[] { TAG_TRY_IT},
                    new int[] { R.id.try_it});

                    // updating listview
                    setListAdapter(adapter);
                                    }
            });
   }
  }

非常感谢您的帮助!

【问题讨论】:

标签: android


【解决方案1】:

您只需在自定义适配器中设置 onClick 事件。 (在 getView 方法中)

【讨论】:

    【解决方案2】:

    考虑在列表行布局中为每个按钮使用android:onClick。这可能是最方便的方式,几乎总是如此。

    【讨论】:

    • 非常感谢!我可以使它与来自 xml 页面的 onClick 一起工作
    • @usd123:不客气。在这种情况下,请检查此答案。
    【解决方案3】:

    您只是在第一个Button 上设置OnClickListener,ID 为R.id.download。当您使用findViewById() 时,它只会返回具有请求 id 的第一个视图,即使还有更多。因为您是在 Activity 上调用它,所以它在列表视图中有多行带有具有该 ID 的按钮。

    修复它的方法是在您的适配器中进行。您正在使用SimpleAdapter。创建您自己的适配器类并覆盖getView() 函数以将OnClickListener 附加到那里。像这样的:

    private class MyAdapter extends SimpleAdapter {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            View button = view.findViewById(R.id.download);
            button.setOnClickListener( /* ... your code ... */ );
            return view;
        }
    }
    

    View Holder 设计模式

    在您的点击处理程序中,您正在操作 TextView R.id.try_it。因此,您还需要在从getView() 返回的视图上为此执行findViewById()。 View Holder 设计模式适用于这个用例。我的建议是阅读Making ListView Scrolling Smooth 上的 Android 教程以了解其工作原理。

    【讨论】:

    • 非常感谢。我创建了一个自定义适配器,但是在使用 getView() 中的 onClick 启动意图时遇到了问题。下面是代码: bt1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Uri uri = Uri.parse("google.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri) ; context.startActivity(intent); } });如果您有任何建议,请告诉我。
    • 把协议放在uri中:Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多