Playing with lists in Android - Part II

I hope you already read my first topic about listView. In part II, I am just extending the part I topic. So please read part I before reading part II.

In this, I am creating an adapter, in which each list item consists of two views, a TextView and an IMageView respectively. I am putting these two views in a relative layout. Then I am aligning the imageView to the right end of the layout using the addRule(). One more thing is, here I am putting a static image, that is the standard Android icon.

The adapter class is,



class MyAdapter extends BaseAdapter {

Context mContext;

public MyAdapter(Context context) {
mContext = context;
}

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

public Object getItem(int id) {
return id;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout layout = new RelativeLayout(mContext);

android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams
(android.widget.RelativeLaout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

android.widget.RelativeLayout.LayoutParams imgParams = new android.widget.RelativeLayout.LayoutParams\(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

TextView tv = new TextView(mContext);
ImageView img = new ImageView(mContext);
tv.setText(list.get(position));
img.setImageResource(R.drawable.icon);
layout.addView(tv, params);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(img, imgParams);
return layout;
}
}



The complete code is,



public class SampleListView_Chatextends Activity {

ListView lView;
ArrayList list;
EditText eText;
Button but;
ArrayAdapter adapter;
LinearLayout layout, subLayout;
LayoutParams params, lParams;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
subLayout = new LinearLayout(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
eText = new EditText(this);
but = new Button(this);
lView = new ListView(this);
list = new ArrayList();
but.setText("Send");
/*adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);*/
lView.setAdapter(new MyAdapter(this));
but.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
list.add(eText.getText().toString());
eText.setText(null);
/*lView.setAdapter(adapter);*/
lView.setAdapter(new MyAdapter(SampleListView_Chat.this));
}
});
lParams.height = 380;
layout.addView(lView, lParams);
eText.setWidth(260);
subLayout.addView(eText, params);
subLayout.addView(but, params);
layout.addView(subLayout, params);
setContentView(layout);
}

class MyAdapter extends BaseAdapter {

Context mContext;

public MyAdapter(Context context) {
mContext = context;
}

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

public Object getItem(int id) {
return id;
}

public long getItemId(int position) {
return position;
}

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

RelativeLayout layout = new RelativeLayout(mContext);

android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

android.widget.RelativeLayout.LayoutParams imgParams = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

TextView tv = new TextView(mContext);
ImageView img = new ImageView(mContext);
tv.setText(list.get(position));
img.setImageResource(R.drawable.icon);
layout.addView(tv, params);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(img, imgParams);
return layout;
}
}
}



Screenshot

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication