【问题标题】:How to add button inside listadapter view in android如何在android的listadapter视图中添加按钮
【发布时间】:2014-10-14 05:43:26
【问题描述】:

这是我的代码:

@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.detailedlist_row, null);

    convertView.setClickable(true);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title); 

    TextView price = (TextView) convertView.findViewById(R.id.price); 
    TextView view = (TextView) convertView.findViewById(R.id.viewlistin); 
    TextView edit = (TextView) convertView.findViewById(R.id.editlisting); 
    view.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
               String userid;
              // Toast.makeText(activity, "view clicked: ", Toast.LENGTH_SHORT).show();

               Intent intent = new Intent(activity, review.class);
               activity.startActivity(intent); 

           }
       });

    final Movie m = movieItems.get(position); 
} 

我在adapter 内设置textviewonclick,当我点击textview 时,我的应用程序会强制关闭,你能指导我我的代码有什么问题吗

【问题讨论】:

  • 请显示错误日志
  • 删除convertView.setClickable(true);
  • 您的问题解决了吗?如果没有,请发布错误日志。

标签: android listview button adapter


【解决方案1】:

如果我没说错,你可以使用这种方式

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    TextView textView = (TextView)view.findViewById(R.id.viewlistin);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent (ThisClass.this, NextClass.class);
            startActivity(intent);
        }
    });
}

【讨论】:

    【解决方案2】:

    您是否创建了ListView?我不能仅通过查看发布的代码来判断。如果是这样,在创建之后,你可以创建一个ArrayAdapter并设置adapter

    这是一个例子:

    ListView list = (ListView) findViewById(R.id.listView);
    
    // Create The Adapter with passing ArrayList as 3rd parameter
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(First.this, R.layout.button, yourList);
    
    // Set The Adapter
    list.setAdapter(arrayAdapter);
    
    // register onClickListener to handle click events on each item
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
        // argument position gives the index of item which is clicked
        public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
            String itemSelected = yourList.get(position);
            int quantity= yourList.size();
            for (int l=0;l<quantity;l++) {
                if(yourList.get(l)==itemSelected) {
                    Intent intent = new Intent(First.this, Second.class);
    
                    // rest of code here
                    startActivity(intent);
    
                }
            }
        }
    });
    

    由于您想为列表的每个项目创建一个按钮,您可以按照本教程了解如何执行此操作button

    然后将R.layout.button 设置为您为按钮创建的布局 (xml) 的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多