单击按钮更新ListView行项

时间:2014-01-11 11:54:40

标签: java android listview

我有一个带有一些TextView的ListView,一个ImageView&一个按钮。 ImageView是默认的“隐形”。在按钮的单击处理程序中,我必须执行一些无法在UI-Thread中完成的http调用,因此我为它创建了一个后台线程。根据http调用的结果,我必须显示ImageView(最初是'Invisible')。

以下是我的布局文件(list_row.xml)的一部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:padding="5dip" >

<ImageView
    android:id="@+id/imageViewIcon"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:visibility="invisible"
 />

<TextView
    android:id="@+id/textViewName"
    ...
/>

<Button
    android:id="@+id/button"
    ...
/>

以下是我设置适配器的方法:

ListView appList = (ListView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter(getApplicationContext(), MainActivity.this, MyDataInAnArray);
appList.setAdapter(adapter);

这是MyAdapter类:

public class MyAdapter extends BaseAdapter
{
    Context mContext;
    Context mActivity;
    SomeDataType[] mData;
    LayoutInflater mInflater;

public MyAdapter(Context context, Context activity, SomeDatatype[] data)
{
    mContext = context;
    mActivity = activity;
    mData = data;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

    ..... Some more required @Override functions here ....

    @Override
public View getView(final int pos, View convertView, ViewGroup parent)
{
    View vi = convertView;

    if (convertView == null)
        vi = mInflater.inflate(R.layout.list_row, null);

    TextView tvName = (TextView) vi.findViewById(R.id.textViewName);
    tvName.setText(mData[pos].name);

    final ImageView icon = (ImageView) vi.findViewById(R.id.imageViewIcon);

    final Button button = (Button) vi.findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Thread thread = new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    String result = Utils.httpPost("MyURLHere", SomePostData);
                    if (result != null)
                    {
                        ... and some more processing

                        **makeIconVisibile(icon);**
                    }
                }
            });
            thread.start();
        }
    });

    return vi;
}

问题:如何在函数makeIconVisible()中切换ImageView的可见性? 由于http响应处理是在后台线程中,我不能直接从那里修改visiblity。我将不得不在UIThread上运行它。 mContext.runOnUiThread和{ mActivity.runOnUiThread无效。我该如何修改以下功能才能使其正常工作?

private void makeIconVisible(ImageView icon)
{
    // What else is to be done here?
    icon.setVisibility(View.VISIBLE);
}

2 个答案:

答案 0 :(得分:2)

您可以使用AsyncTask尝试类似的内容。

private void makeIconVisible(ImageView icon)
{
    new MakeIconVisibleTask().execute(icon);
}

class MakeIconVisibleTask extends AsyncTask<View, Void, Integer> {
    private View mView;
    String mResult = null;

    @Override
    protected Integer doInBackground(View... arg0) {
        mView = arg0[0];

        mResult = Utils.httpPost("MyURLHere", SomePostData);

        int success = -1; 
        if(mResult!=null && !mResult.isEmpty()) {
            success = 1;
        }

        return success;
    }

    @Override
    protected void onPostExecute(Integer res) {
        if(res >0 ) {
            mView.setVisibility(View.VISIBLE);
        }
    }

}

答案 1 :(得分:1)

使用这个。

Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                String result = Utils.httpPost("MyURLHere", SomePostData);
                if (result != null)
                {
                    handler.sendEmptyMessage(0);
                }
            }
        });
        thread.start();



Handler handler = new Handler() { 
@Override 
public void handleMessage(Message msg) { 
    makeIconVisibile(icon);
} 

};