如何根据自定义列表视图中的条件设置图像

时间:2015-10-01 08:26:17

标签: android

我只想根据自己的情况将图像添加到不同的项目中。 egif status等于open然后设置open.png img status等于wip然后wip.png状态等于closed then closed.png并且在设置图像后我想根据票号no打开活动并显示所有细节项目点击的票证。那么我应该写什么来设置图像和代码来写项目方法。我是新的android,这里是我的代码

class viewticket extends AsyncTask<String, Void, String> {

         @Override
         protected void onPreExecute() {
            super.onPreExecute();
            pdialog = new ProgressDialog(UserLogedIn.this);
            pdialog.setMessage("Loading....");
            pdialog.setIndeterminate(false);
            pdialog.setCancelable(false);
            pdialog.show();
         }

         @Override
         protected String doInBackground(String... params)  {
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("userid", u_id));
                      // JSONObject jsonArray = jpar.makeHttpRequest(URLMyTicket, "POST", param);

             ServiceHandler sh = new ServiceHandler();


                      String jsonStr = sh.makeServiceCall(URLMyTicket, ServiceHandler.POST, param);
             Log.d("Response: ", "> " + jsonStr);
             if (jsonStr != null)

             {
                 try {

                    contacts = new JSONArray(jsonStr);
                     a=contacts.length();
                    Log.v(TAG, ".................." + a);

                    if(a > 0 ) {
                        id = new String[contacts.length()];
                        stats = new String[contacts.length()];
                        due_date = new String[contacts.length()];
                        prob = new String[contacts.length()];

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

                            String aa = c.getString(TAG_ID);
                            String bb = c.getString(TAG_PROB);
                            String cc = c.getString(TAG_status);
                            String dd = c.getString(TAG_due_date);

                            Log.v(TAG, "TAG_ID" + aa);
                            Log.v(TAG, "TAGPROB" + bb);
                            Log.v(TAG, "TAGPROB" + cc);
                            Log.v(TAG, "TAGPROB" + dd);

                            id[i] = aa;
                            prob[i] = bb;
                            stats[i] = cc;
                            due_date[i] = dd;

                            Log.v(TAG, "aaaaa" + id[i]);
                            Log.v(TAG, "bbbbb" + prob[i]);
                            Log.v(TAG, "cccc" + stats[i]);
                            Log.v(TAG, "dddd" + due_date[i]);


                        }

                    }
                } catch (JSONException e) {
                  System.out.print("hiiiiiiiiiiii" );
                    e.printStackTrace();
                }
            }

            return null;
        }
        @Override
        protected void onPostExecute(String  result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog

            Log.v(TAG, "length of aaaaa" + a);
            if(id!=null && id.length > 0 )
            {
                        Ticket_adapter adapter=new Ticket_adapter(UserLogedIn.this,id,prob,stats,due_date);
                lv.setAdapter(adapter);
            }
            else
            {
                      Toast.makeText(UserLogedIn.this, "No Ticket Present", Toast.LENGTH_SHORT).show();
            }
            pdialog.hide();
            pdialog.dismiss();



             }

            }

所以你能为我编写代码来根据状态设置图像以及如何编写onitemclicklistener方法。

适配器类

public class Ticket_adapter extends ArrayAdapter<String> {
Context context;
String[] id;
String[] prob;
String[] stats;
String[] due_date;
int [] stimg;

LayoutInflater inflater;

public Ticket_adapter(UserLogedIn context, String[] id,String[] prob,String[] stats,String[] due_date,int[] stimg) {
    super(context, R.id.list_item,id);
    this.context=context;
    this.id=id;
    this.prob=prob;
   this.stats=stats;
    this.due_date=due_date;
    this.stimg=stimg;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null)
    {
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.list_item,null);

    }
    TextView idtxt=(TextView)convertView.findViewById(R.id.uid);
    TextView probtxt=(TextView)convertView.findViewById(R.id.prob);
    TextView stustxt=(TextView)convertView.findViewById(R.id.status);
    TextView duetxt=(TextView)convertView.findViewById(R.id.duedate);
    ImageView immm=(ImageView)convertView.findViewById(R.id.image12);
    idtxt.setText(id[position]);
    probtxt.setText(prob[position]);
    stustxt.setText(stats[position]);
    duetxt.setText(due_date[position]);
    immm.setImageResource(stimg[position]);


    return convertView;

}

}

2 个答案:

答案 0 :(得分:1)

select substr(substr('F_ALIGN_PROF_PRD_MTH_SLS_NM2', 1, 24),
              1,
              instr(substr('F_ALIGN_PROF_PRD_MTH_SLS_NM2', 1, 24), '_', -1)) substr_table_name
from   dual;

SUBSTR_TABLE_NAME    
---------------------
F_ALIGN_PROF_PRD_MTH_

答案 1 :(得分:0)

您的getView()方法

中的

  if(stats[position].Equals("open"))
    {
     immm.setImageResource(R.drawable.open);
    }
   else if(stats[position].Equals("wip"))
   {
    immm.setImageResource(R.drawable.wip);
    }
   else
    {
       immm.setImageResource(R.drawable.closed);
    }