Rohit
Rohit

Reputation: 2681

List Arrayadapter not showing Anything

I am parsing XML and trying to propagate it in list view.

I am using Following code

Main Activity,

public class MainActivity extends ListActivity {

ArrayList<String> xmlList=new ArrayList<String>();
ArrayList<String> xmlList1=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try{
        InputStream is=getResources().openRawResource(R.raw.sample);

        DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc=builder.parse(is, null);

        NodeList nodes=doc.getElementsByTagName("id");
        NodeList nodes1=doc.getElementsByTagName("Name");

        if(nodes!=null && nodes.getLength() >0)
        {
            xmlList.clear();
            xmlList1.clear(); 
            int len=nodes.getLength();

            for(int i=0;i<len;i++)
            {
                Node node=nodes.item(i);
                Node node1=nodes1.item(i);
                xmlList.add(node.getTextContent());
            }
        }
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), ""+e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    CustomList adapter=new CustomList(MainActivity.this,xmlList,xmlList1);
    setListAdapter(adapter);

CustomList.java

public class CustomList extends ArrayAdapter<String> {

Context context;
private final ArrayList<String> mainText;
private final ArrayList<String> subText1;

public CustomList(Context context, ArrayList<String> mainText, ArrayList<String>  subText1) {
    super(context,R.layout.custumlist);
    // TODO Auto-generated constructor stub
    this.context=context;
    this.mainText=mainText;
    this.subText1=subText1;

}

@Override
public View getView(int position, View view, ViewGroup parent)
{
    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    view=inflater.inflate(R.layout.custumlist, null);

    TextView main=(TextView)view.findViewById(R.id.mainText);
    TextView sub1=(TextView)view.findViewById(R.id.subText1);

    main.setText(mainText.toString());
    sub1.setText(subText1.toString());
    return view;
}

}

But it is not showing anything nor posting any log cat. I think there is something wrong inside infliating the View. Minor problem but not able to find. Please help me in that.

Thnx

Upvotes: 0

Views: 353

Answers (2)

MHP
MHP

Reputation: 2731

in your CustomList put below code:

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mainText.size();
}   

Upvotes: 2

Apoorv
Apoorv

Reputation: 13520

Override the getCount method in your CustomList

@Override
public int getCount()
{
  return mainText.size();
}

OR

change the super call in CustomList constructor from

super(context, R.layout.custumlist);

to

super(context, R.layout.custumlist,mainText);

Upvotes: 5

Related Questions