Skip to main content

Simple and sort Listview example (Android)


private View getChapterLayout() {
ListView listView = new ListView(this);
listView.setAdapter(new BaseAdapter() {

public int getCount() {
return chapterdata.size();
}

public Object getItem(int position) {
return chapterdata.get(position);
}

public long getItemId(int addressList) {
return 0;
}

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

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.button_chapter, null);

TextView textview = (TextView) convertView
.findViewById(R.id.button);
textview.setText(chapterdata.get(position).getName());
return convertView;
}



});
return listView;
}


// chapterdata is an arrayList


// Add Event to each Row



 listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
try {
runOnUiThread(new Runnable() {

@Override
public void run() {

}
});
} catch (Exception e) {
// TODO: handle exception
}



}
});




Comments

Popular posts from this blog

ProgressDialog example using handler android

final ProgressDialog dialog = ProgressDialog . show ( this , "Title" , "Message" , true ); final Handler handler = new Handler () { public void handleMessage ( Message msg ) { dialog . dismiss (); } } ; Thread t = new Thread () { public void run () { // write process code here. handler . sendEmptyMessage ( 0 ); } } ; t . start ();

How to set image button click effect ?

I have one simple solution: using that, you can set the default press effect to the button. // Create button and add event Button addImageView = (Button) bottomView.findViewById(R.id. addImage ); addImageView.setOnClickListener( new OnClickListener () { @Override public void onClick(View v) { // click event } }); //Using this code set touch listener addImageView.setOnTouchListener( touchEffect ); //touch listner OnTouchListener touchEffect = new OnTouchListener() {         public boolean onTouch(View v, MotionEvent event) {             switch (event.getAction()) {                 case MotionEvent. ACTION_DOWN : {                     v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode. SRC_ATOP );                     v....

Store array of String or int or list in SharedPreferences android

 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences( this );         JSONArray arr = new JSONArray ();         arr.put( "Viren" );         arr.put( "Android" );         prefs.edit().putString( "key" , arr.toString());         prefs.edit().commit();         try {             arr = new JSONArray (prefs.getString( "key" , "{}" ));             System. out .println(arr.getString(1));          } catch ( JSONException e) {             e.printStackTrace();         }