这是我的java代码;
public class Main extends Activity {
ListView lvLokacije;
String sLista, class;
Class<?> ourClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvLokacije = (ListView) findViewById(R.id.lvLokacije);
String lista[] = { "Class1", "Class2", "Class3", "EXIT", };
ArrayAdapter<String> aLista = new ArrayAdapter<String>(Main.this,
R.layout.costum_list_item2, lista);
lvLokacije.setAdapter(aLista);
lvLokacije.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
sLista = (String) (lvLokacije.getItemAtPosition(myItemInt));
String ispis = sLista.toString();
if (ispis.contentEquals("Class1")) {
class= "myClass1";
} else if (ispis.contentEquals("Class2")) {
class= "myClass2";
} else if (ispis.contentEquals("Class3")) {
class= "myClass3";
} else if (ispis.contentEquals("IZLAZ")) {
onBackPressed();
}
try {
ourClass = Class.forName("crni03.obracun." + class);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent ourIntent = new Intent(Main.this, ourClass);
startActivity(ourIntent);
}
});
}
@Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
// This above line close correctly
}
}
这是costum_list_item2 XML;
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:padding="3dp"
android:singleLine="true"
android:textColor="#FFFF4D"
android:textSize="19sp" />
我在costum_list_item2中设置了文本颜色,但我想从java更改它,但我不知道如何。 我想要一个用户可以更改列表项颜色的功能。
当我将android:id="@android:id/text1"
更改为android:id="@+id/text1"
时
我可以创建一个TextView并调用该id,但这是我可以去的。
每当我尝试使用TextViev做某事时,我都会收到null.pointer.exeption错误。
感谢名单!
答案 0 :(得分:0)
您无法在此处直接访问textview。由于每个textview都是id为#lvLokacije&#39;的视图中的子元素。什么是lvLokacije在这里。如果它是listview或gridview,那么您可以使用getView()方法访问视图的行。这就是我使用它的方法,其中position是listview中元素的从零开始的索引。
ListView listView;
String list[] = { "TEXT", "TEXT2", "TEXT3", "TEXT4"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
ListAdapter adapter = new ListCellAdapter(this);
listView.setAdapter(adapter);
}
public class ListCellAdapter extends BaseAdapter
{
Context ctx;
TextView txtView;
public ListCellAdapter(Context context)
{
super();
ctx = context;
}
@Override
public int getCount() {
return list.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.costum_list_item2, parent, false);
}
txtView = ((TextView)(row.findViewById(android.R.id.text1)));
txtView.setText(String.valueOf(position));
if(position == 1)
{
txtView.setTextColor(Color.CYAN);
}
else
{
txtView.setTextColor(Color.BLUE);
}
return row;
}
}
}
答案 1 :(得分:0)
Thanx Durga !!! 在Durga帮助之后,这是我的代码
public class Main extends Activity {
ListView lvLokacije;
String class;
Class<?> ourClass;
String list[] = { "Class1", "Class2", "Class3",
"EXIT", };
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvLokacije = (ListView) findViewById(R.id.lvLokacije);
ListAdapter adapter = new ListCellAdapter(this);
lvLokacije.setAdapter(adapter);
}
public class ListCellAdapter extends BaseAdapter implements
OnItemClickListener {
Context ctx;
TextView txtView;
public ListCellAdapter(Context context) {
super();
ctx = context;
lvLokacije.setOnItemClickListener(this);
}
public int getCount() {
return list.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.costum_list_item2, parent,
false);
}
txtView = ((TextView) (row.findViewById(android.R.id.text1)));
txtView.setText(String.valueOf(list[position]));
txtView.setTextColor(Color.BLUE);
return row;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Object oList = (list[arg2]);
String ispis = sList.toString();
if (ispis.contentEquals("Class1")) {
class = "myClass1";
} else if (ispis.contentEquals("Class2")) {
class = "myClass2";
} else if (ispis.contentEquals("Class3")) {
class = "myClass3";
} else if (ispis.contentEquals("EXIT")) {
onBackPressed();
}
try {
ourClass = Class.forName("crni03.obracun." + class);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent ourIntent = new Intent(Main.this, ourClass);
startActivity(ourIntent);
}
}
@Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
// This above line close correctly
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}