我有一个列表视图,我想在其中显示一些值,连接没有任何错误,但我的列表视图没有显示那些值,我不知道我做错了什么,我的代码是下面请帮帮我。我认为我在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
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 :(得分: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>