我是android和java的新手,最近在使用baseadapter将获取的json数据显示到listview中时遇到问题。起初我使用了这段代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params) {
String str = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray new_array = new JSONArray(response);
for(int i = 0, count = new_array.length(); i< count; i++)
{
try {
JSONObject jsonObject = new_array.getJSONObject(i);
stringArray.add(jsonObject.getString("title").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,R.layout.test_tuh,stringArray);
ListView list= (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//tv.setText("error2");
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
此代码成功显示获取的json数据。但它仅显示一行。我在列表视图中需要多于一(两)行。因此,我尝试了此代码,但它不起作用,它显示了一个空白屏幕。
我的代码如下
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params) {
String str = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
String response = result.toString();
try {
arrayList=new ArrayList<get_set>();
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray new_array = new JSONArray(response);
for(int i = 0, count = new_array.length(); i< count; i++)
{
try {
JSONObject jsonObject = new_array.getJSONObject(i);
stringArray.add(jsonObject.getString("title").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
ListView listView;
adap= new BaseAdapter() {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public View getView(int position, View view, ViewGroup viewgroup) {
if (view==null) {
view=inflater.inflate(R.layout.bsdi, null);
}
TextView title_tuh=(TextView) view.findViewById(R.id.title1);
TextView notice_tuh=(TextView) view.findViewById(R.id.notice1);
title_tuh.setText(arrayList.get(position).getTitle());
notice_tuh=.setText(arrayList.get(position).getNotice());
return view;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
};
listView=(ListView) findViewById(R.id.listView1);
listView.setAdapter(adap);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//tv.setText("error2");
}
}
据我所知,问题就在这里
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray new_array = new JSONArray(response);
for(int i = 0, count = new_array.length(); i< count; i++)
{
try {
JSONObject jsonObject = new_array.getJSONObject(i);
stringArray.add(jsonObject.getString("title").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
到目前为止,我猜测提取的json数据存储在stringArray中,但以后不使用。如果我尝试像这样使用它,则会出现错误
title_tuh.setText(stringArray .get(position).getTitle());
notice_tuh=.setText(stringArray .get(position).getNotice());
如果我尝试不使用ArrayList stringArray = new ArrayList();
并使用
arrayList = new ArrayList(); 相反,这样我也会得到错误。
arrayList=new ArrayList<get_set>();
JSONArray new_array = new JSONArray(response);
//JSONArray jsonArray = new JSONArray();
for(int i = 0, count = new_array.length(); i< count; i++)
{
try {
JSONObject jsonObject = new_array.getJSONObject(i);
arrayList.add(jsonObject.getString("title").toString());
// String in = mInflater.inflate(R.layout.custom_row_view, null);
}
catch (JSONException e) {
e.printStackTrace();
}
}
我不知道如何解决这个问题。我看过很多在线教程,但是这些教程对我没有帮助。请帮我。
首先,您不需要创建如下row_listitem.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/list_selector"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/iv_icon_social"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:background="@drawable/image_border"
android:src="@drawable/sms_t"
android:visibility="gone" />
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="85dp"
android:layout_marginRight="50dp"
android:layout_marginTop="0dp"
android:layout_toRightOf="@+id/iv_icon_social"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="5dip"
android:visibility="visible" >
<TextView
android:id="@+id/txt_ttlsm_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Sample text"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_ttlcontact_row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginTop="3dp"
android:paddingLeft="10dp"
android:maxEms="20"
android:maxLines="2"
android:singleLine="false"
android:ellipsize="end"
android:text="Sample text2"
android:textColor="#808080"
android:textSize="15dp"
android:textStyle="normal"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
现在,您需要创建 Custom BaseAdapter, 如下所示:
public class BaseAdapter2 extends BaseAdapter {
private Activity activity;
// private ArrayList<HashMap<String, String>> data;
private static ArrayList title,notice;
private static LayoutInflater inflater = null;
public BaseAdapter2(Activity a, ArrayList b, ArrayList bod) {
activity = a;
this.title = b;
this.notice=bod;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return title.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.row_listitem, null);
TextView title2 = (TextView) vi.findViewById(R.id.txt_ttlsm_row); // title
String song = title.get(position).toString();
title2.setText(song);
TextView title22 = (TextView) vi.findViewById(R.id.txt_ttlcontact_row2); // notice
String song2 = notice.get(position).toString();
title22.setText(song2);
return vi;
}
}
现在,您可以设置您的 主要活动, 例如:
public class MainActivity extends Activity {
ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();
ListView list;
BaseAdapter2 adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://10.0.2.2/BSDI/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
JSONArray new_array = new JSONArray(response);
for (int i = 0, count = new_array.length(); i < count; i++) {
try {
JSONObject jsonObject = new_array.getJSONObject(i);
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("notice").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter = new BaseAdapter2(MainActivity.this, title_array, notice_array);
list.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// tv.setText("error2");
}
}
}
}
我有以下JSON数据: 我想在标记中显示它。我试着这样做:
我有一个列表视图,显示了我从Github API得到的一些信息,我为每个项目设置了ItemClickListener,这样我就可以在警报对话框中显示一些关于它们的信息。 在你用重复标记这个之前,我想说这些解决方案都不适合我如何获得在Android系统中点击的列表视图项目的值? 我试过这个: 但我得到了类Cast异常 java.lang.ClassCast异常:us.egek.repofinderf
我正在尝试从我的FoodAdapter中传递数据,该数据从BaseAdapter扩展到从AppCompatDialog片段扩展的FoodDialog。当用户单击list_item中的按钮时,我需要将数据传递给FoodDialog。 确认FoodDialog。班
我想在计数器的基础上排序listview,但我无法做到这一点,请任何人帮助我。 在这里,我正在分享我的代码和屏幕截图。 这是链接到列表视图的适配器 自定义适配器组.java 下面是使用ListView的代码 Fragment_groups.java 这是屏幕截图
我正在使用Spring Elasticsearch chRepository来查询Elasticsearch: 我可以成功运行搜索查询并检索映射到MyDocument bean中的elasticsearch结果列表 虽然发送到elasticsearch和响应的查询是正确的(我可以在调试日志中看到突出显示块)并且结果也合适,但我不知道如何在我的java代码中获取突出显示信息。 我不想使用Elasti
问题内容: 我有一个文件,其中包含要在R中使用的1500个json对象。我已经能够将数据作为列表导入,但是在将其强制转换为有用的结构时遇到了麻烦。我想创建一个数据框,其中每个json对象包含一行,每个key:value对包含一列。 我用这个小的假数据集重新创建了我的处境: 数据的一些功能: 所有对象都包含相同数量的key:value对,尽管某些值是null 每个对象有两个非数字列(名称和组) 名称