Home > Mobile >  Has set the setOnItemClickListener, haven't why click reaction
Has set the setOnItemClickListener, haven't why click reaction

Time:02-09

 public class MainActivity extends the Activity 
{

Private ListView mListView;
Private TextView tv1.
Public void onCreate (Bundle savedInstanceState)
{
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
MListView=(ListView) the findViewById (R.i d.l istInfo);
Tv1=(TextView) the findViewById (R.i d.l ist_title);

//initialization data
List List=new ArrayList (a);
for (int i=0; I & lt; 10; I++) {
The Content of m.
If (I & lt; 3)
M=new Content (" A ", "b");
Else if (I & lt; 6)
M=new Content (" F ", "party");
The else
M=new Content (" D ", "east");
List. Add (m);
}

//instantiate custom content adaptation class
MyAdapter adapter=new MyAdapter (this, list, tv1);
//for listView
MListView. SetAdapter (adapter);
MListView. SetOnItemClickListener (new AdapterView. OnItemClickListener () {
@ Override
Public void onItemClick (AdapterView<?> The parent, View View, int position, long id) {
}
});

}

}


 public class MyAdapter extends BaseAdapter {

Private List List=null;
Private Context mContext;
TextView TV;

Public MyAdapter (Context mContext List The list, the TextView TextView) {
Enclosing mContext=mContext;
Enclosing a list=list;
TV=textview;
}

Public void onItemClick (AdapterView<?> The parent, View View, int position, long id) {
Toast. MakeText (mContext, (String) the getItem (position), Toast. LENGTH_SHORT), show ();
TV. SetText ((String) the getItem (position));
}


Public int getCount () {
Return this. The list. The size ();
}

Public Object the getItem (int position) {
return null;
}

Public long getItemId (int position) {
Return the position;
}

Public View getView (final int position, View View and ViewGroup arg2) {
Final String item=(String) the getItem (position);
ViewHolder ViewHolder=null;
If (view==null) {
ViewHolder=new viewHolder ();
View=LayoutInflater. The from (mContext). Inflate (R.l ayout. Item, null);
ViewHolder. TvTitle=(TextView) view. The findViewById (R.i which itle);
ViewHolder. TvLetter=(TextView) view. The findViewById (R.i, dc atalog);
The setTag (viewHolder);
} else {
ViewHolder=(viewHolder) the getTag ();
}

The final Content mContent=list. Get (position);

If (position==0) {
ViewHolder. TvLetter. SetVisibility (the VISIBLE).
ViewHolder. TvLetter. SetText (mContent getLetter ());
} else {
String lastCatalog=list. Get (position 1) getLetter ();
If (mContent. GetLetter (.) the equals (lastCatalog)) {
ViewHolder. TvLetter. SetVisibility (View. GONE);
} else {
ViewHolder. TvLetter. SetVisibility (the VISIBLE).
ViewHolder. TvLetter. SetText (mContent getLetter ());
}
}

ViewHolder. TvTitle. SetText (enclosing a list. The get (position). The getName ());

Return the view;

}




Final static class ViewHolder {
TextView tvTitle;
TextView tvLetter;
}


Public Object [] getSections () {
//TODO Auto - generated method stub
return null;
}

Public int getSectionForPosition (int position) {

return 0;
}

Public int getPositionForSection (int section) {
The Content mContent;
String l;
If (section=='! ') {
return 0;
} else {
for (int i=0; I & lt; GetCount (); I++) {
MContent=(Content) list. Get (I);
L=mContent. GetLetter ();
Char firstChar=l.t oUpperCase (). The charAt (0);
If (firstChar==section) {
Return the I + 1;
}

}
}
MContent=null;
L=null;
return -1;
}
}




CodePudding user response:

 

MListView. SetOnItemClickListener (new AdapterView. OnItemClickListener () {
@ Override
Public void onItemClick (AdapterView<?> The parent, View View, int position, long id) {
//there is no writing?
}
});

CodePudding user response:

You setOnItemClickListener is not empty
  • Related