2

[EDIT] I'm new to android development, so please bear with me. I have two java classes named Join Game, extends to AppCompatActivity and HintList,extends to ArrayAdapter<>. This is connected to a database. So maybe its one of the factors?

For the layout of join game, I have a listview and for the layout of hintlist I have three textview.

The code goes this way

JoinGame

public class JoinGame extends AppCompatActivity {
ListView list;
String[] itemdescription = {};
String[] itemhints = {};
String[] itemlocasyon = {};
ProgressDialog progress;
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_joingame);
    Button schan = (Button)findViewById(R.id.tarascan);
    schan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(JoinGame.this,ScanActivity.class);
            startActivity(intent);
        }
    });
}

 @Override
protected void onStart() {
    super.onStart();
    progress = new ProgressDialog(this);
    progress.setMessage("Connecting...");
    progress.setCancelable(false);
    progress.show();

    RequestFactory.joingameitems(JoinGame.this, RequestFactory.user_id, new RequestCallback<JSONArray>() {
        @Override
        public void onSuccess(final JSONArray response) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    RequestFactory.response = response;
                    itemdescription = new String[response.length()];
                    itemhints = new String[response.length()];
                    itemlocasyon = new String[response.length()];

                    for (int hl = 0; hl < response.length(); hl++){
                        try{
                            itemdescription[hl] = ((String)(response.getJSONObject(hl)).get("description"));
                            itemhints[hl] = ((String)(response.getJSONObject(hl)).get("hint"));
                            itemlocasyon[hl] = ((String)(response.getJSONObject(hl)).get("location"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } ////////// below this is the adapter
                    final HintList hladapt = new HintList(JoinGame.this,itemdescription,itemlocasyon,itemhints);
                    list = (ListView)findViewById(R.id.item_hints_and_location);
                    list.setAdapter(hladapt);
                    progress.dismiss();
                }
            });
        }

        @Override
        public void onFailed(final String message) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(JoinGame.this, message, Toast.LENGTH_LONG).show();
                            progress.dismiss();
                        }
                    });
                }
            });
        }
    });

}

HintList

   public class HintList extends ArrayAdapter<String> {
    private final Activity context;
    private String[] itemDesc = {};
    private String[] itemHints = {};
    private String[] itemLocation = {};

    public HintList(Activity context,String[] itemDesc,String[] itemhints,String[] itemlocation) {
        super(context,R.layout.hints_list);
        this.context = context;
        this.itemDesc = itemDesc;
        itemHints = itemhints;
        itemLocation = itemlocation;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.hints_list, null, true);
        TextView itemDesc2 = (TextView)rowView.findViewById(R.id.itemdescription);
        TextView itemHint = (TextView)rowView.findViewById(R.id.itemhint);
        TextView itemLocation2 = (TextView)rowView.findViewById(R.id.itemlocation);

        itemDesc2.setText(itemDesc[position]);
        itemHint.setText(itemHints[position]);
        itemLocation2.setText(itemLocation[position]);

        return rowView;
    }
}

I actually retrieved the data (here)

E/DB: [{"description":"chinese garter","location":"near Tricycle station","hint":"garter plus chinese"},{"description":"isang pinoy game","location":"near ....","hint":"may salitang baka"},{"description":"\"tinik\"","location":"below...","hint":"may salitang tinik"},{"description":"aka Tinubigan","location":"at the back...","hint":"katunog ng pintero"},{"description":"\"knock down the can\"","location":"near...","hint":"gumagamit ng lata"}]

but it doesnt display on my listview I dont know anymore what I should do. I actually tried making this (I added view)

final HintList hladapt = new HintList(JoinGame.this,itemdescription,itemlocasyon,itemhints);
                    list = (ListView)view.findViewById(R.id.item_hints_and_location);
                    list.setAdapter(hladapt);
                    progress.dismiss();

but it will only returns an error of java.lang.NullPointerException: Attempt to invoke virtual method

4 Answers4

1

Change this:

View rowView = inflater.inflate(R.layout.hints_list, null, true);

to:

View rowView = inflater.inflate(R.layout.hints_list, parent, false);

Modify your getView() method to:

    @Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.hints_list, parent, false);
    TextView itemDesc2 = (TextView)rowView.findViewById(R.id.itemdescription);
    TextView itemHint = (TextView)rowView.findViewById(R.id.itemhint);
    TextView itemLocation2 = (TextView)rowView.findViewById(R.id.itemlocation);

    itemDesc2.setText(itemDesc[position]);
    itemHint.setText(itemHints[position]);
    itemLocation2.setText(itemLocation[position]);

    return rowView;
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

try adding getCount

@Override
    public int getCount() {
        return itemHints.length;
    }

Try to change your code to this

public class HintList extends ArrayAdapter<String> {
private final Activity context;
private String[] itemDesc = {};
private String[] itemHints = {};
private String[] itemLocation = {};
private LayoutInflater inflater=null;

public HintList(Activity context,String[] itemDesc,String[] itemhints,String[] itemlocation) {
    super(context,R.layout.hints_list);
    this.context = context;
    this.itemDesc = itemDesc;
    itemHints = itemhints;
    itemLocation = itemlocation;
        inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
     return itemHints.length;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
        if (view==null){
            view=inflater.inflate(R.layout.hints_list, null, true);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

    holder.itemDesc2.setText(itemDesc[position]);
    holder.itemHint.setText(itemHints[position]);
    holder.itemLocation2.setText(itemLocation[position]);

    return view;
}
static class ViewHolder{

        TextView itemDesc2,itemHint,itemLocation2;
        ViewHolder(View view) {
            itemDesc2 = (TextView)view.findViewById(R.id.itemdescription);
    itemHint = (TextView)view.findViewById(R.id.itemhint);
    itemLocation2 = (TextView)view.findViewById(R.id.itemlocation);
        }
    }
}
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0

My suggestions is create a bean class (DetailsBean), used for setter and getter method, and also the ArrayList (details1).

List<DetailsBean> details1 = new ArrayList<>(); // declare this as global

Then add the bean to below code

  public void run() {
         RequestFactory.response = response;
         itemdescription = new String[response.length()];
         itemhints = new String[response.length()];
         itemlocasyon = new String[response.length()];

         for (int hl = 0; hl < response.length(); hl++){
          try{
                itemdescription[hl] = ((String)(response.getJSONObject(hl)).get("description"));
                itemhints[hl] = ((String)(response.getJSONObject(hl)).get("hint"));
                itemlocasyon[hl] = ((String)(response.getJSONObject(hl)).get("location"));
                // here the bean 
                DetailsBean dbean = new DetailsBean(itemDescription, itemhints, itemlocasyon);
               details1.add(dbean); // add all the data to details1 ArrayList
               HintList hladapt = new HintList(getActivity(), details1);
              (ListView)findViewById(R.id.item_hints_and_location);
               list.setAdapter(hladapt);
            } catch (JSONException e) {
              e.printStackTrace();
         }
    }

Your DetailsBean should looked like this

public class DetailsBean {

        private String itemDescription="";
        private String itemhints="";
        private String  itemlocasyon ="";


    public DetailsBean(String description, String hints, String itemlocasyon) {
       this.itemDescription=description;
        .....
 }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDesription;
    }

    public String getItemDescription() {
        return itemDescription;
    }
       .... 
}

Then your HintList

public class HintList extends BaseAdapter{   
    Activity context;
    List<DetailsBean> details;
    private LayoutInflater mInflater;

    public CustomBaseAdapter(Activity context,List<DetailsBean> details) {
        this.context = context;
        this.details = details;
    }

     ........
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • okay so I should create a new java class, name it DetailsBean. then write the code above? And declare List details1 = new ArrayList<>(); // declare this as global inside JoinGame class? am I correct? – Clarizze Esteban Mar 24 '17 at 10:17
  • HintList hladapt = new HintList(getActivity(), details1); the get activity is error – Clarizze Esteban Mar 24 '17 at 10:29
  • I should use the code for HintList and delete my created code? – Clarizze Esteban Mar 24 '17 at 10:31
  • I'm having errors with DetailsBean dbean = new DetailsBean(itemdescription,itemhints,itemlocasyon); it looks like it is not the expected parameters but I believe it is it. But i dont know why this is happening – Clarizze Esteban Mar 24 '17 at 10:39
0

Add these Override methods in your adapter

@Override
public int getCount() {
    return itemHints.length;
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}
AlexGuerra
  • 179
  • 7