我怎样才能使用不同类的视图?

时间:2014-07-16 10:37:43

标签: java android android-layout android-view

我搜索了很多,但我真的没找到我需要的东西。

我想要的是:

我有一个listview并将其加载到我的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);

myrow布局xml在这里:

< RelativeLayout   
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >
    <ProgressBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/my_prog"
    ></ProgressBar>
<TextView  
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"  
    android:textSize="25sp" >

</TextView>

</RelativeLayout >

你可以在textview看到我有进度条,所以我需要触及它并将其设置为可见。我的意思是有时我会将其设置为不可见有时可见

我做的是:

在我的 MainActivity on create method上,代码在此处:

ProgressBar my_prog;
my_prog=(ProgressBar)findViewById(R.id.my_prog);
my_prog.setVisibility(View.INVISIBLE);

错误是:

07-16 13:35:39.219: E/AndroidRuntime(29096): java.lang.RuntimeException: Unable to start activity ComponentInfo{....MainActivity}: java.lang.NullPointerException

我想我的主要活动无法达到 my_prog ,但我需要它。

我该怎么做。

感谢adnvace ......

1 个答案:

答案 0 :(得分:2)

您需要实现自定义Adapter才能执行此操作。我写了这个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;
    }
}

您可以像这样使用ExampleAdapter

if(friends != null) {
    ExampleAdapter adapter = new ExampleAdapter(Main.this, friends);
    setListAdapter(adapter);
}

如果您想在特定位置显示ProgressBar,请执行以下操作:

// Shows the ProgressBar in the first row
adapter.showProgressBar(0, true)