如何在listview中使用OnItemClickListener进行onclick子视图(按钮)

时间:2012-10-03 08:20:16

标签: android android-layout android-intent onclick onitemclicklistener

任何人都可以举例说明/帮我看下一个活动的onclick按钮。我的自定义列表视图包含文本,按钮和滚动索引..我正在点击按钮问题..我尝试了3天但没有用..如果任何人说我怎么做使用onitemclickchildview(按钮(btnlist) ..正如我在一些文档中看到的那样,他们告诉listview是父级和内部列表视图项/内容/数据  是孩子..我希望儿童按钮(btnlist)onclick。请帮帮忙..先谢谢你。

homemplebrowview.xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:descendantFocusability="beforeDescendants"
android:padding="5dip" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="100dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textColor="#663333"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="21dp"
        android:textColor="#666666"
        android:text="MRN:" />

    <TextView
        android:id="@+id/mrn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_marginLeft="24dp"
        android:textColor="#666666"
        android:layout_toRightOf="@+id/textView4"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginLeft="63dp"
        android:layout_toRightOf="@+id/mrn"
        android:textColor="#666666"
        android:text="Actual Start Date:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/mrn"
        android:layout_alignBottom="@+id/mrn"
        android:layout_alignLeft="@+id/textView2"
        android:textColor="#666666"
        android:text="Age:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:textColor="#666666"
        android:text="Gender"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView6"
        android:layout_marginLeft="58dp"
        android:layout_toRightOf="@+id/textView2"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView6"
        android:layout_alignBottom="@+id/textView6"
        android:layout_alignRight="@+id/date"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView7"
        android:layout_alignBottom="@+id/textView7"
        android:layout_alignLeft="@+id/age"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/date"
        android:layout_alignBottom="@+id/date"
        android:layout_marginLeft="119dp"
        android:layout_toRightOf="@+id/date"
        android:textColor="#666666"
        android:text="Ward No:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/age"
        android:layout_alignBottom="@+id/age"
        android:layout_alignLeft="@+id/textView3"
        android:textColor="#666666"
        android:text="Room No:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView12"
        android:layout_alignParentBottom="true"
        android:textColor="#666666"
        android:text="Bed No:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/wardno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_marginLeft="60dp"
        android:layout_toRightOf="@+id/textView3"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/roomno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView12"
        android:layout_alignBottom="@+id/textView12"
        android:layout_alignLeft="@+id/wardno"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/bedno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView13"
        android:layout_alignBottom="@+id/textView13"
        android:layout_alignLeft="@+id/roomno"
        android:textColor="#666666"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/listbutton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/name"
        android:layout_marginRight="43dp"   
        android:focusable="false"      
        android:text="Episode"
        android:textColor="#666666" />


</RelativeLayout>

</RelativeLayout>

MainActivity.java

   public class MainActivity extends Activity implements
    SearchView.OnQueryTextListener, SearchView.OnCloseListener, OnClickListener{

private ListView listView;

private SearchView search;
EfficientAdapter objectAdapter; 
int textlength = 0;
private CheckBox checkStat, checkRoutine, checkTat;
private ArrayList<Patient> patientListArray;
private Patient patient;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homempleb);
    Log.i("scan", " txtScanResult ");

    ActionItem nextItem = new ActionItem();
    final QuickAction quickAction = new QuickAction(this,
            QuickAction.VERTICAL);
    quickAction.addActionItem(nextItem);
    quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
        @Override
        public void onDismiss() {
            Toast.makeText(getApplicationContext(), "Dismissed",
                    Toast.LENGTH_SHORT).show();
        }
    });


  listView = (IndexableListView) findViewById(R.id.homelistView);
    //listView = (ListView) findViewById(R.id.homelistView);      
    listView.setTextFilterEnabled(true);
    listView.setFastScrollEnabled(true);
    //listView.setItemsCanFocus(true);
    listView.setClickable(false);
    listView.addStatesFromChildren();
    objectAdapter = new EfficientAdapter(this);     
    listView.setAdapter(objectAdapter);     


}



@Override
public boolean onClose() {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

    **

@Override
public void onClick(View v) {
    Toast.makeText(MainActivity.this, "STAT", Toast.LENGTH_SHORT).show();       
    Intent next = new Intent(MainActivity.this, SeviceDetails.class);
   // Log.i("patient", " next "+ position + " onclickposition " + patientListArray.get(position).getMrnNumber());

    Log.i("DDDD ", patient.getMrnNumber());             


    System.out.println("patient"+ patient);
    startActivity(next);    

}

    **

  }

EfficientAdapter.java

    public class EfficientAdapter extends BaseAdapter implements SectionIndexer {
IndexableListView mListView;
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;

private Intent intent;
private Patient patient;
private LayoutInflater mInflater;
private Context context;
private int positions;
ViewHolder holder;
private String position;


public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    this.context = context;

    String patientListJson = CountriesList.jsonData;
    JSONObject jssson;
    try {
        jssson = new JSONObject(patientListJson);
        patientListJson = jssson.getString("PostPatientDetailResult");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
    patientListArray = new ArrayList<Patient>();
    for (JsonElement obj : Jarray) {
        Patient patientList = gson.fromJson(obj, Patient.class);
        patientListArray.add(patientList);
    //  Log.i("patientList", patientListJson);

    }
}


public int getCount() {

    return patientListArray.size();
}

public Object getItem(int position) {

    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    this.positions = position;      
    View rowView = convertView;

    if (rowView  == null) { 
        Log.i("row", "Inside IF");
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.homemplebrowview, null); 

        holder = new ViewHolder();
        holder.text1 = (TextView) rowView.findViewById(R.id.name);
        holder.text2 = (TextView) rowView.findViewById(R.id.mrn);
        holder.text3 = (TextView) rowView.findViewById(R.id.date);
        holder.text4 = (TextView) rowView.findViewById(R.id.age);
        holder.text5 = (TextView) rowView.findViewById(R.id.gender);
        holder.text6 = (TextView) rowView.findViewById(R.id.wardno);
        holder.text7 = (TextView) rowView.findViewById(R.id.roomno);
        holder.text8 = (TextView) rowView.findViewById(R.id.bedno);


    **  holder.btnList = (Button) rowView.findViewById(R.id.listbutton);**



        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }
    Log.i("row", "Inside ELSE");

    holder.text1.setText(Util.formatN2H(patientListArray.get(position)
            .getName()));
    holder.text2.setText(patientListArray.get(position).getMrnNumber());
    holder.text3.setText(Util.formatN2H(patientListArray.get(position)
            .getRoom()));
    holder.text4.setText(Util.formatN2H(patientListArray.get(position)
            .getAge()));
    holder.text5.setText(Util.formatN2H(patientListArray.get(position)
            .getGender()));
    holder.text6.setText(Util.formatN2H(patientListArray.get(position)
            .getWard()));
    holder.text7.setText(Util.formatN2H(patientListArray.get(position)
            .getRoom()));
    holder.text8.setText(Util.formatN2H(patientListArray.get(position)
            .getBed()));        


**  holder.btnList.setOnClickListener(((MainActivity) context));  **

return rowView;
}       

static class ViewHolder {
    public Button btnList;
    public TextView text8;
    public TextView text7;
    public TextView text6;
    public TextView text5;
    public TextView text4;
    public TextView text1;
    public TextView text2;
    public TextView text3;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}


public int getPositionForSection(int section) {
    sortMyData();

    Log.i("getPositionForSection", "section" + section);
    // If there is no item for current section, previous section will be
    // selected
    for (int i = section; i >= 0; i--) {
        for (int j = 0; j < getCount(); j++) {
            if (i == 0) {

                Log.i("getPositionForSection- i", "section" + i);
                // For numeric section
                for (int k = 0; k <= 9; k++) {

                    if (StringMatcher.match(
                            String.valueOf(patientListArray.get(j)
                                    .getName().charAt(0)),
                            String.valueOf(k)))
                        Log.i("getPositionForSection- j", "section" + j);

                        return j;
                }
            } else {
                if (StringMatcher.match(
                        String.valueOf(patientListArray.get(j).getName()
                                .charAt(0)),
                        String.valueOf(mSections.charAt(i))))
                    return j;
            }
        }
    }
    return 0;
}

public int getSectionForPosition(int position) {
    return 0;
}

public Object[] getSections() {
    String[] sections = new String[mSections.length()];
    for (int i = 0; i < mSections.length(); i++)
        sections[i] = String.valueOf(mSections.charAt(i));
    return sections;
}

/**
 * sorting the patientListArray data
 */
public void sortMyData() {
    // sorting the patientListArray data
    Collections.sort(patientListArray, new Comparator<Object>() {
        @Override
        public int compare(Object k1, Object k2) {
            Patient p1 = (Patient) k1;
            Patient p2 = (Patient) k2;
            return p1.getName().compareToIgnoreCase(p2.getName());
        }

    });
}

2 个答案:

答案 0 :(得分:0)

我不确定你的代码,它太繁琐了。但我以前遇到过同样的问题,我解决了如下问题。在适配器中,您需要为该按钮设置on click侦听器并为其添加标签。

holder.btnList.setOnClickListener(((MyActivity) context));

其中context是您传递给适配器的上下文,而MyActivity是要处理点击的任何活动。因此,当您单击该按钮时,将调用点击侦听器上的MyActivity。如果要传递任何数据,还可以向持有者对象添加标记。然后在MyActivity中实施onClickListener。您可以在MyActivity中处理所需的任何事件。如果您有任何疑问,请发表更多疑问。

答案 1 :(得分:0)

holder.btnList.setOnClickListener(new OnClickListener(){
     @Override
     public void onClick(View v){
     //youre code

     }
);