gpt4 book ai didi

Android Listview - 如何在 getview() 覆盖中设置 ListView 。 ListView 采用父布局而不是 ListView

转载 作者:行者123 更新时间:2023-11-29 17:56:15 27 4
gpt4 key购买 nike

我之前在论坛上发布过这个,但我想我不太清楚。我有一个侧边栏 ListView 和一个包含我的控件的主面板。我想在 oncreate() 中突出显示 listview 中的 Activity 项,因为每次单击边栏都会启动一个新 Activity 。这是我的 listview 布局供引用- enter image description here

但是我写的 GetView() override 错了,所以我的布局最终是这样的。 enter image description here

这是我的 Activity 布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?android:attr/activatedBackgroundIndicator">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="@+id/textViewMenuA2"
android:text="Menu"
android:background="@android:color/background_dark"
android:textSize="20dp"
>

</TextView>

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center"
android:background="@android:color/holo_green_light"
android:minHeight="50dp"
/>

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="2">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time To Gather Knapsacks
"
android:id="@+id/textView4"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:layout_marginTop="0dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of KnapSacks"
android:id="@+id/textView5"
android:layout_marginLeft="28dp"
android:layout_marginTop="10dp"
android:textSize="15dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/numberKnapSack"
android:layout_marginLeft="200dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Time"
android:id="@+id/A2Stbutton"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/textView5" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:id="@+id/A2textView2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/numberKnapSack"
android:textSize="20dp"
android:layout_marginLeft="15dp" />
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Time"
android:id="@+id/A2Stopbutton2"
android:layout_marginLeft="25dp"
android:onClick="SetEndTime"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:id="@+id/A2textView3"
android:layout_marginLeft="212dp"
android:textSize="20dp"
android:layout_toLeftOf="@+id/A2button3"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/A2button3"
android:layout_marginTop="45dp" />
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:layout_alignBottom="@+id/A2button3"
android:layout_alignParentLeft="true"
android:layout_marginBottom="42dp"
android:layout_gravity="left|center_vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/A2button3"
android:layout_marginTop="0dp"
android:onClick="NextActivity"
android:layout_marginRight="65dp"
android:layout_below="@+id/A2Stopbutton2"
android:layout_alignParentRight="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/imageView"
android:layout_below="@+id/button"
android:layout_toLeftOf="@+id/A2button3"
android:layout_marginTop="20dp" />

</RelativeLayout>
</LinearLayout>

</LinearLayout>

这是我的自定义适配器

package com.example.listviewandroid;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
* Created by on 10/2/13.
*/
public class CustomAdapter extends BaseAdapter{

Context mycontext;
ArrayList<String> contactsList;
LayoutInflater minflater;

public CustomAdapter(Context context, ArrayList<String> list)
{
this.mycontext=context;
contactsList=list;
minflater =LayoutInflater.from(context);

}
@Override
public int getCount()
{
return contactsList.size();
}


@Override
public Object getItem(int position)
{
return contactsList.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position,View convertView,ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view =inflater.inflate(R.layout.layout2,parent,false);

if (convertView !=null)
{
if (position ==1)
{
view.setBackgroundColor(Color.BLUE);

}
}
return view;
}

}

这是我的 Activity 代码

package com.example.listviewandroid;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.text.format.Time;

import java.util.ArrayList;


/**
* Created by on 9/26/13.
*/
public class Activity2 extends Activity {
ListView listView ;
static final String A2STE="";
static final String A2STM="";
static final String Knapsacks="";
@Override

protected void onCreate(Bundle savedInstanceState) {




super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
listView = (ListView) findViewById(R.id.listView);

ArrayList<String > list = new ArrayList<String>();

list.add("Test");
list.add("Test");list.add("Test");list.add("Test");list.add("Test");list.add("Test");


CustomAdapter adapter=new CustomAdapter(this,list);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {



switch( position )
{
case 0: Intent newActivity = new Intent(Activity2.this,MainActivity.class);
startActivity(newActivity);

break;

case 2: Intent newActivity1 = new Intent(Activity2.this,Activity3.class);
startActivity(newActivity1);

break;
case 3: Intent newActivity2 = new Intent(Activity2.this,Activity4.class);
startActivity(newActivity2);

break;
case 4: Intent newActivity3 = new Intent(Activity2.this,Activity5.class);
startActivity(newActivity3);

break;
case 5: Intent newActivity4 = new Intent(Activity2.this,Activity6.class);
startActivity(newActivity4);

break;

}
/* Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
*/
}

});
Time now = new Time();
now.setToNow();
TextView textview2= (TextView)findViewById(R.id.A2textView2);
textview2.setText(now.hour+" : "+now.minute);

if (savedInstanceState !=null)
{
restoreState(savedInstanceState);

}

this.listView.setSelector(R.drawable.selector_resource);
}



private void restoreState(Bundle savedInstanceState) {

EditText editText=(EditText)findViewById(R.id.numberKnapSack);

editText.setText(savedInstanceState.getString(Knapsacks));

TextView startime=(TextView)findViewById(R.id.A2textView2);

startime.setText(savedInstanceState.getString(A2STE));

TextView endtime=(TextView)findViewById(R.id.A2textView3);

endtime.setText(savedInstanceState.getString(A2STM));



}





public void SetEndTime(View view) {
Time now = new Time();
now.setToNow();
TextView textview3= (TextView)findViewById(R.id.A2textView3);
textview3.setText(now.hour+" : "+now.minute);

}


@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

EditText editText=(EditText)findViewById(R.id.numberKnapSack);
outState.putString(Knapsacks,editText.getText().toString());

TextView startime=(TextView)findViewById(R.id.A2textView2);
outState.putString(A2STE,startime.getText().toString());

TextView endtime=(TextView)findViewById(R.id.A2textView3);
outState.putString(A2STM,endtime.getText().toString());




}

public void NextActivity(View view) {
Intent newActivity1 = new Intent(Activity2.this,Activity3.class);
startActivity(newActivity1);
this.overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);

}

}

我哪里错了?

最佳答案

这是你的问题

在你的 Activity 中你这样做

setContentView(R.layout.layout2);

这很好,但是在您的 adaptergetView 中,您也可以这样做

View view =inflater.inflate(R.layout.layout2,parent,false);

所以你再次膨胀相同的布局,显然你不希望这样

关于Android Listview - 如何在 getview() 覆盖中设置 ListView 。 ListView 采用父布局而不是 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19146399/

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