具有自定义列表视图的按钮单击事件

时间:2015-10-08 17:35:21

标签: android android-layout android-intent android-activity

我在底部有两个按钮,即listview结束后。

我正在使用自定义列表视图,该列表视图显示具有替代颜色的列表项。

如何为底部的两个按钮设置setOnClickListener?

public class PieMainActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mRes = getResources();
    new Random(new Date().getTime());

    solbtn = (Button)findViewById(R.id.solution);

    String recive ;

    Bundle b = getIntent().getExtras();
    final int piewrong=b.getInt("piewrong");
    final int pieright=b.getInt("pieright");
    final int pieunclick=b.getInt("pieunclick");

    setContentView(R.layout.piechart_result);


    recive = pieright+"/20";
    mPie.setUnit(recive);



    data1 = new ArrayList<String>();
    fillData() ;

    listtotal =getIntent().getStringArrayListExtra("listtotal");
    timeuse =getIntent().getStringArrayListExtra("timeused");

    adapter1 = new ListAdapter(this, data1, listtotal,timeuse);
    ListView lvMain = (ListView) findViewById(R.id.list);
    lvMain.setAdapter(adapter1);


    lvMain.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
             int questionno = position+1;

                Bundle b = new Bundle();                
                b.putInt("questionno",questionno);
                b.putInt("piewrong",piewrong);
                b.putInt("pieright",pieright);
                b.putInt("pieunclick",pieunclick);
                Intent in=new Intent(PieMainActivity.this,Solution.class);
                in.putStringArrayListExtra("listtotal", (ArrayList<String>) listtotal);
                in.putStringArrayListExtra("timeused", (ArrayList<String>) timeuse);
                in.putExtras(b); 
                startActivity(in);
                finish();

        }
    });


}
void fillData() {
    for (int i = 1; i <= 20; i++) {
        data1.add("Question " + i);
    }
}
void fillcmp() {
    for (int i = 1; i <= 20; i++) {
        cmp.add("cmp " + i);
    }
}
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.staritsolutions.apptitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"

android:orientation="vertical" >
 <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
           android:layout_marginTop="5dp" 
           android:layout_weight="1.0">
 <ListView
    android:id="@+id/list"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
         >
      </ListView>


  </LinearLayout>

  <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="40dp"
           android:layout_marginBottom="10dp"
           android:layout_marginTop="5dp"
           android:orientation="horizontal"
           android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp">

       <Button
         android:id="@+id/home"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_gravity="bottom|center_horizontal"
         android:layout_marginLeft="6dp"
         android:layout_marginRight="3dp"
         android:textSize="12dp"
         android:layout_weight="1"
         android:clickable="true"
         android:gravity="center"

         android:text="Main Menu"
         android:background="@drawable/btn_blue" 
         android:textColor="#fff"
         android:textStyle="bold" />

    <Button
         android:id="@+id/solution"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_gravity="bottom|center_horizontal"
         android:layout_marginLeft="6dp"
         android:layout_marginRight="3dp"
         android:textSize="12dp"
         android:layout_weight="1"
         android:clickable="true"
         android:gravity="center"

         android:text="Solutions"
         android:background="@drawable/btn_blue" 
         android:textColor="#fff"
         android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

BaseAdapter文件

public class ListAdapter extends BaseAdapter{
Context ctx;
LayoutInflater lInflater;
List<String> data1;
List<String> cmp;
List<String> timeused;

ListAdapter(Context context, List<String> data1 ,List<String> cmp ,List<String> timeused) {
    ctx = context;
    this.data1 = data1;
    this.cmp = cmp;
    this.timeused = timeused;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {

    return data1.size();
}
public int getCount1() {
    return cmp.size();
}


@Override
public Object getItem(int position) {
    return data1.get(position);
}
public Object getItem1(int position1) {
    return cmp.get(position1);
}

@Override
public long getItemId(int position) {
    return position;
}
public long getItemId1(int position1) {
    return position1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.listitem, parent, false);
    }
    /*if (position % 2 == 0) {
        view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
    } else {
        view.setBackgroundResource(R.drawable.artists_list_background_alternate);
    }*/
    TextView text = (TextView) view.findViewById(R.id.heading);
    int no = position;
    if(cmp.get(position).equals("GREEN"))
    {
        text.setTextColor(Color.parseColor("#00D50E"));
    }else if(cmp.get(position).equals("RED"))
    {
        text.setTextColor(Color.parseColor("#e84040"));
    }else
    {
        text.setTextColor(Color.parseColor("#B441E9"));
    }

    ((TextView) view.findViewById(R.id.heading)).setText(data1.get(position));
    ((TextView) view.findViewById(R.id.duration)).setText(timeused.get(position));
    return view;
    //String.valueOf(value);
 }
}

2 个答案:

答案 0 :(得分:0)

所以这些按钮不是ListView的一部分?好吧,比你在onCreate做的那样:

solbtn = (Button)findViewById(R.id.solution);
solbth.setOnClickListener( toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourMethod();
        }
    });

第二个按钮也是如此。

或者你可以让你的Activity实现View.OnClickListener,在这样做之后你可以像这样设置你的点击监听器:

//this two lines in your OnCreate
button1.setOnClickListener(this);
button2.setOnClickListener(this);

//somewhere later in the code

@Override
public void onClick(View v) {
    //do something
}

答案 1 :(得分:0)

只需在onCrete()方法中添加clickListeners:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    .....

    solbtn = (Button) findViewById(R.id.home);
    homebtn = (Button) findViewById(R.id.solution);

    solbtn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // do stuff for solution button
            }
    });

    homebtn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // do stuff for home button
            }
    });

    .....
}