简体   繁体   English

单击按钮更新ListView行项

[英]Update ListView row items on click of button

I have a ListView with some TextViews, an ImageView & a Button. 我有一个ListView与一些TextViews,一个ImageView和一个按钮。 the ImageView is bydefault 'Invisible'. ImageView是默认的“隐形”。 In button's click handler, I have to do some http calls which can not be done in the UI-Thread, so I have created a background thread for it. 在按钮的单击处理程序中,我必须执行一些无法在UI-Thread中完成的http调用,因此我为它创建了一个后台线程。 Based on the result of the http call, I have to show the ImageView (which was initially 'Invisible'). 根据http调用的结果,我必须显示ImageView(最初是'Invisible')。

Here is a part of my layout file ( list_row.xml ): 这是我的布局文件( 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"
    ...
/>

Here is how I am setting the adapter: 以下是我设置适配器的方法:

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

and here is the MyAdapter class: 这是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;
}

Question : How should I toggle the visiblity of ImageView in the function makeIconVisible() ? 问题 :如何在函数makeIconVisible()切换ImageView的可见性? Since the http response processing is in a background thread, I can not modify the visiblity directly from there. 由于http响应处理是在后台线程中,我不能直接从那里修改visiblity。 I will have to run this on UIThread. 我将不得不在UIThread上运行它。 both mContext.runOnUiThread & mActivity.runOnUiThread are not working. mContext.runOnUiThreadmActivity.runOnUiThread都不起作用。 How should I modify the below function to make it work? 我应该如何修改以下功能才能使其正常工作?

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

You can try something similar to this, using AsyncTask. 你可以使用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);
        }
    }

}

Use this one. 使用这个。

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);
} 

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM