2016-02-29 80 views
-1

我想在ListView的1行中顯示2條數據,但是我卡住了。JSON的自定義列表視圖

我的問題是在showList()方法。我無法使用自定義ListView XML顯示2個數據,我只能顯示1個數據。我想在我的自定義ListView中顯示itemitemsemail數組,而不僅僅是item

這裏是我的自定義的ListView適配器代碼...所有你需要的是有兩個的TextView,你將你的數據源綁定到一個充氣佈局的適配器類的

private void getDosen() { 
    //Ketika Aplikasi mengambil data kita akan melihat progress dialog 
    final ProgressDialog loading = ProgressDialog.show(this,"Mengambil Data","Mohon tunggu..",false,false); 
    //Logging Interceptor 
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
    //set Level Log 
    logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
    httpClient.addInterceptor(logging); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(ROOT_URL) 
      .addConverterFactory(GsonConverterFactory.create())//GsonConverter untuk parsing json 
      .client(httpClient.build()) 
      .build(); 

    RestAPI service = retrofit.create(RestAPI.class); 

    Call<Model> call = service.loadListDosen(); 
    call.enqueue(new Callback<Model>() { //Asyncronous Request 
     @Override 
     public void onResponse(Call<Model> call, Response<Model> response) { 
      loading.dismiss(); 
      List<Listdosen> dosen = response.body().getListdosen(); 

      //memasukkan data dari varibel dosen ke dosens 
      dosens = dosen; 
      //memanggil method untuk menampilkan list 
      showList(); 
     } 

     @Override 
     public void onFailure(Call<Model> call, Throwable t) { 

     } 
    }); 
} 

private void showList() { 
    //String array untuk menyimpan nama semua nama dosen 
    String[] items = new String[dosens.size()]; 
    String[] itemsemail = new String[dosens.size()]; 

    for (int i = 0; i < dosens.size(); i++) { 
     items[i] = dosens.get(i).getNama(); 
     itemsemail[i] = dosens.get(i).getEmail(); 

    } 
    //Membuat Array Adapter for listview 
    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_dosen_listview, items); 

    //setting adapter untuk listview 

    listview.setAdapter(adapter); 
} 
+0

查找關於如何使一個自定義的ArrayAdapter的教程。你無法用'ArrayAdapter ' –

回答

0

第一。

我已經創建了一個佈局文件和適配器,您可以插入到您現有的代碼中,我希望它適用於您。

這是佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/items" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="@string/app_name" 
    android:layout_margin="16dp"/> 

<TextView 
    android:id="@+id/itemsEmail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/items" 
    android:text="@string/app_name" 
    android:layout_alignBaseline="@+id/items"/> 

</RelativeLayout> 

爲適配器類,代碼低於

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.List; 

public class MyBaseAdapter extends BaseAdapter { 

private LayoutInflater layoutinflater; 
private List<Listdosen> listStorage; 
private Context context; 

public MyBaseAdapter(Context context, List<Listdosen> customizedListView) { 
    this.context = context; 
    layoutinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    listStorage = customizedListView; 
} 

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

@Override 
public Listdosen getItem(int position) { 
    return listStorage.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder listViewHolder; 
    if(convertView == null){ 
     listViewHolder = new ViewHolder(); 
     convertView = layoutinflater.inflate(R.layout.two_items, parent, false); 
     listViewHolder.items = (TextView)convertView.findViewById(R.id.items); 
     listViewHolder.itemEmail = (TextView)convertView.findViewById(R.id.itemsEmail); 
     convertView.setTag(listViewHolder); 
    }else{ 
     listViewHolder = (ViewHolder)convertView.getTag(); 
    } 
    listViewHolder.items.setText(listStorage.get(position).getName()); 
    listViewHolder.itemEmail.setText(listStorage.get(position).getEmail()); 

    return convertView; 
} 

static class ViewHolder{ 
    TextView items; 
    TextView itemEmail; 
} 
} 

調整你的代碼一點正好與變化。

showList() to showList(List<Listdosen> dosen) 

而不是

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_dosen_listview, items); 

使用本

ArrayAdapter adapter = new ArrayAdapter<Listdosen>(this, dosen);