我知道有很多关于如何制作Android ListView的帖子,但即使仔细查看了大部分内容,我也无法弄清楚我的问题是什么。我是Android新手,显然有点不知所措。活动运行但未显示任何内容。
这是我的活动:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class HistoryActivity extends Activity {
CustomRowAdapter customRowAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = (ListView)findViewById(R.id.listView);
customRowAdapter = new CustomRowAdapter(getApplicationContext());
listView.setAdapter(customRowAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.history, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是activity_history.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ListView(custom_row.xml)中输入的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/textView2"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/textView3"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
和CustomRowAdapter.java:
package com.example.myfirstapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomRowAdapter extends ArrayAdapter<String> {
private String[] text1 = {
"Google Plus",
"Twitter",
"Facebook",
"Instagram"};
private String[] text2 = {
"test1",
"test2",
"test3",
"test4"};
private String[] text3 = {
"Google Plus",
"Twitter",
"Facebook",
"Instagram"};
private LayoutInflater layoutInflater;
private Context context;
public CustomRowAdapter(Context context) {
super(context,0);
layoutInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context=context;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup){
view = layoutInflater.inflate(R.layout.custom_row,null);
TextView textViewT1 = (TextView)view.findViewById(R.id.textView1);
TextView textViewT2 = (TextView)view.findViewById(R.id.textView2);
TextView textViewT3 = (TextView)view.findViewById(R.id.textView3);
// textViewT1.setText("test");
// textViewT2.setText(text2[0]);
// textViewT3.setText(text3[0]);
return view;
}
@Override
public int getCount(){
return 0;
}
}
希望有人可以帮助我。
答案 0 :(得分:3)
由于
,您的ListView没有显示任何内容@Override
public int getCount(){
return 0;
}
getCount
必须返回属于您的数据集的元素数量。例如
@Override
public int getCount(){
return text1.length;
}
答案 1 :(得分:1)
你从getCount
返回0。返回列表中的项目数,我怀疑你想要text1.length
。
答案 2 :(得分:0)
嘿,我看到了我的简单和最好的例子,按照一些步骤
我主要使用自定义进行测试
JSON解析 第1步。
JSONParser.java
public class JSONParser {
InputStream is = null;
JSONObject jObj = null;
String json = "";
// constructor
public JSONParser() {
}
public String getJSONFromUrl(String url) {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.e("response", "" + httpResponse);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
}
9月2日。
MainHome.java
public class MainActivity extends Activity {
ListView lvdata;
LinkedList<HashMap<String, String>> aldata = new LinkedList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
new getData().execute();
}
void init() {
lvdata = (ListView) findViewById(R.id.lvdata);
}
private class getData extends AsyncTask<Void, Void, String> {
Dialog dialog;
String url;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
url = "Enter your url";
JSONParser jParser = new JSONParser();
String json = jParser.getJSONFromUrl(url);
try {
JSONObject jobject = new JSONObject(json);
int success = jobject.getInt("success");
for (int i = 0; i < jobject.length() - 1; i++) {
JSONObject jobj = jobject
.getJSONObject(Integer.toString(i));
if (success == 1) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("p_name", jobj.getString("p_name"));
hm.put("p_title", jobj.getString("p_title"));
aldata.add(hm);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog != null)
if (dialog.isShowing())
dialog.dismiss();
Custom_Adapter adapter = new Custom_Adapter(
(Activity) MainActivity.this, aldata);
lvdata.setAdapter(adapter);
}
}
}
Custom_Adapter.java
public class Custom_Adapter extends ArrayAdapter<HashMap<String, String>> {
private final Activity context;
private final LinkedList<HashMap<String, String>> allData;
public Custom_Adapter(Activity context,
LinkedList<HashMap<String, String>> list) {
super(context, R.layout.custom_list, list);
this.context = context;
this.allData = list;
}
static class ViewHolder {
TextView tvname, tvinfo;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
view = null;
if (view == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.custom_list, null);
final ViewHolder viewHolder = new ViewHolder();
initAll(view, viewHolder);
view.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) view.getTag();
fillAll(holder, position);
return view;
}
public void initAll(View view, ViewHolder viewHolder) {
viewHolder.tvname = (TextView) view.findViewById(R.id.tvname);
viewHolder.tvinfo = (TextView) view.findViewById(R.id.tvinfo);
}
public void fillAll(final ViewHolder holder, final int position) {
holder.tvname.setText(allData.get(position).get("p_name"));
holder.tvinfo.setText(allData.get(position).get("p_title"));
}
}