2012-09-28 107 views
1

我的項目包含listView(homelistView),其中包含按鈕(btnList)。如何在適配器列表視圖中添加onclick按鈕

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

請給我一個很好的例子。 點擊我的按鈕(btnList)不工作plz sujject我的錯誤&它的解決方案plz ..提前感謝您。

編輯:

最後2天之後的奮鬥,我得到總產量的70%,清楚地看到是這個環節button in listview adapter

它看起來像這樣

enter image description here

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

EfficientAdapter.java

public class EfficientAdapter extends BaseAdapter implements SectionIndexer { 

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

private LayoutInflater mInflater; 
private Context context; 
protected ListView mListView; 
private int positions; 
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) { 

    this.positions = position; 

    if (convertView == null) {      
     convertView = mInflater.inflate(R.layout.homemplebrowview, parent,false); 
     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(test)); 

    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"+ next); 
      context.startActivity(next); 
     } 
    }); 

    return convertView; 
} 



     /*OnItemClickListener test = new OnItemClickListener() { 
onItemClick(AdapterView<?> parent, View view, int position, long id){ 
     ((TextView)view.findViewById(R.id.yourTextViewId)).getText(); 

    { 
// Button btnList=(Button)v.findViewById(R.id.listbutton); 

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

}); 
     } 

     }; 
     */ 


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 void onItemClick(AdapterView<?> arg0, View v, final int position, 
      long arg3) 
     { 
    holder.btnList=(Button)v.findViewById(R.id.listbutton); 

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

}); 
     } 
     */ 



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

    } 
+1

您可能需要在按鈕上添加'android:focusable =「false」'才能使它工作。 – Aprian

+0

看到這個tuts [用按鈕自定義列表視圖](http://samir-mangroliya.blogspot.in/p/android-customized-listview.html) –

+0

嘗試intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) – alezhka

回答

1

本示例可能對您有所幫助。

class EfficientAdapter extends BaseAdapter { 
    Context con; 

    public EfficientAdapter(Context con) { 
     this.con = con; 

    } 

    @Override 
    public int getCount() { 

     return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 

     return position; 
    } 

    @Override 
    public long getItemId(int position) { 

     return position; 
    } 

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

     LayoutInflater inflater = (LayoutInflater) con 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.homemplebrowview, parent, false); 

     Button row = (Button) rowView.findViewById(R.id.listbutton); 


     final int n = position; 
     row.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent next = new Intent(context, Home.class); 
        startActivity(next); 

      } 
     }); 

     return rowView; 
    } 
} 
+0

什麼是con(con.getSystemS)它向我顯示錯誤 –

+0

得到了上下文禮儀... if(convertView == null){\t i已使用這是否有用..然後有什麼區別convertView和rowview。 –

+0

(1)。如果不需要條件,我從來沒有使用過它,並且很好地工作過。 (2)。 con是我在類的頂部定義的上下文的對象。 (3)。 convertView是方法參數,rowview是我們從佈局文件創建的視圖。 –

1

你必須要像

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

private LayoutInflater mInflater; 
private Context context; 
protected ListView mListView; 
private int positions; 
ViewHolder holder; 
private Activity activity; 

,並在構造函數中

public EfficientAdapter(Activity act) { 
mInflater = LayoutInflater.from(context); 
//this.context = context; 
this.activity = act; 

他們意圖改變上下文活動的一些變化。

希望它有幫助

相關問題