的onclick按钮没有工作列表视图视图、按钮、列表、工作

2023-09-05 04:17:00 作者:暗哑行秋

我的项目包含的ListView(homelistView)包含按钮(btnList)。

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

请给我建议这方面一个很好的例子。

下面是我的code:

如何使INFOPATH与EXCEL文件建立联系

下面是我的列表视图中包含按钮。当点击按钮,就必须去其他活动

  -------------------------------- A--
  文字文本按钮(btnList)乙
   -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - C - -
  文字文本按钮(btnList)D
  -------------------------------- E--
 

homempleb.xml

在我用这个code XML格式。 buttonlist工作得很好,我按下面的code

 <的ListView
            机器人:ID =@ + ID / homelistView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1.04
            机器人:dividerHeight =0dip>

      < / ListView控件>

  EfficientAdapter.java

公共EfficientAdapter(上下文的背景下){
        mInflater = LayoutInflater.from(上下文);
this.context =背景;
    }

     在您的ViewHolder类,你需要添加'按钮btnList.`

holder.btnList.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                接下来的意图=新的意图(背景下,SeviceDetails.class);
                context.startActivity(下);
            }
        });
 

homempleb.xml

目前我添加滚动索引到我的列表视图,并改变了code按如下.. Listbutton工作不适合我现在.. PLZ帮我可以看出U code在EfficientAdapter.JAVA-快速参考----> g​​etview方法---> holder.btnList。

 < com.woozzu.android.widget.IndexableListView

            机器人:ID =@ + ID / homelistView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1.04
            机器人:dividerHeight =0dip>
    < /com.woozzu.android.widget.IndexableListView>
 

MainActivity.java

 公共类MainActivity扩展活动器具
    SearchView.OnQueryTextListener,SearchView.OnCloseListener {

私人的ListView ListView的;
//私人IndexableListView的ListView;
私人搜索查看搜索;
EfficientAdapter objectAdapter;
// EfficientAdapter2 objectAdapter1;
INT长度限制:Textlength = 0;
私人复选框checkStat,checkRoutine,checkTat;
私人的ArrayList<患者GT; patientListArray;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.homempleb);
    Log.i(扫描,txtScanResult);

    ActionItem nextItem =新ActionItem();
    最后QuickAction quickAction =新QuickAction(这一点,
            QuickAction.VERTICAL);
    quickAction.addActionItem(nextItem);
    quickAction.setOnDismissListener(新QuickAction.OnDismissListener(){
        @覆盖
        公共无效onDismiss(){
            Toast.makeText(getApplicationContext(),撤职,
                    Toast.LENGTH_SHORT).show();
        }
    });

    搜索=(搜索查看)findViewById(R.id.searchView1);
    sea​​rch.setIconifiedByDefault(假);
    sea​​rch.setOnQueryTextListener(本);
    sea​​rch.setOnCloseListener(本);
    sea​​rch.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            quickAction.show(五);
        }
    });
    checkStat =(复选框)findViewById(R.id.checkBoxStat);
    checkRoutine =(复选框)findViewById(R.id.checkBoxRoutine);
    checkTat =(复选框)findViewById(R.id.checkBoxTat);
    checkStat.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            如果(((复选框)ⅴ).isChecked()){
                checkStat.setChecked(真正的);
                Toast.makeText(MainActivity.this,STAT,
                        Toast.LENGTH_SHORT).show();
                checkRoutine.setChecked(假);
                checkTat.setChecked(假);
            }
        }
    });
    checkRoutine.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            如果(((复选框)ⅴ).isChecked()){
                checkRoutine.setChecked(真正的);
                Toast.makeText(MainActivity.this,例行公事,
                        Toast.LENGTH_SHORT).show();
                checkStat.setChecked(假);
                checkTat.setChecked(假);
            }
        }
    });
    checkTat.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            如果(((复选框)ⅴ).isChecked()){
                checkTat.setChecked(真正的);
                Toast.makeText(MainActivity.this,TAT效益分析,
                        Toast.LENGTH_SHORT).show();
                checkRoutine.setChecked(假);
                checkStat.setChecked(假);
            }
        }
    });

    // ListView控件=(IndexableListView)findViewById(R.id.homelistView);
    ListView的=(的ListView)findViewById(R.id.homelistView);
    listView.setTextFilterEnabled(真正的);
    listView.setFastScrollEnabled(真正的);
    listView.setFastScrollAlwaysVisible(真正的);
    objectAdapter =新EfficientAdapter(本);
    listView.setAdapter(objectAdapter);

    按钮refreshButton =(按钮)findViewById(R.id.refreshButton);
    refreshButton.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            // objectAdapter1 =新EfficientAdapter2(MainActivity.this);
            objectAdapter =新EfficientAdapter(MainActivity.this); //适配器
                                                                    //用
                                                                    // 新
                                                                    // 数据
            listView.setAdapter(objectAdapter);

            Log.i(notifyDataSetChanged,数据更新);
            // objectAdapter1.notifyDataSetChanged();
            objectAdapter.notifyDataSetChanged();

        }
    });

}

@覆盖
公共布尔的OnClose(){
    返回false;
}

@覆盖
公共布尔onQueryTextChange(字符串newText){
    返回false;
}

@覆盖
公共布尔onQueryTextSubmit(查询字符串){
    返回false;
}
 

}

EfficientAdapter.JAVA

 公共类EfficientAdapter扩展了BaseAdapter实现SectionIndexer {

私人字符串mSections =#ABCDEFGHIJKLMNOPQRSTUVWXYZ;
ArrayList的<患者GT; patientListArray;

私人LayoutInflater mInflater;
私人上下文的背景下;
        ViewHolder持有人;
公共EfficientAdapter(上下文的背景下){
    mInflater = LayoutInflater.from(上下文);
    this.context =背景;

    字符串patientListJson = CountriesList.jsonData;
    的JSONObject jssson;
    尝试 {
        jssson =新的JSONObject(patientListJson);
        patientListJson = jssson.getString(PostPatientDetailResult);
    }赶上(JSONException E){
        e.printStackTrace();
    }
    GSON GSON =新GSON();
    JsonParser分析器=新JsonParser();
    JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
    patientListArray =新的ArrayList<患者GT;();
    对于(JsonElement OBJ:Jarray){
        患者patientList = gson.fromJson(OBJ,Patient.class);
        patientListArray.add(patientList);
        Log.i(patientList,patientListJson);

    }
}

/ **
 *排序patientListArray数据
 * /
公共无效sortMyData(){
    //排序patientListArray数据
    Collections.sort(patientListArray,新的比较<对象>(){
        @覆盖
        公众诠释比较(对象01,对象02){
            病人P1 =(患者)01;
            患者P2 =(患者)O2;
            返回p1.getName()与compareToIgnoreCase(p2.getName())。
        }

    });
}

公众诠释getCount将(){

    返回patientListArray.size();
}

公共对象的getItem(INT位置){

    返回的位置;
}

众长getItemId(INT位置){
    返回的位置;
}

公共查看getView(INT位置,查看convertView,ViewGroup中父){


    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.homemplebrowview,NULL);
        持有人=新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 =(按钮)convertView.findViewById(R.id.listbutton);



        convertView.setTag(保持器);
    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }

    holder.text1.setText(Util.formatN2H(patientListArray.get(位置)
            .getName()));
    holder.text2.setText(patientListArray.get(位置).getMrnNumber());
    holder.text3.setText(Util.formatN2H(patientListArray.get(位置)
            .getRoom()));
    holder.text4.setText(Util.formatN2H(patientListArray.get(位置)
            .getAge()));
    holder.text5.setText(Util.formatN2H(patientListArray.get(位置)
            .getGender()));
    holder.text6.setText(Util.formatN2H(patientListArray.get(位置)
            .getWard()));
    holder.text7.setText(Util.formatN2H(patientListArray.get(位置)
            .getRoom()));
    holder.text8.setText(Util.formatN2H(patientListArray.get(位置)
            .getBed()));
            holder.btnList.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(上下文,STAT,Toast.LENGTH_SHORT).show();
                接下来的意图=新的意图(背景下,Home.class);
                Log.i(next23,下一步);
                context.startActivity(下);
            }
        });

    返回convertView;
}

静态类ViewHolder {
    公众按钮btnList;
    公众的TextView text8;
    公众的TextView text7;
    公众的TextView text6;
    公众的TextView text5;
    公共TextView的文本4;
    公众的TextView文本1;
    公众的TextView文本2;
    公众的TextView文本3;
}

@覆盖
公共无效notifyDataSetChanged(){
    super.notifyDataSetChanged();
}

公众诠释getPositionForSection(INT部分){
    //排序patientListArray数据
    sortMyData();
    //如果没有为当前部分没有项目,previous节会
    //选择
    的for(int i =节; I> = 0;我 - ){
        对于(INT J = 0; J< getCount将(); J ++){
            如果(我== 0){
                //对于数字部分
                对于(INT K = 0; K< = 9; k ++){
                    如果(StringMatcher.match(
                            将String.valueOf(patientListArray.get(J)
                                    .getName()。的charAt(0)),
                            将String.valueOf(K)))
                        复位J;
                }
            } 其他 {
                如果(StringMatcher.match(
                        将String.valueOf(patientListArray.get(J).getName()
                                .charAt(0)),
                        将String.valueOf(mSections.charAt(I))))
                    复位J;
            }
        }
    }
    返回0;
}

公众诠释getSectionForPosition(INT位置){
    返回0;
}

公共对象[] getSections(){
    的String []部分=新的String [mSections.length()];
    的for(int i = 0; I< mSections.length();我++)
        区段[I] =将String.valueOf(mSections.charAt(ⅰ));
    返回段;
}

 }
 

解决方案

在你高效的适配器类中声明 ViewHolder持有 getView方法外 做的MoshErsan说。

也会改变你的

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

  convertView = mInflater.inflate(R.layout.homemplebrowview,父母,假);
 

My project contains listView(homelistView) that contains button(btnList).

When I click on button(btnList) it must go to another Activity. I tried a lot but I didn't find a good example.

Please suggest me a good example regarding this.

Below is my code:

Here is my listview contains button. When on click of button it must go to other activity

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

homempleb.xml

Before i used this code in xml. buttonlist worked fine for me as per below code

       <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

Currently i added scroll index to my listview and changed the code as per below.. Listbutton is not working for me now.. Plz help me u can see code for quick reference in EfficientAdapter.JAVA-----> getview method--->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;
}

 }

解决方案

In your Efficient adapter class declare ViewHolder holder outside getView method and do as MoshErsan said.

Also Change your

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

to

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