gpt4 book ai didi

java - ListView 不显示 - Android 工作室

转载 作者:行者123 更新时间:2023-11-30 10:43:21 24 4
gpt4 key购买 nike

我有一个包含 ListView 的 Activity 。它将包含 3 个名称,但未显示。CinemaActivity 创建一个新的 ListView 和一个新的适配器(下面的代码),然后检查 ListView 的哪个项目被按下并打开一个新的 Activity 。现在的问题只是 ListView。

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

最佳答案

改变

super(context, layoutResourceId);

super(context, layoutResourceId, data);

关于java - ListView 不显示 - Android 工作室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com