G. Ferruzzi G. Ferruzzi - 5 months ago 21
Android Question

ListView not showing - Android studio

I've got an activity containing a ListView. It will contain 3 names but it's not showing up.
CinemaActivity create a new ListView and a new Adapter (code below) and then check which item of the ListView get pressed and open a new activity.
The problem is just the ListView for now.

CinemaActivity.java

public class CinemaActivity extends AppCompatActivity {
public Upcoming up;
public MostPopular mos;

public List<Data> dataUP;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cinema_activity);

initUI();
}

public void initUI() {

String data[] = new String[]{"Most Popular", "UpComing", "Top Rated"};

ListView cinemaListview = (ListView) findViewById(R.id.lwCinema);
cinemaMainAdapter cma = new cinemaMainAdapter(this, android.R.layout.simple_list_item_1, data);
cinemaListview.setAdapter(cma);
cinemaListview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapter, final View component, int pos, long id) {

try {
openActivity(pos);
} catch (Exception e) {
e.printStackTrace();
}

}

});


}

public void openActivity(int pos) {

switch(pos) {
case 0:
Intent mpIntent = new Intent(this, MostPopular.class);
startActivity(mpIntent);
break;
case 1:
Intent ucIntent= new Intent(this, Upcoming.class);
startActivity(ucIntent);
break;
case 2:
Intent trIntent = new Intent(this, TopRated.class);
startActivity(trIntent);
break;
}

}


cinemaMainAdapter.java

public class cinemaMainAdapter extends ArrayAdapter<String> {

Context context;
int layoutResourceId;
String data[] = null;

public cinemaMainAdapter(Context context, int layoutResourceId, String data[]) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.cinema_listview, parent, false);

holder = new ViewHolder();
holder.textviewHolder = (TextView) convertView.findViewById(R.id.twcinemaLayoutMain);

convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

String title = data[position];
holder.textviewHolder.setText(title);

return convertView;
}

static class ViewHolder {
public TextView textviewHolder;
}


}

Answer

Just change

super(context, layoutResourceId);

to

super(context, layoutResourceId, data);