1

任何可以解决我的问题..最后经过2天的斗争,我清楚地得到了70%的输出。经验:我有listview哪些数据来自服务器..内部Listview有一些文本,按钮和滚动索引A,BC,D,E,F,G。
问题:按钮不工作意味着当我点击它时...如果添加下面的代码。 其寻找一些以下图片是什么:按钮在列表视图适配器

在这里,我曾使用XML以这种方式---> com.woozzu.android.widget.IndexableListView 在本案例按钮ONCLICK工作不滚动视图A,B,C为表示正确的方式和工作很好..

enter image description here

状态1: -

homempleb.xml

<LinearLayout 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:background="#335552" 
     android:layout_below="@+id/relativeLayout4" >      
<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" /> 

MainActivity.java

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


@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.setFastScrollAlwaysVisible(true); 
    objectAdapter = new EfficientAdapter(this);  
    listView.setAdapter(objectAdapter); 

}

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

    } 

状态2: -

在这种情况下点击按钮工作正常,但滚动视图A,B,C也工作正常,但在一边将不会有字母A,B,C,D,E,F,G ... 如果我ADD在homempleb.xml中的列表视图,那么这两个都将工作得很好,但没有像在图像中显示的字母索引。我希望按钮和滚动索引像上面的图像一样工作..我不擅长这个话题。 plz帮助我 它显示了这种方式:

enter image description here

homempleb.xml

<LinearLayout 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:background="#335552" 
     android:layout_below="@+id/relativeLayout4" >      
    <ListView 
      android:id="@+id/homelistView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"    
      android:layout_weight="1.04" 
      android:dividerHeight="0dip" /> 

+1

的可能重复[如何添加适配器列表视图的onclick按钮(http://stackoverflow.com/questions/12637301/how-add-onclick-button-in-adapter-listview)请不要再次提出同样的问题。如果您有要添加的内容,请编辑第一个问题并添加新的数据/信息。 – Luksprog

+0

@Luksprog这是不同的问题。下次我做你的评论.. –

回答

4

如果有人还在关心:

使用TouchEvent由索引滚动逮住。 请参阅https://github.com/denley/IndexableListView/commit/18210a54487ba079bb332fafec709e2de26883db

只要改变那几行就可以了。

编辑:我刚刚出于好奇。链接已经死亡。我猜这个错误是在最近的版本中解决的(请查看最近更新的original IndexableListView project)。我不知道是什么的变化是,甚至问题是了,所以在这里什么是恕我直言,本来是负责的问题,以防万一我的工作职能:

IndexedListView.java

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    if (mScroller != null && mScroller.onTouchEvent(ev)) 
     return true; 
    try{ 
    if (mGestureDetector == null) { 
     mGestureDetector = new GestureDetector(getContext(), 

       new GestureDetector.SimpleOnGestureListener() { 
        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, 
          float velocityX, float velocityY) { 
         if(mScroller!=null){ 
         mScroller.show(); 
         } 
         return super.onFling(e1, e2, velocityX, velocityY); 
        } 

       }); 

    } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    mGestureDetector.onTouchEvent(ev); 

    return super.onTouchEvent(ev); 
} 

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

    return super.onInterceptTouchEvent(ev); 
    } 
    return false; 
} 

IndexedScroller。java的

public boolean onTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     if (mState != STATE_HIDDEN && contains(ev.getX(), ev.getY())) { 
      setState(STATE_SHOWN); 
      mIsIndexing = true; 

      mCurrentSection = getSectionByPoint(ev.getY()); 

      mListView.setSelection(mIndexer 
        .getPositionForSection(mCurrentSection)); 
      return true; 
     } 
     break; 
    case MotionEvent.ACTION_MOVE: 
     if (mIsIndexing) { 

      if (contains(ev.getX(), ev.getY())) { 

       mCurrentSection = getSectionByPoint(ev.getY()); 
       mListView.setSelection(mIndexer 
         .getPositionForSection(mCurrentSection)); 
      } 
      return true; 
     } 
     break; 
    case MotionEvent.ACTION_UP: 
     if (mIsIndexing) { 
      mIsIndexing = false; 
      mCurrentSection = -1; 
     } 
     if (mState == STATE_SHOWN) 
      setState(STATE_HIDING); 
     break; 
    } 
    return false; 
} 
+1

感谢它的工作......非常感谢你 – maverickosama92