如何在listview中setOnclicklistener

时间:2015-08-01 09:29:31

标签: listview

我有一个ListActivity,我有这样的自定义列表视图。我想要setOnclicklistener 3个按钮。我怎样才能做到这一点?例如,我想按按钮分享一些文字。我怎么能这样做?

package ali.nurollahi.net;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class storys extends ListActivity {

private database  db;
private String[]  Codee;
private String[]  Moneey;
private String[]  Name;
private String[]  Teedad;
private String[]  Star;
private String[]  Share;
private String[]  Buy;

private TextView  teedad;
private TextView  code;

private String    season;
private String    name;
private TextView  moneyy;
private ImageView img_share;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.storys);

    db = new database(this);
    Bundle ex = getIntent().getExtras();
    season = ex.getString("season");

    refresher();
    setListAdapter(new AA());

}//this code is not correct


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    final TextView tv = (TextView) findViewById(R.id.codee);
    Button share = (Button) findViewById(R.id.share);
    share.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");

            shareIntent.putExtra(Intent.EXTRA_TEXT, tv.getText().toString());
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "کد های اینترنتی");
            startActivity(Intent.createChooser(shareIntent,
                    "share text ...."));

        }
    });

}//until


class AA extends ArrayAdapter<String> {

    public AA() {
        super(storys.this, R.layout.row_storys, Name);

    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater in = getLayoutInflater();
        View row = in.inflate(R.layout.row_storys, parent, false);

        TextView name = (TextView) row.findViewById(R.id.name);
        TextView money = (TextView) row.findViewById(R.id.moneyy);
        TextView code = (TextView) row.findViewById(R.id.codee);
        // Button share = (Button) row.findViewById(R.id.share);
        //  Button buy = (Button) row.findViewById(R.id.buy);

        final ImageView star = (ImageView) row.findViewById(R.id.starr);
        if (Star[position].equals("1")) {
            star.setImageResource(R.drawable.staron);
        } else {
            star.setImageResource(R.drawable.starof);
        }

        star.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                db.open();

                if (Star[position].equals("1")) {
                    db.beroozresani_doostdashtaniha("ussd", season, Name[position], "0");
                    star.setImageResource(R.drawable.starof);
                    Star[position] = "0";
                } else {
                    db.beroozresani_doostdashtaniha("ussd", season, Name[position], "1");
                    star.setImageResource(R.drawable.staron);
                    Star[position] = "1";
                }

            }
        });

        name.setText(Name[position]);
        code.setText(Codee[position]);
        money.setText(Moneey[position]);

        name.setTypeface(MainActivity.font);

        return (row);

    }

}


private void refresher() {

    db.open();
    int save = db.shomaresh_dastan("ussd", season);
    Codee = new String[save];
    Moneey = new String[save];
    Name = new String[save];
    Teedad = new String[save];
    Star = new String[save];

    for (int i = 0; i < save; i++) {
        Codee[i] = db.namayesh_code("ussd", i, season, 1);
        Name[i] = db.namayesh_time("ussd", i, season, 1);
        Moneey[i] = db.namayesh_money("ussd", i, season, 1);

        Star[i] = db.namayesh_dastan("ussd", i, season, 5);

    }
    db.close();

}

}

1 个答案:

答案 0 :(得分:1)

您还没有将星标设为按钮,而是__init__.py。它必须是ImageView才能处理ImageButton