可以在适配器中放入if / else语句吗?这是代码:
我希望将textview的可见性设置为在其为空时消失,否则它将可见。
package daily.diary;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class MainActivity extends ListActivity {
Intent intent;
public static ArrayList<HashMap<String, String>> listresults;
public static int val = 2;
DBHelper controller = new DBHelper(this);
public int entry;
ArrayAdapter<String> myadapter;
public ListView lv;
TextView Id, counter, title, content;
private int count;
private EditText searchtext;
private Button cancel, btnsearch,settings;
public static String searchkeyword;
private ArrayList<HashMap<String, String>> EntryList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
counter = (TextView) findViewById(R.id.cnt);
searchtext = (EditText) findViewById(R.id.searchtext);
cancel = (Button) findViewById(R.id.cancel);
btnsearch = (Button) findViewById(R.id.search);
settings = (Button) findViewById(R.id.settings);
lv = (ListView) findViewById(android.R.id.list);
registerForContextMenu(getListView());
EntryList = controller.getAllEntry();
if (EntryList.size() != 0) {
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
val = 0;
Id = (TextView) view.findViewById(R.id.Id);
String valId = Id.getText().toString();
MainActivity.this.onDestroy();
Intent objIndent = new Intent(getApplicationContext(),
EditEntry.class);
objIndent.putExtra("Id", valId);
startActivity(objIndent);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
Id = (TextView) v.findViewById(R.id.Id);
title = (TextView) v.findViewById(R.id.title);
content = (TextView) v.findViewById(R.id.content);
return false;
}
});
viewentry();
count = lv.getCount();
counter.setText("Entries:" + count);
}
}
/*@Override
public void onBackPressed() {
Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG).show();
super.onBackPressed();
}*/
public void viewentry() {
EntryList = controller.getAllEntry();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
EntryList,
R.layout.timeline,
new String[] { "Id", "title", "content", "date", "time", "category", "tag", "location", "mood" },
new int[] { R.id.Id, R.id.title, R.id.content, R.id.date, R.id.time, R.id.category, R.id.tag, R.id.location, R.id.mood }
);
if (TextUtils.isEmpty(title.getText())){
title.setVisibility(View.GONE);
}
setListAdapter(adapter);
}
public void onCancel(View view) {
searchtext.setText("");
searchtext.setVisibility(View.GONE);
cancel.setVisibility(View.GONE);
settings.setVisibility(View.VISIBLE);
btnsearch.setVisibility(View.VISIBLE);
btnsearch.setEnabled(true);
}
public void onSearch(View view) {
if (searchtext.getVisibility() == View.GONE) {
btnsearch.setEnabled(false);
cancel.setVisibility(View.VISIBLE);
searchtext.setVisibility(View.VISIBLE);
btnsearch.setVisibility(View.INVISIBLE);
settings.setVisibility(View.GONE);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i,
int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i1, int i2) {
searchlist();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
searchtext.addTextChangedListener(textWatcher);
}
}
public void searchlist() {
searchkeyword = searchtext.getText().toString();
if (!searchtext.getText().toString().equals("")) {
listresults = controller.getAllEntryfromsearch();
} else {
listresults = controller.getAllEntry();
}
ListAdapter adapter = new SimpleAdapter(MainActivity.this, listresults,
R.layout.timeline,
new String[] { "Id", "title", "content", "date", "time",
"category", "tag", "location", "mood" }, new int[] {
R.id.Id, R.id.title, R.id.content, R.id.date,
R.id.time, R.id.category, R.id.tag, R.id.location,
R.id.mood });
setListAdapter(adapter);
count = lv.getCount();
counter.setText("Entries:" + count);
}
public void onAdd(View view) {
val = 1;
Intent objIntent = new Intent(getApplicationContext(), NewEntry.class);
startActivity(objIntent);
MainActivity.this.onDestroy();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo itemId) {
super.onCreateContextMenu(menu, v, itemId);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.action_list, menu);
menu.setHeaderTitle("Choose Action");
menu.setHeaderIcon(R.drawable.ic_action_warning);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
SQLiteDatabase database = controller.getWritableDatabase();
database.execSQL("DELETE FROM Entry Where Id="
+ Id.getText().toString() + "");
viewentry();
count = lv.getCount();
counter.setText("Entries:" + count);
Toast.makeText(getApplication(), "Deleted Successfully",
Toast.LENGTH_LONG).show();
break;
case R.id.mail:
final Intent send = new Intent(android.content.Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(android.content.Intent.EXTRA_SUBJECT, title.getText()
.toString());
send.putExtra(android.content.Intent.EXTRA_TEXT, content.getText()
.toString());
MainActivity.this.startActivity(android.content.Intent
.createChooser(send, "Send via"));
break;
case R.id.cancel:
break;
}
return super.onContextItemSelected(item);
}
}
有人能帮助我吗?
答案 0 :(得分:1)
有很多方法可以做到这一点。我使用这样的东西:
if (tv.length() == 0) {
tv.setVisibility(View.GONE)
}
答案 1 :(得分:1)
根据你的问题答案是
if (tv.length() == 0) {
tv.setVisibility(View.GONE);
}
或者您可以使用
if (TextUtils.isEmpty(tv.getText()){
tv.setVisibility(View.GONE);
}
的更新:强> 的
将此代码移至onCreate()方法。
Id = (TextView) v.findViewById(R.id.Id);
title = (TextView) v.findViewById(R.id.title);
content = (TextView) v.findViewById(R.id.content);
到
Id = (TextView)findViewById(R.id.Id);
title = (TextView)findViewById(R.id.title);
content = (TextView)findViewById(R.id.content);
答案 2 :(得分:0)
在文本视图中添加文本观察器。
当文本为空时,将visibilty设置为GONE
。
textview.setTextWatcher(new TextWatcher(){
private void hideIfEmpty(String s){
if(s.length == 0){
textview.setVisibility(View.GONE);
}
}
public void afterTextChanged(Editable s){
hideIfEmpty(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
hideIfEmpty(s.toString());
}
public void onTextChanged(CharSequence s, int start, int count){
hideIfEmpty(s.toString());
}
});