I want image before textview and want to customize TextView in each row but it is hard for me to implement it because there is already xml such layout file simple_li
Create a custom_item
inside layout folder
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/listItemImgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/done"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/listItemTxtView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Create a custom adapter like this
public class CustomAdapter extends ArrayAdapter<Tweet> {
private Context mContext;
private int layoutId;
private ArrayList<Tweet> dataList;
public CustomAdapter(Context context, int resourceId,
ArrayList<Tweet> objects) {
super(context, resourceId, objects);
// TODO Auto-generated constructor stub
mContext = context;
layoutId = resourceId;
dataList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutId, null);
viewHolder = new ViewHolder();
viewHolder.listItemTxtView = (TextView) convertView.findViewById(R.id.listItemTxtView);
viewHolder.listItemImgView = (ImageView) convertView.findViewById(R.id.listItemImgView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.listItemTxtView.setText(dataList.get(position).toString());
//place picasso jar into libs folder of your project and use it for download and set images like this
Picasso.with(context).load("url of image you want to load").into(viewHolder.listItemImgView);
return convertView;
}
private class ViewHolder {
TextView listItemTxtView;
ImageView listItemImgView;
}
}
download picasso jar from here
instead of this
ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(activity, android.R.layout.simple_list_item_1, twits);
setListAdapter(adapter);
Use this
CustomAdapter adapter = new CustomAdapter(MainActivity.this, R.layout.custom_item, twits);
setListAdapter(adapter);