这是我完成的android studio项目编码: 我知道我犯了多个错误,但在哪里? 我是初学者
这是我的MainActivity类:
public class MainActivity extends AppCompatActivity{
public Elements titles;
public ArrayList<String> news_list = new ArrayList<>();
private CustomAdapter customAdapter;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
customAdapter = new CustomAdapter(this,news_list);
new NewThread().execute();
}
public class NewThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg) {
Document doc;
try {
doc = Jsoup.connect("http://www.varzesh3.com").get();
titles = doc.getElementsByClass("small-news-link");
news_list.clear();
for (Element news : titles) {
news_list.add(news.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
lv.setAdapter(customAdapter);
}
}
}
这是我的CustomAdapter类
public class CustomAdapter extends ArrayAdapter<String> {
List<String> listString;
Activity context;
Typeface typeFace;
public CustomAdapter(MainActivity mainActivity, List<String> listString) {
super(mainActivity, R.layout.list_item,listString);
this.context = mainActivity;
this.listString = listString;
}
static class ViewHolder {
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
LayoutInflater inflater = context.getLayoutInflater();
vi = inflater.inflate(R.layout.list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView = (TextView) vi.findViewById(R.id.news);
vi.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) vi.getTag();
typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/IRANSans.ttf");
holder.textView.setTypeface(typeFace);
return vi;
}
}`
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="15dip"
android:textStyle="bold"
android:textColor="#000000"/>
答案 0 :(得分:1)
在getView()
的{{1}}方法中,您实际上从未显示任何文字。您为视图充气,创建CustomAdapter
,设置ViewHolder
...但不显示任何文字。添加看起来像这样的代码:
Typeface
答案 1 :(得分:0)
@Ben P.给你确切的答案,你没有从任何地方获得字符串,你刚刚膨胀list_row而没有得到任何acsual字符串。
我建议你打开一个新类来处理带有外部类的Strings / Objetcs,然后用getter和setter说明 StringClass 并将ArrayList<StringClass> list;
放在上面并将它连接到适配器之后只需在asynctask中使用它,更容易并使用OOP ...
干杯,祝你好运!