Java 如何使用来自不同类的视图?
我已经搜索了很多,但我真的没有找到我需要的东西 我想要的是: 我有一个列表视图,并将其加载到我的Java 如何使用来自不同类的视图?,java,android,android-layout,android-view,Java,Android,Android Layout,Android View,我已经搜索了很多,但我真的没有找到我需要的东西 我想要的是: 我有一个列表视图,并将其加载到我的main类中。我这样加载它: ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this, R.layout.myrow,R.id.text ); if (friends != null) { for (Par
main
类中。我这样加载它:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this,
R.layout.myrow,R.id.text );
if (friends != null) {
for (ParseObject friend : friends) {
adapter.add((String) friend.get("name"));
}
}
setListAdapter(adapter);
if(friends != null) {
ExampleAdapter adapter = new ExampleAdapter(Main.this, friends);
setListAdapter(adapter);
}
你可以看到我在textview
上有进度条,所以我需要到达它并将其设置为可见。我的意思是有时我将其设置为不可见有时可见
我所做的是:
在我的main活动中在创建方法中
代码如下:
ProgressBar my_prog;
my_prog=(ProgressBar)findViewById(R.id.my_prog);
my_prog.setVisibility(View.INVISIBLE);
错误是:
我想我的主要活动无法达到我的进度,但我需要它
我该怎么做呢
感谢adnvace…要做到这一点,您需要实现一个自定义的
适配器。我写了这个ExampleAdapter
来向您展示它是如何工作的,我对所有重要的部分都做了注释:
public class ExampleAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final List<ParseObject> objects;
private final boolean[] activated;
public ExampleAdapter(Context context, List<ParseObject> objects) {
this.inflater = LayoutInflater.from(context);
this.objects = objects;
this.activated = new boolean[objects.size()];
}
public void showProgressBar(int position, boolean visible) {
this.activated[position] = visible;
notifyDataSetChanged();
}
@Override
public int getCount() {
return this.objects.size();
}
@Override
public ParseObject getItem(int position) {
return this.objects.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If the convertView is null, we need to inflate a new one
if(convertView == null) {
// We inflate the view with your layout
convertView = inflater.inflate(R.layout.myrow, parent, false);
// Here we create a view holder object which keeps a
// reference to the Views in this row so we have to
// perform the expensive findViewById() only once
ViewHolder holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.my_prog = (ProgressBar) convertView.findViewById(R.id.my_prog);
// The view holder is set as tag to the view so we can access it later
convertView.setTag(holder);
}
// We retrieve the ParseObject for the current position
ParseObject parseObject = getItem(position);
// And get the view holder from the View
ViewHolder holder = (ViewHolder) convertView.getTag();
// We read the data we need from the ParseObject
String name = parseObject.get("name");
// And here is the visibility logic
int progressBarVisibility = this.activated[position] ? View.VISIBLE : View.GONE;
int textViewVisibility = this.activated[position] ? View.GONE : View.VISIBLE;
// Now we set the data to the Views through the view holder
holder.text.setText(name);
holder.text.setVisibility(textViewVisibility);
holder.my_prog.setVisibility(progressBarVisibility);
return convertView;
}
// This is our view holder class. It keeps a reference to the Views
// inside each row so we have to perform the expensive
// findViewById() only once
private class ViewHolder {
public TextView text;
public ProgressBar my_prog;
}
}
如果要在特定位置显示ProgressBar
,只需执行以下操作:
// Shows the ProgressBar in the first row
adapter.showProgressBar(0, true)
您需要保留父视图(视图持有者)的引用,然后找到其子视图(进度条),以便对其执行任何操作。您是否使用listview?是的,我在my main.xml上使用listview,但您可以看到适配器如何加载它,如何使用视图持有者?为了更改listview,您需要了解notify数据集更改。在这种情况下,父视图引用将是一件非常危险的事情。下面是您的ans。。。。。这是完成任务的最佳方式。在主活动中,我可以设置我的进度可见性吗?是一次性设置每行中所有进度条的可见性,还是单独设置每行中所有进度条的可见性?我想在用户单击listview上的某个项目时设置其中一个进度条。我想隐藏单击的文本并仅在那里显示进度条。因此,我需要在主要活动中这样做,当单击时,我会修改上面的代码以满足您的需要。但是您错了,大部分魔法发生在适配器中。main活动
不应该对列表视图中的内容了解太多。这只会让事情变得更复杂和潜在的危险。谢谢你的回答,那么我该怎么办呢?我的意思是,我有一个列表视图,当一个人点击该项目并在该项目上显示progressbar时,我需要分解项目文本。再次感谢
if(friends != null) {
ExampleAdapter adapter = new ExampleAdapter(Main.this, friends);
setListAdapter(adapter);
}
// Shows the ProgressBar in the first row
adapter.showProgressBar(0, true)