我试图确定一个包含每行不同布局的单个ListView的最佳方法.我知道如何创建自定义行+自定义数组适配器以支持整个列表视图的自定义行,但是如何在ListView中实现许多不同的行样式?
有人可以用简单的话来解释我的用法getViewTypeCount()和getItemViewType()方法ArrayAdapter吗?
我在ListView中获得了重复的项目.向后和向下滚动有时会更改项目顺序.我用谷歌搜索并发现许多线程报告了这个错误,但没有一个帮助我解决我的问题.
这是我的代码:
活动:
package com.github.progval.SeenDroid;
import java.util.ArrayList;
import java.util.List;
import com.github.progval.SeenDroid.lib.Connection;
import com.github.progval.SeenDroid.lib.Message;
import com.github.progval.SeenDroid.lib.MessageFetcher;
import com.github.progval.SeenDroid.lib.Query.ParserException;
import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class ShowUserActivity extends ListActivity {
private Connection connection;
public ArrayList<Message> listMessages = new ArrayList<Message>();
public MessageAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
this.connection = new Connection();
this.setTitle(R.string.homefeed_title);
this.listMessages = new MessageFetcher(this.connection).fetchUser();
this.bindUi();
}
private void bindUi() {
this.adapter = new MessageAdapter(this, this.listMessages); …Run Code Online (Sandbox Code Playgroud) 在我的自定义列表视图中,项目正在重复.项目的位置对于所有项目是相同的.代码如下
ListAdapter.java-
public class ListAdapter extends BaseAdapter{
private List<String> mName;
private List<Drawable> mIcon;
private Context mContext;
public ListAdapter(Context mContext, List<String> Name, List<Drawable> Icon) {
this.mContext=mContext;
this.mName=Name;
this.mIcon=Icon;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mName.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View v, ViewGroup parent) { …Run Code Online (Sandbox Code Playgroud) 希望每个人都好;
我知道这个问题已经提前几次审查过,但经过长时间的搜索,我仍然没有找到解决方案.
我的自定义列表视图每6项重复一次项目.
已经检查并尝试过:
1- layout_width和layout_height不包含wrap_content
2- holder = new ListViewItem()在任何内容初始化之前
3-有一个"convertView!= null"
4- holder.linearLayout.getChild()不能在我的情况下使用,因为布局不是Linear
5-晴()
如果有人可以帮助我,这是我的代码
CustomListViewAdapter.java的getView()
public View getView(final int position, View convertView, ViewGroup parent) {
ListViewItem item = items.get(position);
ListViewItem holder;
View vi=convertView;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(vi==null){
vi = inflater.inflate(R.layout.item, null);
holder = new ListViewItem();
holder.cb = (CheckBox) vi.findViewById(R.id.Item1);
holder.sp = (Spinner) vi.findViewById(R.id.Item2);
holder.title = (TextView) vi.findViewById(R.id.Item3);
holder.pricetitle= (TextView) vi.findViewById(R.id.item4);
holder.Descriptiontitle= (TextView) vi.findViewById(R.id.Item5);
vi.setTag(holder);
}else{
holder = (ListViewItem) vi.getTag();
}
holder.title.setText(item.ItemTitle);
holder.pricetitle.setText(item.price+"");
holder.Descriptiontitle.setText(item.Description);
return …Run Code Online (Sandbox Code Playgroud) android listview duplicates android-listview listview-adapter
我有一个列表,其中我从共享首选项获得的具体数字(位置)应显示imageview(表示当前正在播放的歌曲).但我得到了这个位置,但该项目也显示在其他行上.滚动列表时出现问题.
*当我退出另一个活动时,就会发生这种情况,并且在我的简历上我会这样做:
@Override
protected void onResume() {
super.onResume();
if(AlbumDetails.mediaPlayer!=null)
adapter = new PlaylistAdapter(this, songs);
list.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class PlaylistAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater = null;
private ArrayList<Songs> data;
private DatabaseHelper db;
private SharedPreferences prefs;
int playpos;
public PlaylistAdapter(Activity a, ArrayList<Songs> songs) {
activity = a;
data = songs;
db = new DatabaseHelper(a);
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
prefs = activity.getSharedPreferences("com.darkovski.quran",
Context.MODE_PRIVATE);
playpos = prefs.getInt("posPlaying", -1);
}
public int getCount() {
return data.size();
} …Run Code Online (Sandbox Code Playgroud)