我有一个ListView for RSS,我动态填充。我想为项目设置样式。 我怎么能这样做?
答案 0 :(得分:2)
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:gravity="right"
android:paddingRight="5dp"
android:text="@string/app_name" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bullet"
android:contentDescription="@string/app_name" />
</LinearLayout>
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mCustomList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#666666"
android:dividerHeight="0.05dp"
android:scrollingCache="false" />
</LinearLayout>
CstListAdapter.java
public class CstListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<CstItem> postList = new ArrayList<CstItem>();
private static LayoutInflater inflater = null;
private Context ctx;
public CstListAdapter(Activity act, ArrayList<CstItem> d, Context ctx) {
activity = act;
postList = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.ctx = ctx;
}
@Override
public int getCount() {
return postList.size() < count ? postList.size() : count;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView text;
public ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View row = convertView;
if (row == null) {
row = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.text= (TextViewPlus) row.findViewById(R.id.text);
holder.img= (TextViewPlus) row.findViewById(R.id.img);
row.setTag(holder);
} else
holder = (ViewHolder) row.getTag();
CstItem item = postList.get(position);
holder.text.setText(item.getTitle());
return row;
}
CstItem.java
public class CstItem {
private String title;
private String thumbnail;
private String url;
private String description;
private String pubDate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
...
}
Main.java
public class Main extends Activity {
private ListView mListView;
private CstListAdapter cstListAdaptor;
private ArrayList<CstItem> PostList = new ArrayList<CstItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.mCustomList);
cstListAdaptor = new CstListAdapter(this, PostList, this);
mListView.setAdapter(cstListAdaptor);
PostList.clear();
refreshRss();
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CstItem cit = PostList.get(position);
String cnt = cit.getDescription();
//...
}
});
}
...
}