在实现baseAdapter子类的时候,重载了getview方法来实现自己的Adapter对象,但是在debug的过程中发现,这个getview会不停的被调用,导致了listview中显示不出我们需要的东西。
关键问题,在于我们没有给设定listview的长度是多少,所以无法来确定到底需要绘制多少我们自定的view在listview中,所以我们需要在布局文件中对于listview的高度进行设定,可以选择fill_parent或者设定数值高度。
-
<LinearLayout xmlns:android=""
-
xmlns:tools="" android:layout_width="match_parent"
-
android:layout_height="match_parent" 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=".MainActivity">
-
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/appBar"
-
android:layout_weight="1"/>
-
<!--<LinearLayout-->
-
<!--android:layout_width="fill_parent"-->
-
<!--android:layout_height="wrap_content"-->
-
<!-->-->
-
<ListView
-
android:layout_width="fill_parent"
-
android:layout_height="50dip"
-
android:id="@+id/listview" />
-
<!--</LinearLayout>-->
-
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/rank"
-
android:layout_weight="1"/>
-
</LinearLayout>
这样再设置自己的Adapter时候就可以正确显示了
上代码:
-
package com.example.dolby.webviewtest;
-
-
import android.content.Context;
-
import android.support.v7.app.ActionBarActivity;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.ArrayAdapter;
-
import android.widget.BaseAdapter;
-
import android.widget.ListView;
-
import android.widget.TextView;
-
-
import com.android.volley.Request;
-
import com.android.volley.RequestQueue;
-
import com.android.volley.Response;
-
import com.android.volley.VolleyError;
-
import com.android.volley.toolbox.JsonArrayRequest;
-
import com.android.volley.toolbox.JsonObjectRequest;
-
import com.android.volley.toolbox.Volley;
-
-
import org.json.JSONArray;
-
import org.json.JSONObject;
-
import org.w3c.dom.Text;
-
-
import java.util.ArrayList;
-
import java.util.Date;
-
-
-
//class jobinfo
-
//{
-
// public String url;
-
// public String name;
-
//}
-
-
public class MainActivity extends ActionBarActivity {
-
-
private TextView text;
-
private ListView infolist;
-
private ArrayList<JSONObject> jsData;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
text = (TextView)findViewById(R.id.appBar);
-
jsData = new ArrayList<JSONObject>();
-
infolist = (ListView)findViewById(R.id.listview);
-
startLoadInfo();
-
-
-
}
-
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.menu_main, menu);
-
return true;
-
}
-
-
@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.
-
int id = item.getItemId();
-
-
//noinspection SimplifiableIfStatement
-
if (id == R.id.action_settings) {
-
return true;
-
}
-
-
return super.onOptionsItemSelected(item);
-
}
-
-
private void startLoadInfo()
-
{
-
RequestQueue queue = Volley.newRequestQueue(this);
-
String jsonurl = "http://jobdev.sinaapp.com/api/getinfoList";
-
JsonArrayRequest jsRequest = new JsonArrayRequest(jsonurl,new Response.Listener<JSONArray>() {
-
@Override
-
public void onResponse(JSONArray response) {
-
//Log.d("first is " , "" + response.getJSONObject(0));
-
try{
-
//text.setText(response.getJSONObject(0).getString("name"));
-
for(int i = 0; i < response.length() ; i++)
-
{
-
jsData.add(response.getJSONObject(i));
-
}
-
jsonAdapter adapter = new jsonAdapter(MainActivity.this);
-
infolist.setAdapter(adapter);
-
-
}
-
catch(Exception e)
-
{
-
Log.d("json get exception",e.getMessage());
-
}
-
-
}
-
-
},new Response.ErrorListener() {
-
@Override
-
public void onErrorResponse(VolleyError error) {
-
Log.d("json Request Error",error.getMessage());
-
}
-
});
-
queue.add(jsRequest);
-
}
-
-
class jsonAdapter extends BaseAdapter
-
{
-
Context m_context;
-
public jsonAdapter(Context context)
-
{
-
super();
-
m_context = context;
-
}
-
@Override
-
public int getCount(){
-
return jsData.size();
-
}
-
-
@Override
-
public Object getItem(int position) {
-
return null;
-
}
-
-
@Override
-
public long getItemId(int position) {
-
return 0;
-
}
-
-
@Override
-
public View getView(int position, View convertView, ViewGroup parent) {
-
TextView view = new TextView(m_context);
-
String name = "";
-
try{
-
name = jsData.get(position).getString("name");
-
}catch(Exception e){
-
Log.d("haha error exception","nick");
-
}
-
view.setText(name);
-
return view;
-
}
-
}
-
}
阅读(10939) | 评论(0) | 转发(0) |