如何列出图像视图?任何人都知道如何使用此代码执行此操作



我已经尝试在ListView中显示图像,但是在此行出现错误:

status.setImageResource(R.drawable.inprogress); 

*未为字符串定义设置图像资源。

我正在尝试根据状态显示图像,如果状态等于"正在进行中",则进行中的图标将显示在 ListView 中。

这是我的完整代码:

public class SenaraiSemuaComplaint extends ListActivity {
       String stud_staff_ID;
       private ProgressDialog pDialog;
       JSONParser jParser = new JSONParser();

       ArrayList<HashMap<String, String>> listcomplaint;
       final Context context = this;
       private static String url_listcomplaint = "...";

        private static final String TAG_SUCCESS = "success";
        private static final String TAG_REGISTER = "register";
        private static final String TAG_PID = "pid";
        private static final String TAG_DATE = "date";
        private static final String TAG_STATUS = "status";

        JSONArray register = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listallcomplaint_layout);
    getActionBar().setHomeButtonEnabled(true);
    String input = getIntent().getStringExtra("stud_staff_ID");
    Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    listcomplaint = new ArrayList<HashMap<String, String>>();
    new LoadAllComplaint().execute();

     ListView lv = getListView();
     lv.setTextFilterEnabled(true);



     lv.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {
             String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                     .toString();
             String location = ((TextView) view.findViewById(R.id.location)).getText()
                     .toString();
             String picture = ((TextView) view.findViewById(R.id.picture)).getText()
                     .toString();

                                String input = getIntent().getStringExtra("stud_staff_ID");


                                 Intent in = new Intent(getApplicationContext(),
                                         ViewPage.class);
                                 in.putExtra("location", location);
                                 in.putExtra("picture", picture);
                                 in.putExtra("stud_staff_ID", input);
                                 in.putExtra("pid", pid);
                                 startActivityForResult(in, 100);
                            }
                        });



         }
public boolean onOptionsItemSelected(MenuItem item){
        if(android.R.id.home == item.getItemId()){
        finish();
    }

        return super.onOptionsItemSelected(item);
    }


 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == 100) {
         Intent intent = getIntent();
         finish();
         startActivity(intent);
     }
 }

 class LoadAllComplaint extends AsyncTask<String, String, String> {

     @Override
     protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(SenaraiSemuaComplaint.this);
         pDialog.setMessage("Please wait, Loading Data...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(false);
         pDialog.show();
     }

     protected String doInBackground(String... args) {

         String input = getIntent().getStringExtra("stud_staff_ID");
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("input", input));

         JSONObject json = jParser.makeHttpRequest(url_listcomplaint, "GET", params);

         Log.d("List Complaint: ", json.toString());
         try {
             int success = json.getInt(TAG_SUCCESS);
             if (success == 1) {
                 register = json.getJSONArray(TAG_REGISTER);

                 for (int i = 0; i < register.length(); i++) {
                     JSONObject c = register.getJSONObject(i);


                  // keep to variable
                     String pid = c.getString(TAG_PID);
                     String date = c.getString(TAG_DATE);
                     String status = c.getString(TAG_STATUS);
                     if (status.equals("Complaint in process."))
                     {
                         status.setImageResource(R.drawable.inprogress);
                     }

                     String location = c.getString("location");
                     String picture = c.getString("picture");
                     HashMap<String, String> map = new HashMap<String, String>();


                     map.put(TAG_PID, pid);
                     map.put(TAG_DATE, date);
                     map.put(TAG_STATUS, status);
                     map.put("location", location);
                     map.put("picutre", picture);

                     listcomplaint.add(map);
                 }
             } else {

                 runOnUiThread(new Runnable()
                    {
                        public void run(){
                            String msgs = "No complaint found, please create new complaint.";
                                Toast.makeText(getApplicationContext(), msgs, Toast.LENGTH_SHORT).show();
                        }
                    });

                         String i = getIntent().getStringExtra("stud_staff_ID");
                         Intent in12 = new Intent(getApplicationContext(),ComplaintBaru.class);
                         in12.putExtra("stud_staff_ID", i);
                         startActivity(in12);


             }
         } catch (JSONException e) {
             e.printStackTrace();
         }
         return null;
     }

     protected void onPostExecute(String file_url) {
         pDialog.dismiss();
         runOnUiThread(new Runnable() {
             public void run() {
                 ListAdapter adapter = new SimpleAdapter(
                         SenaraiSemuaComplaint.this, listcomplaint,
                         R.layout.list_pendaftaran, new String[] {TAG_PID,
                                 TAG_DATE, TAG_STATUS, "location", "picture" },
                         new int[] {R.id.pid, R.id.inputDate, R.id.inputStatus, R.id.location, R.id.picture});
                 setListAdapter(adapter);
             }
         });
     }
 }
}

我建议根据您想要实现的目标来判断,以便在ListView适配器中处理此问题,这将是您提供给ListActivity的适配器。

在适配器的getView()检查TextView的值,然后将其替换为ProgressBar,可以动态地替换它,或者通过让它驻留在行的布局中来切换其可见性。

需要

采用ImageView而不是设置为Image

Imageview img=(ImageView)findViewById(R.id.yourimageID).setImageResource(R.drawable.inprogress);

最新更新