2017-10-17 132 views
0

我有一個列表視圖,我想顯示其中的一些值,連接沒有任何錯誤,但我的列表視圖不顯示這些值,我不知道我做了什麼錯了,我的代碼在下面請幫助我。我想我在customeadapter中做了一些錯誤..請檢查我的自定義適配器CustomAdapterViewDriver。列表視圖不顯示值

我的動態

public class PerformBargain extends ActionBarActivity { 
ListView UserDetails; 
JSONArray jsonArray; 
SendPostRequest mSendPostRequest; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_perform_bargain); 
    UserDetails=(ListView)findViewById(R.id.UserDetails); 
    mSendPostRequest= new SendPostRequest(); 
    mSendPostRequest.execute(); 



} 

class SendPostRequest extends AsyncTask<Void, Void, JSONArray> 
    { 

    String postData = ""; 

    HttpURLConnection httpURLConnection= null; 
    @Override 
    protected JSONArray doInBackground(Void... params) { 

     try { 
      Bundle bundle=getIntent().getExtras(); 
      String id=bundle.getString("ID"); 



      httpURLConnection= (HttpURLConnection) new URL("http://192.168.0.14:8080/GoodsServer/ViewRideDetails.jsp").openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 

      DataOutputStream outputStream= new 
      DataOutputStream(httpURLConnection.getOutputStream()); 
      outputStream.writeBytes("id=" +id); 
      outputStream.flush(); 
      outputStream.close(); 

      InputStream in = httpURLConnection.getInputStream(); 
      InputStreamReader inputStreamReader = new InputStreamReader(in); 

      int inputStreamData = inputStreamReader.read(); 
      while (inputStreamData != -1) { 
       char currentData = (char) inputStreamData; 
       inputStreamData = inputStreamReader.read(); 
       postData += currentData; 
      } 
      jsonArray=new JSONArray(postData.trim()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (httpURLConnection!= null) { 
       httpURLConnection.disconnect(); 
      } 
     } 

     return jsonArray; 




    } 

    @Override 
    protected void onPostExecute(JSONArray s) { 
     Toast.makeText(getApplicationContext(),postData,Toast.LENGTH_LONG).show(); 
     try{ 
       CustomAdapterViewDriver customAdapterViewDriver=new CustomAdapterViewDriver(PerformBargain.this,s); 
       UserDetails.setAdapter(customAdapterViewDriver); 

    }catch (Exception e) 
     { 

      System.out.print(e); 
     }} 


    } 

CustomAdapterViewDriver類

class CustomAdapterViewDriver extends BaseAdapter implements ListAdapter { 

String ids; 
TextView name,email,phone,Source,destination; 
private final Activity activity; 
private final JSONArray jsonArray; 

public CustomAdapterViewDriver(Activity activity, JSONArray jsonArray) { 
    assert activity != null; 
    assert jsonArray != null; 

    this.jsonArray = jsonArray; 
    this.activity = activity; 
} 


@Override public int getCount() { 

    return jsonArray.length(); 
} 

@Override public JSONObject getItem(int position) { 

    return jsonArray.optJSONObject(position); 
} 

@Override public long getItemId(int position) { 
    JSONObject jsonObject = getItem(position); 

    return jsonObject.optLong("id"); 
} 

@Override public View getView(int position, View view, ViewGroup parent) { 
    if (view == null) 
     view = activity.getLayoutInflater().inflate(R.layout.userviewforbargain, null); 
    JSONObject jsonObject = getItem(position); 
    name=(TextView)view.findViewById(R.id.namevu); 
    phone =(TextView)view.findViewById(R.id.phone); 
    email=(TextView)view.findViewById(R.id.email); 
    Source=(TextView)view.findViewById(R.id.sourceadderss); 
    destination=(TextView)view.findViewById(R.id.destinationaddress); 

    try{ 

     name.setText(jsonObject.getString("Name")); 
     phone.setText(jsonObject.getString("Phone")); 
     email.setText(jsonObject.getString("Email")); 
     Source.setText(jsonObject.getString("Source")); 
     destination.setText(jsonObject.getString("DestAdd")); 
    }catch(Exception e) 
    { 

     System.out.print(e); 

    } 
    return view; 


} 

}

userviewforbargain的Xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:orientation="horizontal" 
> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 

    android:text="Name" 
    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Phone" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Email" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="SourceAdderss" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Destination" 

    android:textColor="#000" 
    /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginLeft="50dp" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:id="@+id/namevu" 
     android:hint="Name" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Phone" 
     android:id="@+id/phone" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Email" 
     android:id="@+id/email" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="sourceadderss" 
     android:id="@+id/sourceadderss" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Destination" 
     android:id="@+id/destinationaddress" 
     android:textColor="#000" 
     /> 
    </LinearLayout> 

</LinearLayout> 

回答

0

的問題是與我的XML文件...我只需將相對佈局添加到我的XML中,並且現在就可以正常工作。

代碼

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.user.good_carriermacfast.PerformBargain" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:orientation="vertical" 
> 
<LinearLayout 
android:layout_width="110dp" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 

    android:text="Name" 
    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Phone" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Email" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="SourceAdderss" 

    android:textColor="#000" 
    /> 
<TextView 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:text="Destination" 

    android:textColor="#000" 
    /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginLeft="110dp" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:id="@+id/namevu" 
     android:hint="Name" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Phone" 
     android:id="@+id/phone" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Email" 
     android:id="@+id/email" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="sourceadderss" 
     android:id="@+id/sourceadderss" 
     android:textColor="#000" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:hint="Destination" 
     android:id="@+id/destinationaddress" 
     android:textColor="#000" 
     /> 
</LinearLayout> 

</RelativeLayout> 
+0

我猜,這是更多的是事實,你原來是有一個WRAP_CONTENT的LinearLayout,而下面的觀點是match_parent。 您不只是更改頂層佈局,而是將您的下一個LinearLayout更改爲layout_width =「110dp」 – Mars