在listview

时间:2016-09-01 19:57:59

标签: android listview android-intent

我想要做的是以下

  1. 我的自定义适配器布局中有一个按钮,它与服务器上的php脚本交互以在服务器上下载文件
  2. 通过在后台调用IntentService来处理下载过程
  3. 服务完成后,我想更新在活动中启动的列表视图
  4. 我坚持使用Receiver
  5. 我的代码到目前为止如下:

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            if(IsRepository){
                view = vi.inflate(R.layout.filerepositorylayout, null);
            }
         }
          BindLayoutElemnts(view, position);
        return view;
      }
    

    这是调用Intent服务的地方:

     private void BindLayoutElemnts(View view, int position) {
          myFiles = (files) getItem(position);
           img_Download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                Intent intent = new Intent(getContext(),DownloadService.class);
                intent.putExtra("FileID",""+myFiles.getID());
                intent.putExtra("FileName",myFiles.getName());
                context.startService(intent);
            }
        });
     }
    

    通话活动

    这是Receiver被称为

    的地方
        public class user_repositry extends AppCompatActivity implements  DownloadReceiver.Receiver {
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           myDownloadReceiver = new DownloadReceiver(new Handler());
           myDownloadReceiver.setReceiver(this);
     }
    
      @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        Toast.makeText(user_repositry.this, "Downloaded", Toast.LENGTH_SHORT).show();
    
    }
    

    Intent service代码:

     ResultReceiver rec;
     @Override
    protected void onHandleIntent(Intent intent) {
          rec = intent.getParcelableExtra("DownloadReceiver");
    }
    
     private void UpdateDBRecord() {
        HashMap<String, String>  PostData=new HashMap<String, String>();
        PostData.put("call","UpdateFile");
        PostData.put("ID", ""+file_ID);
        BackgroundWorker registerWorker= new BackgroundWorker(this,this,PostData);
        registerWorker.setShowLoadingMessage(false);
        registerWorker.execute(Helper.getPhpHelperUrl());
    }
    

    此函数是Post execute上面的registerWorker中调用的函数

     @Override
    public void processFinish(String results) {
        Log.d("Download","Download DB updated");
        Bundle bundle = new Bundle();
        //bundle.putString("resultValue", "My Result Value. Passed in: " );
        // Here we call send passing a resultCode and the bundle of extras
        rec.send(Activity.RESULT_OK, bundle);
    }
    

    我现在遇到这个错误:

    Failed resolution of: Lcom/bassem/donateme/classes/DownloadReceiver;
    

    我知道接收器没有启动,但我似乎无法在我的逻辑中找到丢失的代码

    感谢您的帮助

2 个答案:

答案 0 :(得分:0)

缺少的代码不在你的逻辑中,它在DownloadReciever的逻辑中。更广泛地寻找错误,并弄清楚为什么没有加载。那或者从中发布一些代码以便可以回答它。例如,寻找一个不存在并因此崩溃的uri?什么是期望和失败,为什么失败。您没有发布任何崩溃的代码。您发布了调用崩溃内容的代码。

答案 1 :(得分:0)

试试这个,

第1步: 使用活动内的ResultReceiver创建结果处理程序

public ResultReceiver downloadReceiver = new ResultReceiver(new Handler()) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        //Broadcast /send the result code in intent service based on your logic(success/error) handle with switch
        switch (resultCode) {
            case 1: {
                //Get the resultData and do your logic here
                //Update your list view item here
                break;
            }
            case 2: {
                //Get the resultData and do your logic here
                //Update your list view item here
                break;
            }
        }
    }
};  

第2步:将结果处理程序放在intent中并启动意向服务

Intent intent = new Intent(getContext(), DownloadService.class);
intent.putExtra("receiver",downloadReceiver);

第3步:处理服务类中的意图(获取结果处理程序的值)

final ResultReceiver receiver;

@Override
protected void onHandleIntent(Intent intent) {
    receiver = intent.getParcelableExtra("receiver");
}

第4步:将数据发送/广播到结果处理程序

//based on your logic, change the result code and data

//Add your result data (success scenario)
Bundle bundle = new Bundle();
bundle.putString("result","Some text");
receiver.send(1,bundle);

//Add your result data (failure scenario)
receiver.send(0,bundle);