gpt4 book ai didi

java - Android - 链接到网站的自定义 ListView

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:01 25 4
gpt4 key购买 nike

所以我创建了一个自定义 ListView,列表中有 2 个对象。例如,列表中的第一个是猫,第二个是狗。然后,当点击其中一个时,它将导航到不同的 Activity 屏幕。例如,当用户点击“狗”时,它将转到另一个屏幕,其中显示一个新列表,列表中的名称包括“柯基犬”、“哈士奇”、“哈士奇”等。然后,当点击某个狗品种时,它将转发到一个网站。例如,当用户点击“Corgi”时,它会将用户转发到 Corgi 的 Wiki 页面。

我将如何创建应用程序的第三部分?

我的代码如下:

主要 Activity :

    public class MainActivity extends ListActivity implements OnItemClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] data = { "Dogs", "Cats" };
int[] icons = { R.drawable.dogz, R.drawable.catz };

// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data, icons));

/* setOnItemClickListener() Register a callback to be invoked when an item
* in this AdapterView has been clicked.
*/
getListView().setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(), ChildActivity.class);

// Add extended data to the intent.
intent.putExtra("POSITION", position);

/*
* Launch a new activity. You will not receive any information about when
* the activity exits. This implementation overrides the base version,
* providing information about the activity performing the launch.
*/
startActivity(intent);
}

}

子 Activity :

    public class ChildActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[][] data = {
{ "Corgi", "Pugs", "Husky" },
{ "Siamese", "Persian", "Maine Coon" } };
int[][] icons = {
{ R.drawable.d, R.drawable.e, R.drawable.f },
{ R.drawable.a, R.drawable.b, R.drawable.c }, };
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);

// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data[position],
icons[position]));

}

}

自定义列表适配器:

public class CustomListAdapter extends ArrayAdapter<String> {

private final Context context;
private final String[] values;
private final int[] icons;

public CustomListAdapter(Context context, String[] values, int[] icons) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
this.icons = icons;
}

@Override
// Get a View that displays the data at the specified position in the data set.
public View getView(int position, View convertView, ViewGroup parent) {

/*
* Instantiates a layout XML file into its corresponding View objects.
* It is never used directly. Instead, use getSystemService(String) to
* retrieve a standard LayoutInflater instance that is already hooked up to
* the current context and correctly configured for the device you are running on.
*/
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

/*
* Inflate a new view hierarchy from the specified xml resource.
* Throws InflateException if there is an error.
*/
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtStatus);
textView.setText(values[position]);
Drawable draw = context.getResources().getDrawable(icons[position]);
Bitmap bitmap = ((BitmapDrawable) draw).getBitmap();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
Drawable newDraw = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bitmap, 40 * w / h, 40, true));

/*
* Sets the Drawables (if any) to appear to the left of, above, to the right of,
* and below the text. Use 0 if you do not want a Drawable there.
* The Drawables' bounds will be set to their intrinsic bounds.
*/
textView.setCompoundDrawablesWithIntrinsicBounds(newDraw, null, null,
null);
return rowView;
}
}

我知道我必须创建第三个 Activity ,但我不知道如何在不使用 .xml 的情况下进行它。提前致谢。

最佳答案

  1. 尝试使用动物项目制作适配器。
  2. OnItemClick() 打开 WebViewFragment并将您的 Animal 对象或 Bundle 中的 Animal.getUrl()-String 传递给它
  3. (可选)查看 ExpandableListView以防止不必要的深层导航层次结构。

关于java - Android - 链接到网站的自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731910/

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