onclick按钮在listview中不起作用

时间:2012-09-27 13:01:44

标签: android android-layout android-intent android-emulator android-widget

我的项目包含 listView(homelistView),其中包含按钮(btnList)

当我点击按钮(btnList)时,它必须转到另一个活动。我尝试了很多,但我没有找到一个很好的例子。

请为此建议一个很好的例子。

以下是我的代码:

这是我的listview包含按钮。点击按钮时,必须转到其他活动

  --------------------------------A--
  text  text      button(btnList) B
  --------------------------------C---
  text  text      BUTTON(btnList) D
  --------------------------------E--

homempleb.xml

之前我在xml中使用此代码。按照以下代码

,按钮列表对我来说很好
       <ListView              
            android:id="@+id/homelistView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.04"
            android:dividerHeight="0dip" >

      </ListView> 

  EfficientAdapter.java

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

     In your ViewHolder class you need to add `Button btnList.`

holder.btnList.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent next=new Intent(context, SeviceDetails.class);
                context.startActivity(next);
            }
        });

homempleb.xml

目前我将滚动索引添加到我的listview并更改了以下代码.. Listbutton现在不适合我.. Plz帮助我你可以看到代码在EfficientAdapter.JAVA中快速参考-----&gt; getview方法---&gt; holder.btnList。

  <com.woozzu.android.widget.IndexableListView

            android:id="@+id/homelistView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.04"
            android:dividerHeight="0dip" >
    </com.woozzu.android.widget.IndexableListView>

MainActivity.java

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

private ListView listView;
// private IndexableListView listView;
private SearchView search;
EfficientAdapter objectAdapter;
// EfficientAdapter2 objectAdapter1;
int textlength = 0;
private CheckBox checkStat, checkRoutine, checkTat;
private ArrayList<Patient> patientListArray;

@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();
        }
    });

    search = (SearchView) findViewById(R.id.searchView1);
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);
    search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            quickAction.show(v);
        }
    });
    checkStat = (CheckBox) findViewById(R.id.checkBoxStat);
    checkRoutine = (CheckBox) findViewById(R.id.checkBoxRoutine);
    checkTat = (CheckBox) findViewById(R.id.checkBoxTat);
    checkStat.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                checkStat.setChecked(true);
                Toast.makeText(MainActivity.this, "STAT",
                        Toast.LENGTH_SHORT).show();
                checkRoutine.setChecked(false);
                checkTat.setChecked(false);
            }
        }
    });
    checkRoutine.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                checkRoutine.setChecked(true);
                Toast.makeText(MainActivity.this, "ROUTINE",
                        Toast.LENGTH_SHORT).show();
                checkStat.setChecked(false);
                checkTat.setChecked(false);
            }
        }
    });
    checkTat.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                checkTat.setChecked(true);
                Toast.makeText(MainActivity.this, "TAT Effeciency",
                        Toast.LENGTH_SHORT).show();
                checkRoutine.setChecked(false);
                checkStat.setChecked(false);
            }
        }
    });

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

    Button refreshButton = (Button) findViewById(R.id.refreshButton);
    refreshButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // objectAdapter1 = new EfficientAdapter2(MainActivity.this);
            objectAdapter = new EfficientAdapter(MainActivity.this);// adapter
                                                                    // with
                                                                    // new
                                                                    // data
            listView.setAdapter(objectAdapter);

            Log.i("notifyDataSetChanged", "data updated");
            // objectAdapter1.notifyDataSetChanged();
            objectAdapter.notifyDataSetChanged();

        }
    });

}

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

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

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

}

EfficientAdapter.JAVA

 public class EfficientAdapter extends BaseAdapter implements SectionIndexer {

private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;

private LayoutInflater mInflater;
private Context context;
        ViewHolder holder;
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);

    }
}

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

    });
}

public int getCount() {

    return patientListArray.size();
}

public Object getItem(int position) {

    return position;
}

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

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


    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.homemplebrowview, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.name);
        holder.text2 = (TextView) convertView.findViewById(R.id.mrn);
        holder.text3 = (TextView) convertView.findViewById(R.id.date);
        holder.text4 = (TextView) convertView.findViewById(R.id.age);
        holder.text5 = (TextView) convertView.findViewById(R.id.gender);
        holder.text6 = (TextView) convertView.findViewById(R.id.wardno);
        holder.text7 = (TextView) convertView.findViewById(R.id.roomno);
        holder.text8 = (TextView) convertView.findViewById(R.id.bedno);
        holder.btnList = (Button) convertView.findViewById(R.id.listbutton);



        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    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(new OnClickListener() {
            @Override
            public void onClick(View v) {                   
                Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
                Intent next = new Intent(context, Home.class);
                Log.i("next23", "next");
                context.startActivity(next);
            }
        });

    return convertView;
}

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) {
    // sorting the patientListArray data
    sortMyData();
    // 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) {
                // For numeric section
                for (int k = 0; k <= 9; k++) {
                    if (StringMatcher.match(
                            String.valueOf(patientListArray.get(j)
                                    .getName().charAt(0)),
                            String.valueOf(k)))
                        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;
}

 }

3 个答案:

答案 0 :(得分:2)

问题在于onInterceptTouchEvent方法。您正在使用来自woozzu的IndexableListView,并且此类重写onInterceptTouchEvent以返回true。这意味着IndexableListView总是从您孩子的视图中窃取触摸事件,以便它可以将其提供给IndexScroller。您可以更改此行为,如果不是返回true,则输入以下条件:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mScroller != null && mScroller.contains(ev.getX(), ev.getY()))
        return true;
    return super.onInterceptTouchEvent(ev);
}

这种方式只会消耗与IndexScroller交互的事件。其他活动将传播到儿童的视图,您的按钮将是可点击的。

答案 1 :(得分:1)

在您的Efficient adapter类中,在getView方法之外声明ViewHolder holder 就像MoshErsan所说的那样。

同时更改

convertView = mInflater.inflate(R.layout.homemplebrowview, null);

convertView = mInflater.inflate(R.layout.homemplebrowview, parent,false);

答案 2 :(得分:0)

在您的适配器中

,只需移动

holder.btnList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {                   
                Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
                Intent next = new Intent(context, Home.class);
                Log.i("next23", "next");
                context.startActivity(next);
            }
        });

return convertView;之前 每次适配器返回视图时都需要设置监听器。