2016-06-20 140 views
-3

我試圖讓下面的觀點是在一個列表無法使用自定義適配器

enter image description here

以下get膨脹視圖爲列表視圖是我用

package com.solodroid.ecommerce; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.graphics.drawable.ColorDrawable; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.text.Html; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

import com.rakyow.srptjobportal.R; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

/** 
* Created by user on 3/29/2016. 
*/ 
public class ActivityJobByCategory extends Activity { 
ArrayList<JobsByCategory> listOfJobs = new ArrayList<JobsByCategory>(); 
ListView listView; 
Long categoryId; 
ProgressBar prgLoading; 
TextView txtAlert; 
String jobName, jobLocation, fromDate, toDate, fromPackage, toPackage, jobType, jobguid; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.job_list); 
    ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header))); 
    bar.setTitle("Jobs Found"); 
    bar.setDisplayHomeAsUpEnabled(true); 
    bar.setHomeButtonEnabled(true); 
    //  prgLoading = (ProgressBar) findViewById(R.id.prgLoading); 
    // txtAlert = (TextView) findViewById(R.id.txtAlert); 

    listView = (ListView) findViewById(R.id.listMenu); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     categoryId = extras.getLong("category_id"); 
    } 

    new task().execute(); 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      // app icon in action bar clicked; go home 
      this.finish(); 
      overridePendingTransition(R.anim.open_main, R.anim.close_next); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

class task extends AsyncTask<String, String, Void> { 
    private ProgressDialog progressDialog = new ProgressDialog(ActivityJobByCategory.this); 
    InputStream is = null; 
    String result = ""; 

    protected void onPreExecute() { 
     progressDialog.setMessage("Loading..."); 
     progressDialog.setIndeterminate(false); 
     progressDialog.setCancelable(false); 
     progressDialog.show(); 
     progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
      public void onCancel(DialogInterface arg0) { 
       task.this.cancel(true); 
      } 
     }); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 

     String url_select = "http://jobportal.rakyow.com/website/joblist/lists?catid=" + categoryId; 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url_select); 
     ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 

     try { 
      httpPost.setEntity(new UrlEncodedFormEntity(param)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      Log.d("Amit", "executed httppost"); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      Log.d("Amit", "Got response"); 
      // read content 
      is = httpEntity.getContent(); 
      Log.d("Amit", "Got content"); 
     } catch (Exception e) { 

      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 
     try { 
      BufferedReader br = new BufferedReader(
        new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 
      String line = ""; 
      while ((line = br.readLine()) != null) { 
       Log.d("Amit", "line = " + line); 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
      Log.d("Amit", "Result = " + result); 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     return null; 
    } 

    protected void onPostExecute(Void v) { 

     try { 
      JSONObject object = new JSONObject(result); 
      Log.d("Amit", "created jsonobject"); 
      JSONArray userDetailsArray = object.getJSONArray("data"); 
      Log.d("Amit", "Got userDetails"); 
      ArrayList<String> items = new ArrayList<String>(); 
      try { 
       for (int i = 0; i < userDetailsArray.length(); i++) { 
        JSONObject obj = userDetailsArray.getJSONObject(i); 
        Log.d("Amit", "inside " + i); 
        jobName = obj.getString("job_title"); 
        jobLocation = obj.getString("location"); 
        fromDate = obj.getString("from_date"); 
        toDate = obj.getString("to_date"); 
        fromPackage = obj.getString("from_package"); 
        toPackage = obj.getString("package"); 
        jobType = obj.getString("job_type"); 
        jobguid = obj.getString("guid"); 

        JobsByCategory job = new JobsByCategory(jobName, jobLocation, fromDate, toDate, fromPackage, toPackage, jobType, jobguid); 

        listOfJobs.add(job); 

       } 

       this.progressDialog.dismiss(); 
       // get an output on the screen 

       //set adapter for listview 


       JobsAdapter adapter = new JobsAdapter(ActivityJobByCategory.this, listOfJobs); 
       listView.setAdapter(adapter); 
       Log.d("Amit", "adapter set"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
    } 


} 

public class JobsAdapter extends ArrayAdapter<JobsByCategory> { 
    // View lookup cache 

    private class ViewHolder { 
     TextView job_name, job_location, date, packages, job_type; 

    }/*@Override 
    public int getCount() { 
     return listOfOrders.size(); 
    }*/ 

    public JobsAdapter(Context context, ArrayList<JobsByCategory> orders) { 

     super(context, R.layout.menu_list_item, orders); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     final JobsByCategory jobs = getItem(position); 
     Log.d("Amit2", "reached adapter getView"); 
     // Log.d("Akhilesh", loads.loadType); 
     // Check if an existing view is being reused, otherwise inflate the view 
     ViewHolder viewHolder; // view lookup cache stored in tag 
     if (convertView == null) { 
      viewHolder = new ViewHolder(); 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.menu_list_item, parent, false); 
      viewHolder.job_name = (TextView) convertView.findViewById(R.id.jobName); 
      viewHolder.job_location = (TextView) convertView.findViewById(R.id.jobLocation); 
      viewHolder.date = (TextView) convertView.findViewById(R.id.dates); 
      viewHolder.packages = (TextView) convertView.findViewById(R.id.salary); 
      viewHolder.job_type = (TextView) convertView.findViewById(R.id.jobType); 
      convertView.setTag(viewHolder); 
      Log.d("Amit", "set adapter view"); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
      Log.d("Amit", " not set adapter view"); 
     } 
     // Populate the data into the template view using the data object 
     viewHolder.job_name.setText(jobs.jobName); 
     viewHolder.job_location.setText(jobs.location); 
     viewHolder.date.setText(jobs.fromDate + "-" + jobs.toDate); 
     viewHolder.packages.setText(jobs.fromPackage + "-" + jobs.toPackage); 
     viewHolder.job_type.setText(jobs.jobType); 
     Log.d("Amit", "set all values"); 
     return convertView; 
    } 
} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    finish(); 
    overridePendingTransition(R.anim.open_main, R.anim.close_next); 
} 

} 

JobsByCategory代碼.class is

package com.solodroid.ecommerce; 

public class JobsByCategory { 
String jobName; 
String location; 
String fromDate; 
String toDate; 
String fromPackage; 
String toPackage; 
String jobType; 
String jobGuid; 

// Constructor to convert JSON object into a Java class instance 
public JobsByCategory(String jN, String lO, String fD, String tD, String fP , String tP , String jT , String gD) { 

    this.jobName = jN; 
    this.location =lO; 
    this.fromDate = fD; 
    this.toDate = tD; 
    this.fromPackage =fP; 
    this.toPackage =tP; 
    this.jobType =jT; 
    this.jobGuid =gD; 


} 

public String getJobName() { 
    return jobName; 
} 
public String getLocation() { 
    return location; 
} 
public String getFromDate() { 
    return fromDate; 
} 
public String getToDate(){ 
    return toDate; 
} 
public String getFromPackage() { 
    return fromPackage; 
} 
public String getToPackage() { 
    return toPackage; 
} 
public String getJobType() { 
    return jobType; 
} public String getJobGuid() { 
    return jobGuid; 
} 

}

我不能夠得到的觀點。但沒有錯誤come.Can任何人可以幫助我解決這個問題?

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#e7e7e7" > 

    <LinearLayout 
     android:id="@+id/lytSearch" 
     android:layout_width="fill_parent" 
     android:layout_height="45dp" 
     android:background="@color/header" 
     android:gravity="center_vertical" 
     android:visibility="visible" > 

     <EditText 
      android:id="@+id/edtKeyword" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="10dp" 
      android:layout_marginLeft="5dp" 
      android:layout_weight="1" 
      android:background="@drawable/search_form" 
      android:hint="@string/search" 
      android:padding="7dp" 
      android:textColor="@color/hint" 
      android:textSize="14sp" 
      android:inputType="text" 
      android:singleLine="true" /> 

     <ImageButton 
      android:id="@+id/btnSearch" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:background="@color/header" 
      android:src="@drawable/ic_search" /> 

    </LinearLayout> 

    <ListView 
     android:visibility="gone" 
     android:id="@+id/listMenu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/lytSearch" 
     android:divider="@null" 
     android:padding="5dp" /> 

    <ProgressBar 
     android:id="@+id/prgLoading" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_centerInParent="true" /> 
    <TextView 
     android:id="@+id/txtAlert" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/not_found" 
     android:textSize="14sp" 
     android:layout_centerInParent="true" 
     android:visibility="gone"/> 

    </RelativeLayout> 

自定義視圖

<LinearLayout 
android:layout_width="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="match_parent" 
android:background="@drawable/button_top_style_1" 
android:orientation="vertical" 
android:padding="5dp" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Accountant" 
    android:padding="10dp" 
    android:id="@+id/jobName" 
    android:textSize="18sp" 
    android:textStyle="bold|italic" 
    android:background="#7153ff" 
    android:clickable="true" 
    android:textColor="#ffffff" /> 

<RelativeLayout 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:background="#69c9ff"> 

    <TextView 
     android:id="@+id/jobLocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/dates" 
     android:drawableLeft="@drawable/placeholder" 
     android:layout_alignParentLeft="true" 
     android:padding="10sp" 
     android:text="aaa" 
     android:textSize="14sp" 
     android:layout_alignParentStart="false" 
     android:layout_alignParentEnd="false" /> 

    <TextView 
     android:id="@+id/dates" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:drawableLeft="@drawable/calendar" 
     android:layout_centerVertical="true" 
     android:text="bbb" /> 

    </RelativeLayout> 
    <RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:background="#69c9ff"> 

    <TextView 
     android:id="@+id/salary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/jobType" 
     android:drawableLeft="@drawable/rupee" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:padding="10sp" 
     android:text="4 lakhs" 
     android:textSize="14sp" /> 

    <TextView 
     android:id="@+id/jobType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="Full Time" 
     android:autoText="true" 
     android:background="#ff8058" 
     android:padding="10dp" 
     android:textStyle="bold" 
     android:textColor="#ffffff" /> 

</RelativeLayout> 
</LinearLayout> 
+0

在JobsAdapter下的getView你需要返回視圖。 – Sach

+0

我已添加..但仍然沒有運氣 –

+0

請發佈錯誤你有什麼? – Sach

回答

3

添加下面一行在getView()方法的底部,並檢查是否你得到你的看法。

return convertView; 

下面的類將替代你的類其現在的工作:

(注:我從所有佈局刪除圖像,使這個運行像我不是你的任何圖片)

1 )ActivityJobByCategory.java

package com.example.c109.temp; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

public class ActivityJobByCategory extends AppCompatActivity { 
    ArrayList<JobsByCategory> listOfJobs = new ArrayList<JobsByCategory>(); 
    ListView listView; 
    Long categoryId; 
    ProgressBar prgLoading; 
    TextView txtAlert; 
    String jobName, jobLocation, fromDate, toDate, fromPackage, toPackage, jobType, jobguid; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.job_list); 
     android.support.v7.app.ActionBar bar = getSupportActionBar(); 
     // bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header))); 
     bar.setTitle("Jobs Found"); 
     bar.setDisplayHomeAsUpEnabled(true); 
     bar.setHomeButtonEnabled(true); 
     //  prgLoading = (ProgressBar) findViewById(R.id.prgLoading); 
     // txtAlert = (TextView) findViewById(R.id.txtAlert); 

     listView = (ListView) findViewById(R.id.listMenu); 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      categoryId = extras.getLong("category_id"); 
     } 

     new task().execute(); 


    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       // app icon in action bar clicked; go home 
       this.finish(); 
       // overridePendingTransition(R.anim.class, R.anim.close_next); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 


    @Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 
     super.onBackPressed(); 
     finish(); 
     //overridePendingTransition(R.anim.open_main, R.anim.close_next); 
    } 


    public class JobsAdapter extends BaseAdapter { 
     // View lookup cache 
     ArrayList<JobsByCategory> orders = new ArrayList<>(); 
     Context mContext; 

     private class ViewHolder { 
      TextView job_name, job_location, date, packages, job_type; 

     } 


     /*@Override 
     public int getCount() { 
      return orders.size(); 
     }*/ 

     public JobsAdapter(Context context, ArrayList<JobsByCategory> orders) { 

      mContext = context; 
      this.orders = orders; 

     } 

     @Override 
     public int getCount() { 
      return orders.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return orders.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // Get the data item for this position 
      final JobsByCategory jobs = (JobsByCategory) getItem(position); 
      Log.d("Amit2", "reached adapter getView"); 
      // Log.d("Akhilesh", loads.loadType); 
      // Check if an existing view is being reused, otherwise inflate the view 
      ViewHolder viewHolder; // view lookup cache stored in tag 
      if (convertView == null) { 
       viewHolder = new ViewHolder(); 
       LayoutInflater inflater = LayoutInflater.from(mContext); 
       convertView = inflater.inflate(R.layout.menu_list_item, parent, false); 
       viewHolder.job_name = (TextView) convertView.findViewById(R.id.jobName); 
       viewHolder.job_location = (TextView) convertView.findViewById(R.id.jobLocation); 
       viewHolder.date = (TextView) convertView.findViewById(R.id.dates); 
       viewHolder.packages = (TextView) convertView.findViewById(R.id.salary); 
       viewHolder.job_type = (TextView) convertView.findViewById(R.id.jobType); 
       convertView.setTag(viewHolder); 
       Log.d("Amit", "set adapter view"); 
      } else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
       Log.d("Amit", " not set adapter view"); 
      } 
      // Populate the data into the template view using the data object 
      viewHolder.job_name.setText(jobs.jobName); 
      viewHolder.job_location.setText(jobs.location); 
      viewHolder.date.setText(jobs.fromDate + "-" + jobs.toDate); 
      viewHolder.packages.setText(jobs.fromPackage + "-" + jobs.toPackage); 
      viewHolder.job_type.setText(jobs.jobType); 
      Log.d("Amit", "set all values"); 
      return convertView; 
     } 
    } 

    class task extends AsyncTask<String, String, Void> { 
     private ProgressDialog progressDialog = new ProgressDialog(ActivityJobByCategory.this); 
     InputStream is = null; 
     String result = ""; 

     protected void onPreExecute() { 
      progressDialog.setMessage("Loading..."); 
      progressDialog.setIndeterminate(false); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 
      /* progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
       public void onCancel(DialogInterface arg0) { 
        task.this.cancel(true); 
       } 
      });*/ 
     } 

     @Override 
     protected Void doInBackground(String... params) { 

      String url_select = "http://jobportal.rakyow.com/website/joblist/lists?catid=" + categoryId; 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url_select); 
      ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 

      try { 
       httpPost.setEntity(new UrlEncodedFormEntity(param)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       Log.d("Amit", "executed httppost"); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       Log.d("Amit", "Got response"); 
       // read content 
       is = httpEntity.getContent(); 
       Log.d("Amit", "Got content"); 
      } catch (Exception e) { 

       Log.e("log_tag", "Error in http connection " + e.toString()); 
      } 
      try { 
       BufferedReader br = new BufferedReader(
         new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = ""; 
       while ((line = br.readLine()) != null) { 
        Log.d("Amit", "line = " + line); 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 
       Log.d("Amit", "Result = " + result); 

      } catch (Exception e) { 
       // TODO: handle exception 
       Log.e("log_tag", "Error converting result " + e.toString()); 
      } 

      return null; 
     } 

     protected void onPostExecute(Void v) { 

      try { 
       this.progressDialog.dismiss(); 
       JSONObject object = new JSONObject(result); 
       Log.d("Amit", "created jsonobject"); 
       JSONArray userDetailsArray = object.getJSONArray("data"); 
       Log.d("Amit", "Got userDetails"); 
       ArrayList<String> items = new ArrayList<String>(); 
       try { 
        for (int i = 0; i < userDetailsArray.length(); i++) { 
         JSONObject obj = userDetailsArray.getJSONObject(i); 
         Log.d("Amit", "inside " + i); 
         jobName = obj.getString("job_title"); 
         jobLocation = obj.getString("location"); 
         fromDate = obj.getString("from_date"); 
         toDate = obj.getString("to_date"); 
         fromPackage = obj.getString("from_package"); 
         toPackage = obj.getString("package"); 
         jobType = obj.getString("job_type"); 
         jobguid = obj.getString("guid"); 

         JobsByCategory job = new JobsByCategory(jobName, jobLocation, fromDate, toDate, fromPackage, toPackage, jobType, jobguid); 

         listOfJobs.add(job); 

        } 


        // get an output on the screen 

        //set adapter for listview 


        JobsAdapter adapter = new JobsAdapter(ActivityJobByCategory.this, listOfJobs); 
        listView.setAdapter(adapter); 
        Log.d("Amit", "adapter set"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } catch (Exception e) { 
       // TODO: handle exception 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 
     } 


    } 

} 

2)job_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#e7e7e7" > 

<LinearLayout 
    android:id="@+id/lytSearch" 
    android:layout_width="fill_parent" 
    android:layout_height="45dp" 

    android:gravity="center_vertical" 
    android:visibility="visible" > 

    <EditText 
     android:id="@+id/edtKeyword" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="5dp" 
     android:layout_weight="1" 

     android:padding="7dp" 

     android:textSize="14sp" 
     android:inputType="text" 
     android:singleLine="true" /> 

    <ImageButton 
     android:id="@+id/btnSearch" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     /> 

</LinearLayout> 

<ListView 
    android:visibility="visible" 
    android:id="@+id/listMenu" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/lytSearch" 
    android:divider="@null" 
    android:padding="5dp" /> 

<ProgressBar 
    android:id="@+id/prgLoading" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_centerInParent="true" /> 
<TextView 
    android:id="@+id/txtAlert" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="14sp" 
    android:layout_centerInParent="true" 
    android:visibility="gone"/> 

3)menu_list_item.xml

<LinearLayout 
android:layout_width="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="5dp" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Accountant" 
    android:padding="10dp" 
    android:id="@+id/jobName" 
    android:textSize="18sp" 
    android:textStyle="bold|italic" 
    android:background="#7153ff" 
    android:clickable="true" 
    android:textColor="#ffffff" /> 

<RelativeLayout 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:background="#69c9ff"> 

    <TextView 
     android:id="@+id/jobLocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/dates" 
     android:layout_alignParentLeft="true" 
     android:padding="10sp" 
     android:text="aaa" 
     android:textSize="14sp" 
     android:layout_alignParentStart="false" 
     android:layout_alignParentEnd="false" 
     android:layout_alignParentRight="false"/> 

    <TextView 
     android:id="@+id/dates" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="bbb" /> 

</RelativeLayout> 
<RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:background="#69c9ff"> 

    <TextView 
     android:id="@+id/salary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/jobType" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:padding="10sp" 
     android:text="4 lakhs" 
     android:textSize="14sp" /> 

    <TextView 
     android:id="@+id/jobType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="Full Time" 
     android:autoText="true" 
     android:background="#ff8058" 
     android:padding="10dp" 
     android:textStyle="bold" 
     android:textColor="#ffffff" /> 

</RelativeLayout> 

+0

我已添加..但仍然沒有運氣 –

+0

確定把你的XML文件,我會檢查工作室添加您的文件。 –

+0

我已經把xml文件也 –

1

爲什麼getCount被註釋掉適配器如何想知道項目

你打電話final JobsByCategory jobs = getItem(position)的數量;

,但沒有計數

+0

我已取消註釋..但現在我也無法獲得視圖 –

+0

你確定數組列表不是空的嗎? –

+0

不是它的空......因爲我打印的是非空的json對象 –

1

你的看法是複雜的使用ArrayAdapter,嘗試使用BaseAdapter來代替。像下面

1_變化extends ArrayAdapter<JobsByCategory>extends BaseAdapter

2_8更改代碼:

ArrayList<JobsByCategory> mOrderList; 
    Context mContext; 
    public JobsAdapter(Context context, ArrayList<JobsByCategory> orders) { 
      mContext = context; 
      mOrderList = orders; 
    } 
    @Override 
    public JobsByCategory getItem(int position) { 
     return mOrderList.get(position) 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    @Override 
    public int getCount() { 
     return mOrderList.size(); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    if (convertView == null) { 
      viewHolder = new ViewHolder(); 
      LayoutInflater inflater = LayoutInflater.from(mContext); 

    ... 
    } 

3_你的XML有問題:

<ListView 
     android:visibility="gone" 

變化gonevisible或刪除此行

並嘗試再次測試!

+0

final JobsByCategory jobs = getItem(position); 更改爲BaseAdapter後,這行不兼容類型的錯誤 –

+0

再次查看我的更改 – NamNH

+0

Ok lemme check ..我會在此之後通知您 –

相關問題