Hello Dudes我在ListView项目中有一个ListView:
<Button
android:id="@+id/button_comments_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Comments"
/>
<ListView
android:id="@+id/post_comment_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
内部:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/post_fragment_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" >
</ListView>
</LinearLayout>
但内部列表视图(项目)的适配器始终位于位置0:
public class PostsCommentsAdapter extends BaseAdapter {
int ITEM_TEXT_TYPE = 0;
int ITEM_IMAGE_TYPE = 1;
LayoutInflater mLayoutInflater;
Context mContext;
public PostsCommentsAdapter(Context context, int layout, Cursor c, String[] from,
int[] to,SparseArray<String> list ,int flags) {
super();
this.mContext = context;
this.list = list;
mLayoutInflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
System.out.println("List Size : "+list.size());
return list.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
System.out.println("Getting for Position : "+position+" is The :
"+list.get(position));
// mCursor.moveToPosition(commentsidslist.get(position));
System.out.println("To Get Type"+list.get(position));
String type = (list.get(position).split("\\&&DETAIL&&"))[1];
int chooser = -1;
// 1 = like 2 = dislike 3 = comment 4 = imageurl
switch (type) {
case "C":
chooser = 0;
break;
case "P":
chooser = 1;
break;
default:
break;
}
System.out.println("TYPE "+type);
return chooser;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return list.get(position).hashCode();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int Demand = getItemViewType(position);
System.out.println("Demand :"+Demand);
if (convertView == null || convertView.getTag() == null) {
holder = new ViewHolder();
System.out.println("ConvertView null");
switch (Demand) {
case 0:
//text
convertView = mLayoutInflater.inflate(R.layout.post_list_row_text, null);
holder.tv = (TextView)convertView.findViewById(R.id.post_detail_row_text);
holder.like = (Button)convertView.findViewById(R.id.button_comment_like);
holder.dislike =
(Button)convertView.findViewById(R.id.button_comment_dislike);
break;
case 1:
//picture
convertView = mLayoutInflater.inflate(R.layout.post_list_row_picture,
null);
holder.iv =
(ImageView)convertView.findViewById(R.id.comment_row_imageview);
holder.like = (Button)convertView.findViewById(R.id.button_comment_like);
holder.dislike =
(Button)convertView.findViewById(R.id.button_comment_dislike);
break;
default:
break;
}
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
switch (Demand) {
case 0:
holder.tv.setText((list.get(position).split("\\&&DETAIL&&"))[3]);
holder.like.setText("0 For Now");
//if
holder.like.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
break;
case 1:
Picasso.with(mContext).load(((list.get(position).split("\\&&DETAIL&&"))
[3])).resize(150, 150).into(holder.iv);
holder.like.setText("0 For Now");
//if
holder.like.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
break;
default:
break;
}
System.out.println("Post Row Detail Item : "+((list.get(position))));
return convertView;
}
我几乎尝试了所有的事情,但它没有工作,我的问题伙伴在哪里?!
答案 0 :(得分:0)
您应该使用可扩展的listView。在此处获取教程:http://androidexample.com/Custom_Expandable_ListView_Tutorial