簡體   English   中英

Android ListView中的onclick按鈕

[英]onclick button in listview in android

我的項目包含listView(homelistView) ,其中包含button(btnList)

當我單擊按鈕(btnList)時,它必須轉到另一個活動。 我做了很多嘗試,但沒有找到一個很好的例子。

請給我一個很好的例子。

下面是我的代碼:

這是我的列表視圖包含按鈕。 當單擊按鈕時,它必須轉到其他活動

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

homempleb.xml在xml中使用此代碼之前。 根據Mohith verma樣本,buttonlist對我來說效果很好

       <ListView              
            android:id="@+id/homelistView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.04"
            android:dividerHeight="0dip" >

      </ListView> 

homempleb.xml當前,我在列表視圖中添加了滾動索引,並按如下所示更改了代碼。Listbutton現在對我不起作用..plz幫助我

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

}

高效適配器

 public class EfficientAdapter extends BaseAdapter implements SectionIndexer {

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

private LayoutInflater mInflater;
private Context context;

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) {
    ViewHolder holder;

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

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

        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) { Intent next=new
     Intent(context, Home.class); 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;
}

 }

首先從MainActivity類中刪除btnList,btnScan。

在您的EfficientAdapter類中添加對象Context Context

更改您的構造器:

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

至:

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

在您的ViewHolder類中,您需要添加Button btnList.

然后在getview方法中使用holder.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);
            }
        });

在您的getview方法中,然后返回convertView。

嘗試這個..

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_layout_ip_addtional_users, null);
    }

    //Handle TextView and display string from your list
    TextView listItemText2 = (TextView)view.findViewById(R.id.list_item_string_name);
    listItemText2.setText(list.get(position));

    ImageButton button8 = (ImageButton) view.findViewById(R.id.ip_additional_user_edit);
    button8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, IPAdditionalUsersEdit.class);
            context.startActivity(intent);
        }
    });

    return view;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM