0

任何人都可以给我一个例子/帮助我如何在下一个活动中执行 onclick 按钮。我的自定义列表视图包含文本、按钮和滚动索引..我在点击按钮上遇到问题..我从 3 天开始尝试了很多但没有用..如果有人能告诉我如何使用 onitemclickchildview(button(btnlist) ..正如我在一些文档中看到的那样,他们告诉listview是父级,而listview内的项目/内容/数据是子级..我希望子按钮(btnlist)可以点击。请帮助它..提前谢谢你。

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);    

}

    **

  }

高效适配器.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());
        }

    });
}
4

2 回答 2

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

     }
);
于 2013-03-22T06:22:17.713 回答
0

我不确定你的代码,它太乏味了。但是我以前也遇到过同样的问题,我解决了如下。在您的适配器中,您需要为该按钮设置点击监听器并向其添加标签。

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

context您传递给适配器的上下文在哪里,以及MyActivity要在其中处理单击的任何活动。因此,当您单击按钮时,MyActivity将调用 's on click 侦听器。如果您希望传递任何数据,您还可以将标签添加到持有者对象。然后在MyActivity您实施onClickListener. 您可以在MyActivity. 如果您有任何疑问,请发表进一步的疑问。

于 2012-10-03T08:29:40.233 回答