Airborne
Airborne

Reputation: 31

the following android CustomAdapter does not work?

Here is my completed android studio project coding: i know i was made multiple errors but where?? i am very beginner

It is my MainActivity class:

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);

    }
}

}

And it is my CustomAdapter class

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"/>

Upvotes: 1

Views: 80

Answers (2)

z3r0
z3r0

Reputation: 49

@Ben P. Gave You the exact answer, You dont get the Strings from anywhere, You have just inflated the list_row without getting any acsual Strings. I would advise You openinng a new class to handle the Strings/Objetcs with an outside class lets say StringClass with getters and setters and put the ArrayList<StringClass> list; in the above and connect it to the adapter and after that just use it in the asynctask, easier and with OOP... Cheers and good luck!

Upvotes: 0

Ben P.
Ben P.

Reputation: 54194

In your getView() method of CustomAdapter, you never actually display any text. You inflate the view, create the ViewHolder, set the Typeface... but don't display any text. Add code that looks something like this:

String s = listString.get(position);
holder.textView.setText(s);

Upvotes: 1

Related Questions