如何在列为空时使用 BaseAdapter 动态删除一行 table?
How to remove dynamically a row of table using BaseAdapter when a column is empty?
我的问题在上面。我的代码如下。
如您所见,一些数据是空的,例如“20:11”、“”、“11:25”……
我希望当字段为空时立即删除布局 inflate 中的行,或者当字段不为空时动态创建更好的解决方案。
public class Zlecenia extends Activity {
ListView listview;
ArrayList<ListData> myList = new ArrayList<>();
String[] strefa = new String[]{
"Jerozolimskie 20/22", "Title 2", "Title 3", "Title 4",
"Title 5", "Title 6", "Title 7", "Title 8"
};
String[] adres = new String[]{
"Desc 1", "Desc 2", "Desc 3", "Desc 4",
"Desc 5", "Desc 6", "Desc 7", "Desc 8"
};
String[] data = new String[]{
"20:11", "", "11:25", "", "15:11", "11:25", "15:11", "11:25"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zlecenia);
listview = (ListView) findViewById(R.id.listView);
for (int i = 0; i < strefa.length; i++) {
ListData ld = new ListData();
ld.setStrefa(strefa[i]);
ld.setAdres(adres[i]);
ld.setData(data[i]);
myList.add(ld);
}
listview.setAdapter(new MyBaseAdapter(this, myList));
}
}
和 BaseAdapter:
public class MyBaseAdapter extends BaseAdapter {
// Other methods of extends BaseAdapter
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.zlecenia_lista, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.strefa.setText(currentListData.getStrefa());
mViewHolder.adres.setText(currentListData.getAdres());
mViewHolder.data.setText(currentListData.getData());
return convertView;
}
private class MyViewHolder {
TextView strefa, adres, data;
public MyViewHolder(View item) {
strefa = (TextView) item.findViewById(R.id.strefa);
adres = (TextView) item.findViewById(R.id.adres);
data = (TextView) item.findViewById(R.id.data);
}
}
}
因此如果数据等于“”,您希望删除(隐藏)列表的所有行(项目)
这是我的建议:
public class MyBaseAdapter extends BaseAdapter {
// Other methods of extends BaseAdapter
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.zlecenia_lista, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.strefa.setText(currentListData.getStrefa());
mViewHolder.adres.setText(currentListData.getAdres());
mViewHolder.data.setText(currentListData.getData());
// try this
if(currentListData.getData().equals("")) {
convertView.setVisibility(View.GONE) // all the row is hided
}else {
convertView.setVisibility(View.VISIBLE) // display the row
}
return convertView;
}
private class MyViewHolder {
TextView strefa, adres, data;
public MyViewHolder(View item) {
strefa = (TextView) item.findViewById(R.id.strefa);
adres = (TextView) item.findViewById(R.id.adres);
data = (TextView) item.findViewById(R.id.data);
}
}
}
希望对您有所帮助,如有任何问题请发表评论!
祝你好运!
我的问题在上面。我的代码如下。 如您所见,一些数据是空的,例如“20:11”、“”、“11:25”…… 我希望当字段为空时立即删除布局 inflate 中的行,或者当字段不为空时动态创建更好的解决方案。
public class Zlecenia extends Activity {
ListView listview;
ArrayList<ListData> myList = new ArrayList<>();
String[] strefa = new String[]{
"Jerozolimskie 20/22", "Title 2", "Title 3", "Title 4",
"Title 5", "Title 6", "Title 7", "Title 8"
};
String[] adres = new String[]{
"Desc 1", "Desc 2", "Desc 3", "Desc 4",
"Desc 5", "Desc 6", "Desc 7", "Desc 8"
};
String[] data = new String[]{
"20:11", "", "11:25", "", "15:11", "11:25", "15:11", "11:25"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zlecenia);
listview = (ListView) findViewById(R.id.listView);
for (int i = 0; i < strefa.length; i++) {
ListData ld = new ListData();
ld.setStrefa(strefa[i]);
ld.setAdres(adres[i]);
ld.setData(data[i]);
myList.add(ld);
}
listview.setAdapter(new MyBaseAdapter(this, myList));
}
}
和 BaseAdapter:
public class MyBaseAdapter extends BaseAdapter {
// Other methods of extends BaseAdapter
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.zlecenia_lista, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.strefa.setText(currentListData.getStrefa());
mViewHolder.adres.setText(currentListData.getAdres());
mViewHolder.data.setText(currentListData.getData());
return convertView;
}
private class MyViewHolder {
TextView strefa, adres, data;
public MyViewHolder(View item) {
strefa = (TextView) item.findViewById(R.id.strefa);
adres = (TextView) item.findViewById(R.id.adres);
data = (TextView) item.findViewById(R.id.data);
}
}
}
因此如果数据等于“”,您希望删除(隐藏)列表的所有行(项目)
这是我的建议:
public class MyBaseAdapter extends BaseAdapter {
// Other methods of extends BaseAdapter
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.zlecenia_lista, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.strefa.setText(currentListData.getStrefa());
mViewHolder.adres.setText(currentListData.getAdres());
mViewHolder.data.setText(currentListData.getData());
// try this
if(currentListData.getData().equals("")) {
convertView.setVisibility(View.GONE) // all the row is hided
}else {
convertView.setVisibility(View.VISIBLE) // display the row
}
return convertView;
}
private class MyViewHolder {
TextView strefa, adres, data;
public MyViewHolder(View item) {
strefa = (TextView) item.findViewById(R.id.strefa);
adres = (TextView) item.findViewById(R.id.adres);
data = (TextView) item.findViewById(R.id.data);
}
}
}
希望对您有所帮助,如有任何问题请发表评论!
祝你好运!