[英]Android list with ListAdapter layout Android Studio
I'm working with android studio and I have this list and I can't get the layout to work.(You'll notice I have a listener that opens another activity when you click on a single item, and that works just fine). 我正在使用android studio,但我有此列表,但无法使用布局(您会注意到我有一个侦听器,当您单击单个项目时,它会打开另一个活动,并且效果很好) 。 Here's my activity_estoque.xml file: 这是我的activity_estoque.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
and this is the estoque.java class, that uses the .xml file above: 这是estoque.java类,它使用上面的.xml文件:
package com.example.asus.mingausfashionmoda;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class estoque extends ListActivity {
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products));
ListView lv = getListView();
//listening to single item in list view
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selected item
LinearLayout ll = (LinearLayout) view;
TextView tv = (TextView) ll.findViewById(R.id.label);
final String product = tv.getText().toString();
//String product = ((TextView) view).getText().toString();
//lauching new activity to single item
Intent i = new Intent(getApplicationContext(), estoqueSingle.class);
//sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
I added some pictures to help you understand what I want and what I'm getting(sorry for bad paiting): 1 - The screen on the left is what I should see, according to the Android Studio activity_estoque.xml Design session, and the right one is what I actually get(Notice the "Mingau's fashion moda" thing missing, I really want this blue thing): ( What I should get vs what I'm getting ) 我添加了一些图片,以帮助您了解我想要的东西和我得到的东西(对不起,我感到很抱歉):1-根据Android Studio activity_estoque.xml设计会话,左侧的屏幕是我应该看到的屏幕,正确的是我实际得到的(注意缺少“ Mingau的时尚模式”,我真的想要这件蓝色的东西):( 我应该得到的与我得到的 )
2 - Now, the left screen is what I want, a top single button and the list bellow. 2-现在,左侧屏幕是我想要的,顶部是单个按钮,下面是列表。 The right one is what I'm receiving after adding a button on the activity_estoque.xml file(code bellow): ( What I want vs what I get ) 正确的是我在activity_estoque.xml文件上添加了一个按钮之后收到的内容(代码在下面):( 我想要的与我得到的 )
this is the activity_estoque.xml file after I add the button: 添加按钮后,这是activity_estoque.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnttt"
android:text="This is a button"
/>
</LinearLayout>
First I thought that I should add a setContentView(R.layout.activity_estoque);
首先,我认为我应该添加setContentView(R.layout.activity_estoque);
on the onCreat method
, like this: 在onCreat method
,如下所示:
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estoque);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
but if I do that I get the following error, and I'm not sure if it makes any sense to add this setContentView...
: 但是如果这样做,我将收到以下错误,并且不确定添加此setContentView...
是否有意义:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.mingausfashionmoda/com.example.asus.mingausfashionmoda.estoque}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
and this: 和这个:
at com.example.asus.mingausfashionmoda.estoque.onCreate(estoque.java:20)
this is what I have on estoque.java.20:
这是我在estoque.java.20:
上estoque.java.20:
setContentView(R.layout.activity_estoque);
Anyone can help? 有人可以帮忙吗? Thanks. 谢谢。
You are extending a ListActivity, so when you start the activity it will use the default layout. 您正在扩展ListActivity,因此当您启动活动时,它将使用默认布局。 If you try to do setContentView(R.layout.activity_estoque);
如果尝试执行setContentView(R.layout.activity_estoque);
in the code then the activity will expect your layout to hava list view with the id "list". 在代码中,活动将使您的布局具有ID为“ list”的列表视图。
Edit-- Change your layout to 编辑-将布局更改为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnttt"
android:text="This is a button"
/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
and 和
and do setContentView(R.layout.activity_estoque)
in onCreate 并在onCreate中do setContentView(R.layout.activity_estoque)
You will need to create a new layout for your list item view, which you need to pass to the ArrayAdapter For eg: activity_test.xml 您将需要为列表项视图创建一个新的布局,您需要将其传递给ArrayAdapter例如:activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
then change this this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products));
然后更改此this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products));
to this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_test, R.id.label1, adobe_products));
到this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_test, R.id.label1, adobe_products));
What you want to achieve is explained here as example here . 你想达到在此说明的例子是什么在这里 。 And the blue bar above you see is actually achievable using Action Bar example here . 您实际上可以使用此处的 “操作栏” 示例来实现上方的蓝色栏。 This action bar is configurable for color, icon, title, sub title and the menus(show/not to show). 该操作栏可配置为颜色,图标,标题,副标题和菜单(显示/不显示)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.